Mvolz has uploaded a new change for review. https://gerrit.wikimedia.org/r/212277
Change subject: [WIP] Create CitoidRequest and promisify service ...................................................................... [WIP] Create CitoidRequest and promisify service Super wip at this point. Bug: T75993 Change-Id: Ie0a98d2cf8518171a536c4c911053adf2da74b43 --- A lib/CitoidRequest.js A lib/CitoidResponse.js M lib/CitoidService.js M routes/root.js 4 files changed, 88 insertions(+), 47 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/citoid refs/changes/77/212277/1 diff --git a/lib/CitoidRequest.js b/lib/CitoidRequest.js new file mode 100644 index 0000000..016d154 --- /dev/null +++ b/lib/CitoidRequest.js @@ -0,0 +1,33 @@ +'use strict'; + +/** + * An object corresponding to each request to the citoid service + */ + +var CitoidReponse = require('./CitoidResponse.js'); + +/** + * Constructor for CitoidRequest object + * @param {Object} req raw request object from express + * @param {Object} opts cleaned request parameters + * @param {Object} logger logger object, must have a log() method + * @param {Object} statsd metrics object + */ +function CitoidRequest(req, logger, statsd) { + + this.request = req; + this.logger = logger; + this.stats = statsd; + + this.idType; // Type of identifier i.e. url, doi + + this.acceptLanguage = this.request.headers['accept-language']; + this.format = encodeURIComponent(this.request.body.format); + this.encodedSearch = encodeURIComponent(this.request.body.search); // Should already be encoded, but reencode in case + this.search = decodeURIComponent(this.encodedSearch); + + //this.response = new CitoidResponse(this, logger, statsd); // Prepare a response + +} + +module.exports = CitoidRequest; diff --git a/lib/CitoidResponse.js b/lib/CitoidResponse.js new file mode 100644 index 0000000..0211a42 --- /dev/null +++ b/lib/CitoidResponse.js @@ -0,0 +1,26 @@ +'use strict'; + +/** + * An object corresponding to each request to the citoid service + */ + +/** + * Constructor for CitoidRequest object + * @param {Object} cr CitoidRequest option + * @param {Object} logger logger object, must have a log() method + * @param {Object} statsd metrics object + */ +function CitoidResponse(cr, logger, stats) { + + this.logger = logger; + this.status = stats; + + this.citation = {}; // Initialise empty native citation object + this.body; // Citation in requested format, or error; final body value + this.status; // Integer response code + this.error; // Error object + + this.format = cr.format; +} + +module.exports = CitoidResponse; diff --git a/lib/CitoidService.js b/lib/CitoidService.js index d0dc979..76aef29 100644 --- a/lib/CitoidService.js +++ b/lib/CitoidService.js @@ -12,6 +12,7 @@ var unshorten = require('./unshorten.js'); var Scraper = require('./Scraper.js'); var ZoteroService = require('./ZoteroService.js'); +var CitoidRequest = require('./CitoidRequest.js'); var pubMedRequest = require('./pubMedRequest.js'); /** @@ -30,19 +31,19 @@ /** * Requests to the citoid service - * @param {Object} opts options object containing request information - * @param {Function} callback callback (error, statusCode, body) + * @param {Object} opts request parameters + * @param {Object} req full Request object + * @param {Function} callback callback (error, statusCode, body) */ -CitoidService.prototype.request = function(opts, callback){ +CitoidService.prototype.request = function(cr, callback){ var runnerOpts; - var citoidService = this; - citoidService.distinguish(opts.search, function(extractedID, runnerFunction){ + this.distinguish(cr.search, function(extractedID, runnerFunction){ runnerOpts = { - format : opts.format, + format : cr.format, search : extractedID, - acceptLanguage : opts.acceptLanguage + acceptLanguage : cr.acceptLanguage }; runnerFunction(runnerOpts, callback); }); diff --git a/routes/root.js b/routes/root.js index f34c024..e3d9b1d 100644 --- a/routes/root.js +++ b/routes/root.js @@ -2,7 +2,7 @@ var sUtil = require('../lib/util'); - +var CitoidRequest = require('../lib/CitoidRequest.js'); /** * The main router object @@ -35,37 +35,31 @@ */ router.post('/url', function(req, res) { - var opts; - var acceptLanguage = req.headers['accept-language']; - var format = req.body.format; - var requestedURL = req.body.url; - var eFormat = encodeURIComponent(format); + var cr = new CitoidRequest(req, app.logger, app.statsd); - // Temp backwards compatibility - if (!format) { - format = 'mwDeprecated'; + // Backwards compatibility with prior version of API which did not require format + if (!req.body.format) { + cr.format = 'mwDeprecated'; } - if (!requestedURL) { + if (!req.body.url) { res.status(400).type('application/json'); res.send({Error:"No 'url' value specified"}); return; } - if (!app.formats[format]) { + // Overwrite search value with uri encoded url + cr.search = encodeURI(req.body.url); + + // Ensure format is supported + if (!app.formats[cr.format]) { res.status(400).type('application/json'); - res.send({Error:'Invalid format requested ' + eFormat}); + res.send({Error:'Invalid format requested ' + cr.format}); return; } - opts = { - search: requestedURL, - format: eFormat, - acceptLanguage: acceptLanguage - }; - - app.citoid.request(opts, function(error, responseCode, body){ - res.status(responseCode).type(app.formats[format]); + app.citoid.request(cr, function(error, responseCode, body){ + res.status(responseCode).type(app.formats[cr.format]); res.send(body); }); @@ -78,37 +72,24 @@ */ router.get('/api', function(req, res) { - var dSearch; - var opts; - var acceptLanguage = req.headers['accept-language']; - var format = req.query.format; - var search = req.query.search; - var eFormat = encodeURIComponent(format); // Encoded format + var cr = new CitoidRequest(req, app.logger, app.statsd); - if (!search) { + if (!req.body.search) { res.status(400).type('application/json'); res.send({Error:"No 'search' value specified"}); return; - } else if(!format) { + } else if(!req.body.format) { res.status(400).type('application/json'); res.send({Error:"No 'format' value specified"}); return; - } else if (!app.formats[format]) { + } else if (!app.formats[cr.format]) { // Use encoded format res.status(400).type('application/json'); - res.send({Error:'Invalid format requested ' + eFormat}); + res.send({Error:'Invalid format requested ' + cr.format}); return; } - dSearch = decodeURIComponent(encodeURI(search)); // Decode urlencoded search string - - opts = { - search: dSearch, - format: eFormat, - acceptLanguage: acceptLanguage - }; - - app.citoid.request(opts, function(error, responseCode, body) { - res.status(responseCode).type(app.formats[format]); + app.citoid.request(cr, function(error, responseCode, body) { + res.status(responseCode).type(app.formats[cr.format]); res.send(body); }); -- To view, visit https://gerrit.wikimedia.org/r/212277 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie0a98d2cf8518171a536c4c911053adf2da74b43 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/citoid Gerrit-Branch: master Gerrit-Owner: Mvolz <mv...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits