Compare View
Commits (3)
Changes
Showing 4 changed files Side-by-side Diff
coreapi/index.js
... | ... | @@ -0,0 +1,58 @@ |
1 | +'use strict'; | |
2 | + | |
3 | +const request = require('request'); | |
4 | + | |
5 | +const logger = require('../logger'); | |
6 | +const coreUrl = require('../core-url'); | |
7 | + | |
8 | +logger.verbose(`CORE URL: ${coreUrl}`); | |
9 | + | |
10 | +function doRequest(params, cb) { | |
11 | + return new Promise((resolve) => { | |
12 | + const options = { | |
13 | + url: `${coreUrl}/${params.path.replace(/^\/+/, '')}`, | |
14 | + method: params.method || 'GET', | |
15 | + qs: params.qs || null, | |
16 | + }; | |
17 | + | |
18 | + logger.verbose('Requesting to CORE', { | |
19 | + xid: params.xid, method: options.method, fullpath: options.url, qs: options.qs, | |
20 | + }); | |
21 | + | |
22 | + request(options, (err, res, body) => { | |
23 | + if (err) { | |
24 | + logger.warn(`COREAPI: Error doing HTTP ${options.method} to CORE. ${err.toString()}`, { xid: params.xid }); | |
25 | + | |
26 | + resolve(err); | |
27 | + if (typeof cb === 'function') cb(err); | |
28 | + return; | |
29 | + } | |
30 | + | |
31 | + if (res.statusCode !== 200) { | |
32 | + const errStatusCode = new Error('COREAPI: CORE responded with non HTTP STATUS CODE 200'); | |
33 | + logger.warn(`COREAPI: CORE returning HTTP STATUS CODE ${res.statusCode}, not 200`, { xid: params.xid, body }); | |
34 | + | |
35 | + resolve(errStatusCode); | |
36 | + if (typeof cb === 'function') cb(errStatusCode); | |
37 | + return; | |
38 | + } | |
39 | + | |
40 | + let bodyObject; | |
41 | + try { | |
42 | + bodyObject = JSON.parse(body); | |
43 | + } catch (e) { | |
44 | + const errNoJson = new Error('COREAPI: CORE responded with non JSON body'); | |
45 | + logger.verbose(errNoJson); | |
46 | + | |
47 | + resolve(errNoJson, body); | |
48 | + if (typeof cb === 'function') cb(errNoJson, body); | |
49 | + return; | |
50 | + } | |
51 | + | |
52 | + resolve(null, bodyObject); | |
53 | + if (typeof cb === 'function') cb(null, bodyObject); | |
54 | + }); | |
55 | + }); | |
56 | +} | |
57 | + | |
58 | +module.exports = doRequest; |
gateway/pull.js
... | ... | @@ -329,7 +329,7 @@ function report(data) { |
329 | 329 | logger.verbose('Report to CORE using HTTP POST'); |
330 | 330 | } |
331 | 331 | |
332 | - request.post(options, function(error, response, body) { | |
332 | + request.post(options, function(error, response) { | |
333 | 333 | if (error) { |
334 | 334 | logger.warn('Error reporting to CORE', {error: error}); |
335 | 335 | resendReport(data); |
package-lock.json