Added some sub-requests to the API

This commit is contained in:
Gaël Métais
2014-12-14 22:36:25 +01:00
parent 90dac7dd19
commit 2ab843efdb
3 changed files with 174 additions and 9 deletions
+45 -8
View File
@@ -60,7 +60,6 @@ var ApiController = function(app) {
// Send result if the user was waiting
if (run.params.waitForResponse) {
res.redirect(302, '/api/results/' + run.runId);
}
@@ -133,21 +132,59 @@ var ApiController = function(app) {
// Retrive one result by id
app.get('/api/results/:id', function(req, res) {
var runId = req.params.id;
getPartialResults(req.params.id, res, function(data) {
return data;
});
});
// Retrieve one result and return only the generalScores part of the response
app.get('/api/results/:id/generalScores', function(req, res) {
getPartialResults(req.params.id, res, function(data) {
return data.scoreProfiles.generic;
});
});
app.get('/api/results/:id/generalScores/:scoreProfile', function(req, res) {
getPartialResults(req.params.id, res, function(data) {
return data.scoreProfiles[req.params.scoreProfile];
});
});
app.get('/api/results/:id/rules', function(req, res) {
getPartialResults(req.params.id, res, function(data) {
return data.rules;
});
});
app.get('/api/results/:id/javascriptExecutionTree', function(req, res) {
getPartialResults(req.params.id, res, function(data) {
return data.javascriptExecutionTree;
});
});
app.get('/api/results/:id/toolsResults/phantomas', function(req, res) {
getPartialResults(req.params.id, res, function(data) {
return data.toolsResults.phantomas;
});
});
function getPartialResults(runId, res, partialGetterFn) {
resultsDatastore.getResult(runId)
.then(function(data) {
// This is the pivot format, we might need to clean it first?
// Hide phantomas results
data.toolsResults.phantomas = {};
var results = partialGetterFn(data);
if (typeof results === 'undefined') {
res.status(404).send('Not found');
return;
}
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(data, null, 2));
res.send(JSON.stringify(results, null, 2));
}).fail(function() {
res.status(404).send('Not found');
});
});
}
};