mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-02-01 03:12:42 +01:00
Add support for multiple api versions
This commit is contained in:
parent
3cbd59c769
commit
ea0f7cb2e9
2 changed files with 68 additions and 47 deletions
|
@ -38,38 +38,40 @@ catch(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
//a list of all functions
|
//a list of all functions
|
||||||
var functions = {
|
var version =
|
||||||
"createGroup" : [],
|
{ "1":
|
||||||
"createGroupIfNotExistsFor" : ["groupMapper"],
|
{ "createGroup" : []
|
||||||
"deleteGroup" : ["groupID"],
|
, "createGroupIfNotExistsFor" : ["groupMapper"]
|
||||||
"listPads" : ["groupID"],
|
, "deleteGroup" : ["groupID"]
|
||||||
"createPad" : ["padID", "text"],
|
, "listPads" : ["groupID"]
|
||||||
"createGroupPad" : ["groupID", "padName", "text"],
|
, "createPad" : ["padID", "text"]
|
||||||
"createAuthor" : ["name"],
|
, "createGroupPad" : ["groupID", "padName", "text"]
|
||||||
"createAuthorIfNotExistsFor": ["authorMapper" , "name"],
|
, "createAuthor" : ["name"]
|
||||||
"listPadsOfAuthor" : ["authorID"],
|
, "createAuthorIfNotExistsFor": ["authorMapper" , "name"]
|
||||||
"createSession" : ["groupID", "authorID", "validUntil"],
|
, "listPadsOfAuthor" : ["authorID"]
|
||||||
"deleteSession" : ["sessionID"],
|
, "createSession" : ["groupID", "authorID", "validUntil"]
|
||||||
"getSessionInfo" : ["sessionID"],
|
, "deleteSession" : ["sessionID"]
|
||||||
"listSessionsOfGroup" : ["groupID"],
|
, "getSessionInfo" : ["sessionID"]
|
||||||
"listSessionsOfAuthor" : ["authorID"],
|
, "listSessionsOfGroup" : ["groupID"]
|
||||||
"getText" : ["padID", "rev"],
|
, "listSessionsOfAuthor" : ["authorID"]
|
||||||
"setText" : ["padID", "text"],
|
, "getText" : ["padID", "rev"]
|
||||||
"getHTML" : ["padID", "rev"],
|
, "setText" : ["padID", "text"]
|
||||||
"setHTML" : ["padID", "html"],
|
, "getHTML" : ["padID", "rev"]
|
||||||
"getRevisionsCount" : ["padID"],
|
, "setHTML" : ["padID", "html"]
|
||||||
"getLastEdited" : ["padID"],
|
, "getRevisionsCount" : ["padID"]
|
||||||
"deletePad" : ["padID"],
|
, "getLastEdited" : ["padID"]
|
||||||
"getReadOnlyID" : ["padID"],
|
, "deletePad" : ["padID"]
|
||||||
"setPublicStatus" : ["padID", "publicStatus"],
|
, "getReadOnlyID" : ["padID"]
|
||||||
"getPublicStatus" : ["padID"],
|
, "setPublicStatus" : ["padID", "publicStatus"]
|
||||||
"setPassword" : ["padID", "password"],
|
, "getPublicStatus" : ["padID"]
|
||||||
"isPasswordProtected" : ["padID"],
|
, "setPassword" : ["padID", "password"]
|
||||||
"listAuthorsOfPad" : ["padID"],
|
, "isPasswordProtected" : ["padID"]
|
||||||
"padUsersCount" : ["padID"],
|
, "listAuthorsOfPad" : ["padID"]
|
||||||
"getAuthorName" : ["authorID"],
|
, "padUsersCount" : ["padID"]
|
||||||
"padUsers" : ["padID"],
|
, "getAuthorName" : ["authorID"]
|
||||||
"sendClientsMessage" : ["padID", "msg"]
|
, "padUsers" : ["padID"]
|
||||||
|
, "sendClientsMessage" : ["padID", "msg"]
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,18 +81,30 @@ var functions = {
|
||||||
* @req express request object
|
* @req express request object
|
||||||
* @res express response object
|
* @res express response object
|
||||||
*/
|
*/
|
||||||
exports.handle = function(functionName, fields, req, res)
|
exports.handle = function(apiVersion, functionName, fields, req, res)
|
||||||
{
|
{
|
||||||
//check the api key!
|
//check if this is a valid apiversion
|
||||||
if(fields["apikey"] != apikey.trim())
|
var isKnownApiVersion = false;
|
||||||
|
for(var knownApiVersion in version)
|
||||||
{
|
{
|
||||||
res.send({code: 4, message: "no or wrong API Key", data: null});
|
if(knownApiVersion == apiVersion)
|
||||||
|
{
|
||||||
|
isKnownApiVersion = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//say goodbye if this is an unkown API version
|
||||||
|
if(!isKnownApiVersion)
|
||||||
|
{
|
||||||
|
res.statusCode = 404;
|
||||||
|
res.send({code: 3, message: "no such api version", data: null});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//check if this is a valid function name
|
//check if this is a valid function name
|
||||||
var isKnownFunctionname = false;
|
var isKnownFunctionname = false;
|
||||||
for(var knownFunctionname in functions)
|
for(var knownFunctionname in version[apiVersion])
|
||||||
{
|
{
|
||||||
if(knownFunctionname == functionName)
|
if(knownFunctionname == functionName)
|
||||||
{
|
{
|
||||||
|
@ -105,6 +119,13 @@ exports.handle = function(functionName, fields, req, res)
|
||||||
res.send({code: 3, message: "no such function", data: null});
|
res.send({code: 3, message: "no such function", data: null});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//check the api key!
|
||||||
|
if(fields["apikey"] != apikey.trim())
|
||||||
|
{
|
||||||
|
res.send({code: 4, message: "no or wrong API Key", data: null});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//sanitize any pad id's before continuing
|
//sanitize any pad id's before continuing
|
||||||
if(fields["padID"])
|
if(fields["padID"])
|
||||||
|
@ -112,7 +133,7 @@ exports.handle = function(functionName, fields, req, res)
|
||||||
padManager.sanitizePadId(fields["padID"], function(padId)
|
padManager.sanitizePadId(fields["padID"], function(padId)
|
||||||
{
|
{
|
||||||
fields["padID"] = padId;
|
fields["padID"] = padId;
|
||||||
callAPI(functionName, fields, req, res);
|
callAPI(apiVersion, functionName, fields, req, res);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if(fields["padName"])
|
else if(fields["padName"])
|
||||||
|
@ -120,23 +141,23 @@ exports.handle = function(functionName, fields, req, res)
|
||||||
padManager.sanitizePadId(fields["padName"], function(padId)
|
padManager.sanitizePadId(fields["padName"], function(padId)
|
||||||
{
|
{
|
||||||
fields["padName"] = padId;
|
fields["padName"] = padId;
|
||||||
callAPI(functionName, fields, req, res);
|
callAPI(apiVersion, functionName, fields, req, res);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
callAPI(functionName, fields, req, res);
|
callAPI(apiVersion, functionName, fields, req, res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//calls the api function
|
//calls the api function
|
||||||
function callAPI(functionName, fields, req, res)
|
function callAPI(apiVersion, functionName, fields, req, res)
|
||||||
{
|
{
|
||||||
//put the function parameters in an array
|
//put the function parameters in an array
|
||||||
var functionParams = [];
|
var functionParams = [];
|
||||||
for(var i=0;i<functions[functionName].length;i++)
|
for(var i=0;i<version[apiVersion][functionName].length;i++)
|
||||||
{
|
{
|
||||||
functionParams.push(fields[functions[functionName][i]]);
|
functionParams.push(fields[ version[apiVersion][functionName][i] ]);
|
||||||
}
|
}
|
||||||
|
|
||||||
//add a callback function to handle the response
|
//add a callback function to handle the response
|
||||||
|
|
|
@ -7,7 +7,7 @@ var apiHandler = require('../../handler/APIHandler');
|
||||||
var apiCaller = function(req, res, fields) {
|
var apiCaller = function(req, res, fields) {
|
||||||
res.header("Content-Type", "application/json; charset=utf-8");
|
res.header("Content-Type", "application/json; charset=utf-8");
|
||||||
|
|
||||||
apiLogger.info("REQUEST, " + req.params.func + ", " + JSON.stringify(fields));
|
apiLogger.info("REQUEST, v"+ req.params.version + ":" + req.params.func + ", " + JSON.stringify(fields));
|
||||||
|
|
||||||
//wrap the send function so we can log the response
|
//wrap the send function so we can log the response
|
||||||
//note: res._send seems to be already in use, so better use a "unique" name
|
//note: res._send seems to be already in use, so better use a "unique" name
|
||||||
|
@ -24,19 +24,19 @@ var apiCaller = function(req, res, fields) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//call the api handler
|
//call the api handler
|
||||||
apiHandler.handle(req.params.func, fields, req, res);
|
apiHandler.handle(req.params.version, req.params.func, fields, req, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.apiCaller = apiCaller;
|
exports.apiCaller = apiCaller;
|
||||||
|
|
||||||
exports.expressCreateServer = function (hook_name, args, cb) {
|
exports.expressCreateServer = function (hook_name, args, cb) {
|
||||||
//This is a api GET call, collect all post informations and pass it to the apiHandler
|
//This is a api GET call, collect all post informations and pass it to the apiHandler
|
||||||
args.app.get('/api/1/:func', function (req, res) {
|
args.app.get('/api/:version/:func', function (req, res) {
|
||||||
apiCaller(req, res, req.query)
|
apiCaller(req, res, req.query)
|
||||||
});
|
});
|
||||||
|
|
||||||
//This is a api POST call, collect all post informations and pass it to the apiHandler
|
//This is a api POST call, collect all post informations and pass it to the apiHandler
|
||||||
args.app.post('/api/1/:func', function(req, res) {
|
args.app.post('/api/:version/:func', function(req, res) {
|
||||||
new formidable.IncomingForm().parse(req, function (err, fields, files) {
|
new formidable.IncomingForm().parse(req, function (err, fields, files) {
|
||||||
apiCaller(req, res, fields)
|
apiCaller(req, res, fields)
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue