[MediaWiki-commits] [Gerrit] mediawiki...cxserver[master]: Combine page fetch, segmentation to a new class

2017-10-16 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/383109 )

Change subject: Combine page fetch, segmentation to a new class
..


Combine page fetch, segmentation to a new class

* Simplify the API end point for page/:language/:title/:revision?
  in v1.js. Router is not supposed to data processing as it does now.
* Move pageloader/PageLoader.js to mw/MWPageLoader class which now does
  page fetch and segmentation call. In follow ups, we will add section
  wrapping call to this class (T177752).
* The new MWPageLoader class inherits APIRequest. Move the remaining
  utility methods in apiUtil to APIRequest class so that api related
  methods are in single place.

Change-Id: I2ad6e1f183c8fe69c960597a29632989965cce82
---
M app.js
D lib/api-util.js
M lib/mw/ApiRequest.js
A lib/mw/MWPageLoader.js
D lib/pageloader/PageLoader.js
M lib/routes/v1.js
6 files changed, 164 insertions(+), 174 deletions(-)

Approvals:
  jenkins-bot: Verified
  Nikerabbit: Looks good to me, approved



diff --git a/app.js b/app.js
index 907fbe6..1795d04 100644
--- a/app.js
+++ b/app.js
@@ -7,7 +7,6 @@
bodyParser = require( 'body-parser' ),
fs = BBPromise.promisifyAll( require( 'fs' ) ),
sUtil = require( './lib/util' ),
-   apiUtil = require( './lib/api-util' ),
packageInfo = require( './package.json' ),
yaml = require( 'js-yaml' );
 
@@ -59,9 +58,6 @@
}
}
}
-
-   // set up the request templates for the APIs
-   apiUtil.setupApiTemplates( app );
 
// set up the spec
if ( !app.conf.spec ) {
diff --git a/lib/api-util.js b/lib/api-util.js
deleted file mode 100644
index 435cd37..000
--- a/lib/api-util.js
+++ /dev/null
@@ -1,80 +0,0 @@
-'use strict';
-
-var preq = require( 'preq' ),
-   Template = require( 'swagger-router' ).Template;
-
-/**
- * Calls the REST API with the supplied domain, path and request parameters
- *
- * @param {Object} app the application object
- * @param {string} domain the domain to issue the request for
- * @param {string} path the REST API path to contact without the leading slash
- * @param {Object} [restReq={}] the object containing the REST request details
- * @param {string} [restReq.method=get] the request method
- * @param {Object} [restReq.query={}] the query string to send, if any
- * @param {Object} [restReq.headers={}] the request headers to send
- * @param {Object} [restReq.body=null] the body of the request, if any
- * @return {Promise} a promise resolving as the response object from the REST 
API
- *
- */
-function restApiGet( app, domain, path, restReq ) {
-   var request;
-   restReq = restReq || {};
-   path = path[ 0 ] === '/' ? path.slice( 1 ) : path;
-
-   request = app.restbase_tpl.expand( {
-   request: {
-   method: restReq.method,
-   params: {
-   domain: domain,
-   path: path
-   },
-   query: restReq.query,
-   headers: Object.assign( {
-   'user-agent': app.conf.user_agent
-   }, restReq.headers ),
-   body: restReq.body
-   }
-   } );
-
-   return preq( request );
-}
-
-/**
- * Sets up the request templates for MW and RESTBase API requests
- *
- * @param {Application} app the application object
- */
-function setupApiTemplates( app ) {
-   /* eslint-disable camelcase */
-   // set up the MW API request template
-   if ( !app.conf.mwapi_req ) {
-   app.conf.mwapi_req = {
-   uri: 'https://{{domain}}/w/api.php',
-   headers: {
-   'user-agent': '{{user-agent}}'
-   },
-   query: '{{ default(request.query, {}) }}',
-   body: '{{request.body}}'
-   };
-   }
-   app.mwapi_tpl = new Template( app.conf.mwapi_req );
-
-   // set up the RESTBase request template
-   if ( !app.conf.restbase_req ) {
-   app.conf.restbase_req = {
-   method: '{{request.method}}',
-   uri: 'http://{{domain}}/api/rest_v1/{+path}',
-   query: '{{ default(request.query, {}) }}',
-   headers: '{{request.headers}}',
-   body: '{{request.body}}'
-   };
-   }
-   app.restbase_tpl = new Template( app.conf.restbase_req );
-   /* eslint-enable camelcase */
-}
-
-module.exports = {
-   restApiGet: restApiGet,
-   setupApiTemplates: setupApiTemplates
-};
diff --git a/lib/mw/ApiRequest.js b/lib/mw/ApiRequest.js
index 58c910d..492b5a3 100644
--- a/lib/mw/ApiRequest.js
+++ b/lib/mw/ApiRequest.js
@@ -1,10 +1,10 @@
 'use 

[MediaWiki-commits] [Gerrit] mediawiki...cxserver[master]: Combine page fetch, segmentation to a new class

2017-10-09 Thread Santhosh (Code Review)
Santhosh has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/383109 )

Change subject: Combine page fetch, segmentation to a new class
..

Combine page fetch, segmentation to a new class

* Simplify the API end point for page/:language/:title/:revision?
  in v1.js. Router is not supposed to data processing as it does now.
* Move pageloader/PageLoader.js to mw/MWPage class which now does page
  fetch and segmentation call. In follow ups, we will add section
  wrapping call to ths class(T177752).
* The new MWPage class inherits APIRequest. Move the remaining utility
  methods in apiUtil to APIRequest class so that api related methods
  are in single place.

Known issues to be fixed:
* The language parameter passed to the /page api is sometimes domain
  because of the way services is keeping cxserver behind
  xx.wikipedia.org/api. Using our domain template we need to understand
  which one is passed and derive language code from it.

Change-Id: I2ad6e1f183c8fe69c960597a29632989965cce82
---
M app.js
D lib/api-util.js
M lib/mw/ApiRequest.js
A lib/mw/MWPage.js
D lib/pageloader/PageLoader.js
M lib/routes/v1.js
6 files changed, 145 insertions(+), 169 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/cxserver 
refs/changes/09/383109/1

diff --git a/app.js b/app.js
index 907fbe6..1795d04 100644
--- a/app.js
+++ b/app.js
@@ -7,7 +7,6 @@
bodyParser = require( 'body-parser' ),
fs = BBPromise.promisifyAll( require( 'fs' ) ),
sUtil = require( './lib/util' ),
-   apiUtil = require( './lib/api-util' ),
packageInfo = require( './package.json' ),
yaml = require( 'js-yaml' );
 
@@ -59,9 +58,6 @@
}
}
}
-
-   // set up the request templates for the APIs
-   apiUtil.setupApiTemplates( app );
 
// set up the spec
if ( !app.conf.spec ) {
diff --git a/lib/api-util.js b/lib/api-util.js
deleted file mode 100644
index 435cd37..000
--- a/lib/api-util.js
+++ /dev/null
@@ -1,80 +0,0 @@
-'use strict';
-
-var preq = require( 'preq' ),
-   Template = require( 'swagger-router' ).Template;
-
-/**
- * Calls the REST API with the supplied domain, path and request parameters
- *
- * @param {Object} app the application object
- * @param {string} domain the domain to issue the request for
- * @param {string} path the REST API path to contact without the leading slash
- * @param {Object} [restReq={}] the object containing the REST request details
- * @param {string} [restReq.method=get] the request method
- * @param {Object} [restReq.query={}] the query string to send, if any
- * @param {Object} [restReq.headers={}] the request headers to send
- * @param {Object} [restReq.body=null] the body of the request, if any
- * @return {Promise} a promise resolving as the response object from the REST 
API
- *
- */
-function restApiGet( app, domain, path, restReq ) {
-   var request;
-   restReq = restReq || {};
-   path = path[ 0 ] === '/' ? path.slice( 1 ) : path;
-
-   request = app.restbase_tpl.expand( {
-   request: {
-   method: restReq.method,
-   params: {
-   domain: domain,
-   path: path
-   },
-   query: restReq.query,
-   headers: Object.assign( {
-   'user-agent': app.conf.user_agent
-   }, restReq.headers ),
-   body: restReq.body
-   }
-   } );
-
-   return preq( request );
-}
-
-/**
- * Sets up the request templates for MW and RESTBase API requests
- *
- * @param {Application} app the application object
- */
-function setupApiTemplates( app ) {
-   /* eslint-disable camelcase */
-   // set up the MW API request template
-   if ( !app.conf.mwapi_req ) {
-   app.conf.mwapi_req = {
-   uri: 'https://{{domain}}/w/api.php',
-   headers: {
-   'user-agent': '{{user-agent}}'
-   },
-   query: '{{ default(request.query, {}) }}',
-   body: '{{request.body}}'
-   };
-   }
-   app.mwapi_tpl = new Template( app.conf.mwapi_req );
-
-   // set up the RESTBase request template
-   if ( !app.conf.restbase_req ) {
-   app.conf.restbase_req = {
-   method: '{{request.method}}',
-   uri: 'http://{{domain}}/api/rest_v1/{+path}',
-   query: '{{ default(request.query, {}) }}',
-   headers: '{{request.headers}}',
-   body: '{{request.body}}'
-   };
-   }
-   app.restbase_tpl = new Template( app.conf.restbase_req );
-   /* eslint-enable camelcase */
-}