parent
80d5b6f894
commit
ffc1fa3fe1
@ -93,6 +93,8 @@ function handleQuery(req, res){
|
||||
// TODO: refactor formats to external object
|
||||
if (format === 'geojson'){
|
||||
toGeoJSON(result, res, this);
|
||||
} else if (format === 'csv'){
|
||||
toCSV(result, res, this);
|
||||
} else {
|
||||
var end = new Date().getTime();
|
||||
return {
|
||||
@ -107,10 +109,10 @@ function handleQuery(req, res){
|
||||
|
||||
// configure headers for geojson
|
||||
res.header("Content-Disposition", getContentDisposition(format));
|
||||
res.header("Content-Type", getContentType(format));
|
||||
|
||||
// allow cross site post
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
res.header("Access-Control-Allow-Headers", "X-Requested-With");
|
||||
setCrossDomain(res);
|
||||
|
||||
// set cache headers
|
||||
res.header('Last-Modified', new Date().toUTCString());
|
||||
@ -157,12 +159,48 @@ function toGeoJSON(data, res, callback){
|
||||
}
|
||||
}
|
||||
|
||||
function toCSV(data, res, callback){
|
||||
try{
|
||||
var out = ""
|
||||
if (data.rows.length > 0){
|
||||
out = out + _.keys(data.rows[0]).join(',') + '\n';
|
||||
_.each(data.rows, function(ele){
|
||||
out = out + _.values(ele).join(',') + '\n';
|
||||
});
|
||||
}
|
||||
|
||||
// return payload
|
||||
callback(null, out);
|
||||
} catch (err) {
|
||||
callback(err,null);
|
||||
}
|
||||
}
|
||||
|
||||
function getContentDisposition(format){
|
||||
var ext = (format === 'geojson') ? 'geojson' : 'json';
|
||||
var ext = 'json';
|
||||
if (format === 'geojson'){
|
||||
ext = 'geojson';
|
||||
}
|
||||
if (format === 'csv'){
|
||||
ext = 'csv';
|
||||
}
|
||||
var time = new Date().toUTCString();
|
||||
return 'inline; filename=cartodb-query.' + ext + '; modification-date="' + time + '";';
|
||||
}
|
||||
|
||||
function getContentType(format){
|
||||
var type = "application/json; charset=utf-8";
|
||||
if (format === 'csv'){
|
||||
type = "text/csv; charset=utf-8";
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
function setCrossDomain(res){
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
res.header("Access-Control-Allow-Headers", "X-Requested-With");
|
||||
}
|
||||
|
||||
function handleException(err, res){
|
||||
var msg = (global.settings.environment == 'development') ? {error:[err.message], stack: err.stack} : {error:[err.message]}
|
||||
if (global.settings.environment !== 'test'){
|
||||
@ -172,6 +210,9 @@ function handleException(err, res){
|
||||
console.log(err.stack);
|
||||
}
|
||||
|
||||
// allow cross site post
|
||||
setCrossDomain(res);
|
||||
|
||||
// if the exception defines a http status code, use that, else a 500
|
||||
if (!_.isUndefined(err.http_status)){
|
||||
res.send(msg, err.http_status);
|
||||
|
@ -132,6 +132,19 @@ tests['GET /api/v1/sql with SQL parameter and no format, ensuring content-dispos
|
||||
});
|
||||
};
|
||||
|
||||
tests['GET /api/v1/sql ensure cross domain set on errors'] = function(){
|
||||
assert.response(app, {
|
||||
url: '/api/v1/sql?q=SELECT%20*gadfgadfg%20FROM%20untitle_table_4',
|
||||
headers: {host: 'vizzuality.cartodb.com'},
|
||||
method: 'GET'
|
||||
},{
|
||||
status: 400
|
||||
}, function(res){
|
||||
var cd = res.header('Access-Control-Allow-Origin');
|
||||
assert.equal(cd, '*');
|
||||
});
|
||||
};
|
||||
|
||||
tests['GET /api/v1/sql as geojson limiting decimal places'] = function(){
|
||||
assert.response(app, {
|
||||
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson&dp=1',
|
||||
@ -145,6 +158,19 @@ tests['GET /api/v1/sql as geojson limiting decimal places'] = function(){
|
||||
});
|
||||
};
|
||||
|
||||
tests['GET /api/v1/sql as csv'] = function(){
|
||||
assert.response(app, {
|
||||
url: '/api/v1/sql?q=SELECT%20cartodb_id,ST_AsEWKT(the_geom)%20as%20geom%20FROM%20untitle_table_4%20LIMIT%201&format=csv',
|
||||
headers: {host: 'vizzuality.cartodb.com'},
|
||||
method: 'GET'
|
||||
},{
|
||||
status: 200
|
||||
}, function(res){
|
||||
var body = "cartodb_id,geom\n1,SRID=4326;POINT(-3.699732 40.423012)\n"
|
||||
assert.equal(body, res.body);
|
||||
});
|
||||
};
|
||||
|
||||
tests['GET system tables'] = function(){
|
||||
assert.response(app, {
|
||||
url: '/api/v1/sql?q=SELECT%20*%20FROM%20pg_attribute',
|
||||
|
Loading…
Reference in New Issue
Block a user