11'use strict' ;
22
3- const preq = require ( 'preq ' ) ;
3+ const BBPromise = require ( 'bluebird ' ) ;
44const sUtil = require ( './util' ) ;
55const Template = require ( 'swagger-router' ) . Template ;
66const HTTPError = sUtil . HTTPError ;
77
88/**
99 * Calls the MW API with the supplied query as its body
10- * @param {!Object } app the application object
11- * @param {string } domain the domain to issue the request to
10+ * @param {!Object } req the incoming request object
1211 * @param {?Object } query an object with all the query parameters for the MW API
12+ * @param {?Object } headers additional headers to pass to the MW API
1313 * @return {!Promise } a promise resolving as the response object from the MW API
1414 */
15- function mwApiGet ( app , domain , query ) {
15+ function mwApiGet ( req , query , headers ) {
1616
17+ const app = req . app ;
1718 query = Object . assign ( {
1819 format : 'json' ,
1920 formatversion : 2
2021 } , query ) ;
2122
2223 const request = app . mwapi_tpl . expand ( {
2324 request : {
24- params : { domain } ,
25- headers : { 'user-agent' : app . conf . user_agent } ,
25+ params : { domain : req . params . domain } ,
26+ headers : req . headers ,
2627 query
2728 }
2829 } ) ;
30+ Object . assign ( request . headers , headers ) ;
2931
30- return preq ( request ) . then ( ( response ) => {
32+ return req . issueRequest ( request ) . then ( ( response ) => {
3133 if ( response . status < 200 || response . status > 399 ) {
3234 // there was an error when calling the upstream service, propagate that
33- throw new HTTPError ( {
35+ return BBPromise . reject ( new HTTPError ( {
3436 status : response . status ,
3537 type : 'api_error' ,
3638 title : 'MW API error' ,
3739 detail : response . body
38- } ) ;
40+ } ) ) ;
3941 }
4042 return response ;
4143 } ) ;
@@ -44,9 +46,8 @@ function mwApiGet(app, domain, query) {
4446
4547/**
4648 * Calls the REST API with the supplied domain, path and request parameters
47- * @param {!Object } app the application object
48- * @param {string } domain the domain to issue the request for
49- * @param {!string } path the REST API path to contact without the leading slash
49+ * @param {!Object } req the incoming request object
50+ * @param {?string } path the REST API path to contact without the leading slash
5051 * @param {?Object } [restReq={}] the object containing the REST request details
5152 * @param {?string } [restReq.method=get] the request method
5253 * @param {?Object } [restReq.query={}] the query string to send, if any
@@ -55,22 +56,32 @@ function mwApiGet(app, domain, query) {
5556 * @return {!Promise } a promise resolving as the response object from the REST API
5657 *
5758 */
58- function restApiGet ( app , domain , path , restReq ) {
59+ function restApiGet ( req , path , restReq ) {
5960
61+ const app = req . app ;
62+ if ( path . constructor === Object ) {
63+ restReq = path ;
64+ path = undefined ;
65+ }
6066 restReq = restReq || { } ;
61- path = path [ 0 ] === '/' ? path . slice ( 1 ) : path ;
62-
63- const request = app . restbase_tpl . expand ( {
64- request : {
65- method : restReq . method ,
66- params : { domain, path } ,
67- query : restReq . query ,
68- headers : Object . assign ( { 'user-agent' : app . conf . user_agent } , restReq . headers ) ,
69- body : restReq . body
70- }
71- } ) ;
67+ restReq . method = restReq . method || 'get' ;
68+ restReq . query = restReq . query || { } ;
69+ restReq . headers = restReq . headers || { } ;
70+ restReq . params = restReq . params || { } ;
71+ restReq . params . path = path || restReq . params . path ;
72+ restReq . params . domain = restReq . params . domain || req . params . domain ;
73+ if ( ! restReq . params . path || ! restReq . params . domain ) {
74+ return BBPromise . reject ( new HTTPError ( {
75+ status : 500 ,
76+ type : 'internal_error' ,
77+ title : 'Invalid internal call' ,
78+ detail : 'domain and path need to be defined for the REST API call'
79+ } ) ) ;
80+ }
81+ restReq . params . path = restReq . params . path [ 0 ] === '/' ?
82+ restReq . params . path . slice ( 1 ) : restReq . params . path ;
7283
73- return preq ( request ) ;
84+ return req . issueRequest ( app . restbase_tpl . expand ( { request : restReq } ) ) ;
7485
7586}
7687
@@ -83,10 +94,9 @@ function setupApiTemplates(app) {
8394 // set up the MW API request template
8495 if ( ! app . conf . mwapi_req ) {
8596 app . conf . mwapi_req = {
97+ method : 'post' ,
8698 uri : 'http://{{domain}}/w/api.php' ,
87- headers : {
88- 'user-agent' : '{{user-agent}}'
89- } ,
99+ headers : '{{request.headers}}' ,
90100 body : '{{ default(request.query, {}) }}'
91101 } ;
92102 }
0 commit comments