Multiple REST endpoints (one per version)

pull/1527/head
nelson.silva 2013-02-16 14:57:09 +00:00 committed by Nelson Silva
parent 8f279a6710
commit a5987285e0
1 changed files with 123 additions and 107 deletions

View File

@ -1,13 +1,9 @@
var log4js = require('log4js'); var log4js = require('log4js');
var express = require('express'); var express = require('express');
var swagger = require("swagger-node-express");
var apiHandler = require('../../handler/APIHandler'); var apiHandler = require('../../handler/APIHandler');
var apiCaller = require('./apicalls').apiCaller; var apiCaller = require('./apicalls').apiCaller;
var settings = require("../../utils/Settings"); var settings = require("../../utils/Settings");
var versions = Object.keys(apiHandler.version)
var version = versions[versions.length - 1];
var swaggerModels = { var swaggerModels = {
'models': { 'models': {
'SessionInfo' : { 'SessionInfo' : {
@ -177,7 +173,7 @@ var API = {
} }
}, },
"response": {"info":{"type":"SessionInfo"}} "response": {"info":{"type":"SessionInfo"}}
}, }
}, },
"pad": { "pad": {
"listAll" : { "listAll" : {
@ -192,7 +188,7 @@ var API = {
}, },
"create" : { "create" : {
"func" : "createPad", "func" : "createPad",
"description": "creates a new (non-group) pad. Note that if you need to create a group Pad, you should call createGroupPad", "description": "creates a new (non-group) pad. Note that if you need to create a group Pad, you should call createGroupPad"
}, },
"getText" : { "getText" : {
"func" : "getText", "func" : "getText",
@ -334,23 +330,32 @@ for (var resource in API) {
// Store the response model id // Store the response model id
API[resource][func]["responseClass"] = responseModelId; API[resource][func]["responseClass"] = responseModelId;
// get the api function
var apiFunc = apiHandler.version[version][API[resource][func]["func"]];
// Add the api function parameters
API[resource][func]["params"] = apiFunc.map( function(param) {
return swagger.queryParam(param, param, "string");
});
} }
} }
function newSwagger() {
var swagger_module = require.resolve("swagger-node-express");
if (require.cache[swagger_module]) {
// delete the child modules from cache
require.cache[swagger_module].children.forEach(function(m) {delete require.cache[m.id];});
// delete the module from cache
delete require.cache[swagger_module];
}
return require("swagger-node-express");
}
exports.expressCreateServer = function (hook_name, args, cb) { exports.expressCreateServer = function (hook_name, args, cb) {
for (var version in apiHandler.version) {
var swagger = newSwagger();
var basePath = "/rest/" + version;
// Let's put this under /rest for now // Let's put this under /rest for now
var subpath = express(); var subpath = express();
args.app.use(express.bodyParser()); args.app.use(express.bodyParser());
args.app.use("/rest", subpath); args.app.use(basePath, subpath);
swagger.setAppHandler(subpath); swagger.setAppHandler(subpath);
@ -361,6 +366,14 @@ exports.expressCreateServer = function (hook_name, args, cb) {
for (var funcName in API[resource]) { for (var funcName in API[resource]) {
var func = API[resource][funcName]; var func = API[resource][funcName];
// get the api function
var apiFunc = apiHandler.version[version][func["func"]];
// Skip this one if it does not exist in the version
if(!apiFunc) {
continue;
}
var swaggerFunc = { var swaggerFunc = {
'spec': { 'spec': {
"description" : func["description"], "description" : func["description"],
@ -368,7 +381,9 @@ exports.expressCreateServer = function (hook_name, args, cb) {
"summary" : funcName, "summary" : funcName,
"nickname" : funcName, "nickname" : funcName,
"method": "GET", "method": "GET",
"params" : func["params"], "params" : apiFunc.map( function(param) {
return swagger.queryParam(param, param, "string");
}),
"responseClass" : func["responseClass"] "responseClass" : func["responseClass"]
}, },
'action': (function(func, responseProcessor) { 'action': (function(func, responseProcessor) {
@ -394,7 +409,7 @@ exports.expressCreateServer = function (hook_name, args, cb) {
} }
response = JSON.stringify(response); response = JSON.stringify(response);
res.__swagger_send(response); res.__swagger_send(response);
} };
apiCaller(req, res, req.query); apiCaller(req, res, req.query);
}; };
@ -411,5 +426,6 @@ exports.expressCreateServer = function (hook_name, args, cb) {
swagger.configureSwaggerPaths("", "/api" , ""); swagger.configureSwaggerPaths("", "/api" , "");
swagger.configure("http://" + settings.ip + ":" + settings.port + "/rest", version); swagger.configure("http://" + settings.ip + ":" + settings.port + basePath, version);
} }
};