added support for template instanciation with jsonp closes #116

This commit is contained in:
javi
2014-01-29 13:11:37 +01:00
parent 67e4e7e99b
commit ad0b2ffc8e
2 changed files with 193 additions and 13 deletions
+54 -13
View File
@@ -393,7 +393,7 @@ var CartodbWindshaft = function(serverOptions) {
});
// Instantiate a template
ws.post(template_baseurl + '/:template_id', function(req, res) {
function instanciate(req, res, template_params, callback) {
ws.doCORS(res);
var that = this;
var response = {};
@@ -437,9 +437,10 @@ var CartodbWindshaft = function(serverOptions) {
err.http_status = 401;
throw err;
}
if ( ! req.headers['content-type'] || req.headers['content-type'].split(';')[0] != 'application/json' )
/*if ( (! req.headers['content-type'] || req.headers['content-type'].split(';')[0] != 'application/json') && req.query.callback === undefined) {
throw new Error('template POST data must be of type application/json, it is instead ');
var template_params = req.body;
}*/
//var template_params = req.body;
return templateMaps.instance(template, template_params);
},
function prepareParams(err, instance){
@@ -481,17 +482,57 @@ var CartodbWindshaft = function(serverOptions) {
response.layergroupid = cdbuser + '@' + response.layergroupid;
return response;
},
function finish(err, response){
if (err){
var statusCode = 400;
response = { error: ''+err };
if ( ! _.isUndefined(err.http_status) ) {
statusCode = err.http_status;
}
ws.sendError(res, response, statusCode, 'POST INSTANCE TEMPLATE', err.message);
} else {
res.send(response, 200);
callback
);
}
function finish_instanciation(err, response, res) {
if (err) {
var statusCode = 400;
response = { error: ''+err };
if ( ! _.isUndefined(err.http_status) ) {
statusCode = err.http_status;
}
ws.sendError(res, response, statusCode, 'POST INSTANCE TEMPLATE', err.message);
} else {
res.send(response, 200);
}
}
ws.post(template_baseurl + '/:template_id', function(req, res) {
Step(
function() {
if ( ! req.headers['content-type'] || req.headers['content-type'].split(';')[0] != 'application/json') {
throw new Error('template POST data must be of type application/json, it is instead ');
}
instanciate(req, res, req.body, this);
}, function(err, response) {
finish_instanciation(err, response, res);
}
);
});
/**
* jsonp endpoint, allows to instanciate a template with a json call.
* callback query argument is mandartoy
*/
ws.get(template_baseurl + '/:template_id/jsonp', function(req, res) {
Step(
function() {
if ( req.query.callback === undefined || req.query.callback.length === 0) {
throw new Error('callback parameter should be present and be a function name');
}
var config = {};
if(req.query.config) {
try {
config = JSON.parse(req.query.config);
} catch(e) {
throw new Error('badformed config parameter, should be a valid JSON');
}
}
instanciate(req, res, config, this);
}, function(err, response) {
finish_instanciation(err, response, res);
}
);
});