Implemented GET for jobs API

This commit is contained in:
Daniel García Aubert 2015-12-22 11:45:25 +01:00
parent ef59a1d942
commit 30d0b2906b
2 changed files with 106 additions and 3 deletions

View File

@ -25,16 +25,100 @@ function JobController(metadataBackend, tableCache, statsd_client) {
JobController.prototype.route = function (app) {
app.post(global.settings.base_url + '/job', this.createJob.bind(this));
app.get(global.settings.base_url + '/job:jobId', this.getJob.bind(this));
app.get(global.settings.base_url + '/job/:jobId', this.getJob.bind(this));
};
JobController.prototype.getJob = function (req, res) {
var self = this;
var jobId = req.params.jobId;
var body = (req.body) ? req.body : {};
var params = _.extend({}, req.query, body); // clone so don't modify req.params or req.body so oauth is not broken
var sql = (params.query === "" || _.isUndefined(params.query)) ? null : params.query;
var cdbUsername = cdbReq.userByReq(req);
if (!_.isString(jobId)) {
return handleException(new Error("You must indicate a job id"), res);
}
if ( req.profiler ) {
req.profiler.start('sqlapi.job');
}
req.aborted = false;
req.on("close", function() {
if (req.formatter && _.isFunction(req.formatter.cancel)) {
req.formatter.cancel();
}
req.aborted = true; // TODO: there must be a builtin way to check this
});
function checkAborted(step) {
if ( req.aborted ) {
var err = new Error("Request aborted during " + step);
// We'll use status 499, same as ngnix in these cases
// see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
err.http_status = 499;
throw err;
}
}
if ( req.profiler ) {
req.profiler.done('init');
}
step(
function getUserDBInfo() {
var options = {
req: req,
params: params,
checkAborted: checkAborted,
metadataBackend: self.metadataBackend,
cdbUsername: cdbUsername
};
userDatabaseService.getUserDatabase(options, this);
},
function getJob(err, userDatabase) {
assert.ifError(err);
var next = this;
checkAborted('persistJob');
if ( req.profiler ) {
req.profiler.done('setDBAuth');
}
self.jobBackend.get(jobId, function (err, job) {
if (err) {
return next(err);
}
next(null, {
job: job,
userDatabase: userDatabase
});
});
},
function handleResponse(err, result) {
if ( err ) {
handleException(err, res);
}
if ( req.profiler ) {
req.profiler.done('enqueueJob');
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
}
if (global.settings.api_hostname) {
res.header('X-Served-By-Host', global.settings.api_hostname);
}
if (result.host) {
res.header('X-Served-By-DB-Host', result.host);
}
res.send(result.job);
}
);
};
// jshint maxcomplexity:21
@ -146,6 +230,7 @@ JobController.prototype.createJob = function (req, res) {
if (result.host) {
res.header('X-Served-By-DB-Host', result.host);
}
res.send(result.job);
}
);

View File

@ -19,6 +19,7 @@ var assert = require('../support/assert');
var querystring = require('querystring');
describe('job', function() {
var job = {};
it('POST /api/v2/job', function (done){
assert.response(app, {
@ -31,7 +32,7 @@ describe('job', function() {
}, {
status: 200
}, function(res) {
var job = JSON.parse(res.body);
job = JSON.parse(res.body);
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(job.query, "SELECT * FROM untitle_table_4");
assert.equal(job.status, "pending");
@ -40,4 +41,21 @@ describe('job', function() {
});
});
it('GET /api/v2/job/:jobId', function (done){
assert.response(app, {
url: '/api/v2/job/' + job.jobId,
headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'GET'
}, {
status: 200
}, function(res) {
var jobGot = JSON.parse(res.body);
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(jobGot.query, "SELECT * FROM untitle_table_4");
assert.equal(jobGot.status, "pending");
assert.equal(jobGot.user, "vizzuality");
done();
});
});
});