Added some fixes to make it work with the codegen

This commit is contained in:
Nelson Silva 2013-02-15 14:10:03 +00:00
parent 1cfc8eda19
commit 8f279a6710
1 changed files with 71 additions and 34 deletions

View File

@ -142,11 +142,18 @@ var API = {
"description": "returns all sessions of an author", "description": "returns all sessions of an author",
"response": {"sessions":{"type":"List", "items":{"type":"SessionInfo"}}} "response": {"sessions":{"type":"List", "items":{"type":"SessionInfo"}}}
}, },
// We need an operation that return a UserInfo so it can be picked up by the codegen :(
"getName" : { "getName" : {
"func": "getAuthorName", "func": "getAuthorName",
"description": "Returns the Author Name of the author", "description": "Returns the Author Name of the author",
"response": {"authorName":{"type":"string"}} "responseProcessor": function(response) {
}, if (response.data) {
response["info"] = {"name": response.data.authorName};
delete response["data"];
}
},
"response": {"info":{"type":"UserInfo"}}
}
}, },
"session": { "session": {
"create" : { "create" : {
@ -158,10 +165,18 @@ var API = {
"func": "deleteSession", "func": "deleteSession",
"description": "deletes a session" "description": "deletes a session"
}, },
// We need an operation that returns a SessionInfo so it can be picked up by the codegen :(
"info": { "info": {
"func": "getSessionInfo", "func": "getSessionInfo",
"description": "returns informations about a session", "description": "returns informations about a session",
"response": {"authorID":{"type":"string"}, "groupID":{"type":"string"}, "validUntil":{"type":"long"}} "responseProcessor": function(response) {
// move this to info
if (response.data) {
response["info"] = response.data;
delete response["data"];
}
},
"response": {"info":{"type":"SessionInfo"}}
}, },
}, },
"pad": { "pad": {
@ -181,12 +196,12 @@ var API = {
}, },
"getText" : { "getText" : {
"func" : "getText", "func" : "getText",
"description": "returns the text of a pad" "description": "returns the text of a pad",
"response": {"text":{"type":"string"}}
}, },
"setText" : { "setText" : {
"func" : "setText", "func" : "setText",
"description": "sets the text of a pad", "description": "sets the text of a pad"
"response": {"groupID":{"type":"string"}}
}, },
"getHTML": { "getHTML": {
"func" : "getHTML", "func" : "getHTML",
@ -223,7 +238,7 @@ var API = {
"getPublicStatus": { "getPublicStatus": {
"func": "getPublicStatus", "func": "getPublicStatus",
"description": "return true of false", "description": "return true of false",
"response": {"publicStatus":{"type":"bool"}} "response": {"publicStatus":{"type":"boolean"}}
}, },
"setPassword": { "setPassword": {
"func": "setPassword", "func": "setPassword",
@ -232,7 +247,7 @@ var API = {
"isPasswordProtected": { "isPasswordProtected": {
"func": "isPasswordProtected", "func": "isPasswordProtected",
"description": "returns true or false", "description": "returns true or false",
"response": {"passwordProtection":{"type":"bool"}} "response": {"passwordProtection":{"type":"boolean"}}
}, },
"authors": { "authors": {
"func": "listAuthorsOfPad", "func": "listAuthorsOfPad",
@ -247,7 +262,7 @@ var API = {
"users": { "users": {
"func": "padUsers", "func": "padUsers",
"description": "returns the list of users that are currently editing this pad", "description": "returns the list of users that are currently editing this pad",
"response": {"padUsers":{"type":"Lists", "items":{"type": "UserInfo"}}} "response": {"padUsers":{"type":"List", "items":{"type": "UserInfo"}}}
}, },
"sendClientsMessage": { "sendClientsMessage": {
"func": "sendClientsMessage", "func": "sendClientsMessage",
@ -262,10 +277,18 @@ var API = {
"description": "returns the chat history", "description": "returns the chat history",
"response": {"messages":{"type":"List", "items": {"type" : "Message"}}} "response": {"messages":{"type":"List", "items": {"type" : "Message"}}}
}, },
// We need an operation that returns a Message so it can be picked up by the codegen :(
"getChatHead": { "getChatHead": {
"func": "getChatHead", "func": "getChatHead",
"description": "returns the chatHead (last number of the last chat-message) of the pad", "description": "returns the chatHead (chat-message) of the pad",
"response": {"chatHead":{"type":"long"}} "responseProcessor": function(response) {
// move this to info
if (response.data) {
response["chatHead"] = {"time": response.data["chatHead"]};
delete response["data"];
}
},
"response": {"chatHead":{"type":"Message"}}
} }
} }
}; };
@ -277,11 +300,8 @@ function capitalise(string){
for (var resource in API) { for (var resource in API) {
for (var func in API[resource]) { for (var func in API[resource]) {
// Add the response model // The base response model
var responseModelId = capitalise(resource) + capitalise(func) + "Response"; var responseModel = {
swaggerModels['models'][responseModelId] = {
"id": responseModelId,
"properties": { "properties": {
"code":{ "code":{
"type":"int" "type":"int"
@ -292,20 +312,25 @@ for (var resource in API) {
} }
}; };
// This returns some data var responseModelId = "Response";
if (API[resource][func]["response"]) {
// Add the data model
var dataModelId = capitalise(resource) + capitalise(func) + "Data";
swaggerModels['models'][dataModelId] = {
"id": dataModelId,
"properties": API[resource][func]["response"]
};
swaggerModels['models'][responseModelId]["properties"]["data"] = { // Add the data properties (if any) to the response
"type": dataModelId if (API[resource][func]["response"]) {
}; // This is a specific response so let's set a new id
responseModelId = capitalise(resource) + capitalise(func) + "Response";
for(var prop in API[resource][func]["response"]) {
var propType = API[resource][func]["response"][prop];
responseModel["properties"][prop] = propType;
}
} }
// Add the id
responseModel["id"] = responseModelId;
// Add this to the swagger models
swaggerModels['models'][responseModelId] = responseModel;
// Store the response model id // Store the response model id
API[resource][func]["responseClass"] = responseModelId; API[resource][func]["responseClass"] = responseModelId;
@ -351,14 +376,26 @@ exports.expressCreateServer = function (hook_name, args, cb) {
req.params.version = version; req.params.version = version;
req.params.func = func; // call the api function req.params.func = func; // call the api function
if (responseProcessor) { //wrap the send function so we can process the response
//wrap the send function so we can process the response res.__swagger_send = res.send;
res.__swagger_send = res.send; res.send = function (response) {
res.send = function (response) { // ugly but we need to get this as json
response = JSON.parse(response);
// process the response if needed
if (responseProcessor) {
response = responseProcessor(response); response = responseProcessor(response);
res.__swagger_send(response);
} }
// Let's move everything out of "data"
if (response.data) {
for(var prop in response.data) {
response[prop] = response.data[prop];
delete response.data;
}
}
response = JSON.stringify(response);
res.__swagger_send(response);
} }
apiCaller(req, res, req.query); apiCaller(req, res, req.query);
}; };
})(func["func"], func["responseProcessor"]) // must use a closure here })(func["func"], func["responseProcessor"]) // must use a closure here