pad.pub0.org/node/handler/APIHandler.js

161 lines
4.5 KiB
JavaScript
Raw Normal View History

/**
* The API Handler handles all API http requests
*/
/*
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var ERR = require("async-stacktrace");
var fs = require("fs");
var api = require("../db/API");
2011-12-16 20:41:11 +00:00
var padManager = require("../db/PadManager");
2012-01-28 12:24:58 +00:00
var randomString = require("../utils/randomstring");
//ensure we have an apikey
var apikey = null;
try
{
apikey = fs.readFileSync("../APIKEY.txt","utf8");
}
catch(e)
{
apikey = randomString(32);
fs.writeFileSync("../APIKEY.txt",apikey,"utf8");
}
//a list of all functions
var functions = {
2011-08-08 15:21:31 +00:00
"createGroup" : [],
"createGroupIfNotExistsFor" : ["groupMapper"],
2011-08-22 12:11:34 +00:00
"deleteGroup" : ["groupID"],
2011-08-08 16:35:40 +00:00
"listPads" : ["groupID"],
2011-08-04 18:20:14 +00:00
"createPad" : ["padID", "text"],
2011-08-08 16:35:40 +00:00
"createGroupPad" : ["groupID", "padName", "text"],
2011-08-09 11:09:04 +00:00
"createAuthor" : ["name"],
2011-08-16 19:02:30 +00:00
"createAuthorIfNotExistsFor": ["authorMapper" , "name"],
2011-08-09 15:45:49 +00:00
"createSession" : ["groupID", "authorID", "validUntil"],
2011-08-10 13:04:28 +00:00
"deleteSession" : ["sessionID"],
2011-08-09 15:45:49 +00:00
"getSessionInfo" : ["sessionID"],
"listSessionsOfGroup" : ["groupID"],
"listSessionsOfAuthor" : ["authorID"],
2011-08-04 16:18:59 +00:00
"getText" : ["padID", "rev"],
2011-08-04 16:24:36 +00:00
"setText" : ["padID", "text"],
"getHTML" : ["padID", "rev"],
"setHTML" : ["padID", "html"],
2011-08-04 16:24:36 +00:00
"getRevisionsCount" : ["padID"],
2011-08-16 19:02:30 +00:00
"deletePad" : ["padID"],
"getReadOnlyID" : ["padID"],
"setPublicStatus" : ["padID", "publicStatus"],
"getPublicStatus" : ["padID"],
"setPassword" : ["padID", "password"],
"isPasswordProtected" : ["padID"]
};
/**
* Handles a HTTP API call
* @param functionName the name of the called function
* @param fields the params of the called function
* @req express request object
* @res express response object
*/
exports.handle = function(functionName, fields, req, res)
{
//check the api key!
if(fields["apikey"] != apikey.trim())
{
res.send({code: 4, message: "no or wrong API Key", data: null});
return;
}
//check if this is a valid function name
var isKnownFunctionname = false;
for(var knownFunctionname in functions)
{
if(knownFunctionname == functionName)
{
isKnownFunctionname = true;
break;
}
}
//say goodbye if this is a unkown function
if(!isKnownFunctionname)
{
res.send({code: 3, message: "no such function", data: null});
return;
}
2011-12-16 20:41:11 +00:00
//sanitize any pad id's before continuing
if(fields["padID"])
{
padManager.sanitizePadId(fields["padID"], function(padId)
{
fields["padID"] = padId;
callAPI(functionName, fields, req, res);
});
}
else if(fields["padName"])
{
padManager.sanitizePadId(fields["padName"], function(padId)
{
fields["padName"] = padId;
callAPI(functionName, fields, req, res);
});
}
else
{
callAPI(functionName, fields, req, res);
}
}
//calls the api function
function callAPI(functionName, fields, req, res)
{
//put the function parameters in an array
var functionParams = [];
for(var i=0;i<functions[functionName].length;i++)
{
functionParams.push(fields[functions[functionName][i]]);
}
//add a callback function to handle the response
functionParams.push(function(err, data)
{
// no error happend, everything is fine
if(err == null)
{
2011-08-04 18:22:46 +00:00
if(!data)
data = null;
res.send({code: 0, message: "ok", data: data});
}
// parameters were wrong and the api stopped execution, pass the error
else if(err.name == "apierror")
{
res.send({code: 1, message: err.message, data: null});
}
//an unkown error happend
else
{
res.send({code: 2, message: "internal error", data: null});
ERR(err);
}
});
//call the api function
api[functionName](functionParams[0],functionParams[1],functionParams[2],functionParams[3],functionParams[4]);
}