|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import { request as HttpRequest } from 'http'; |
| 4 | +import { EndpointDefinition, StatusAnnouncement } from '../../core/flow_interfaces'; |
| 5 | +import { buildUrl } from '../utils'; |
| 6 | + |
| 7 | +function log(url, qs, res) { |
| 8 | + let _pickLogger = () => { |
| 9 | + if (console && console.log) return console; // eslint-disable-line no-console |
| 10 | + return console; |
| 11 | + }; |
| 12 | + |
| 13 | + let start = new Date().getTime(); |
| 14 | + let timestamp = new Date().toISOString(); |
| 15 | + let logger = _pickLogger(); |
| 16 | + logger.log('<<<<<'); // eslint-disable-line no-console |
| 17 | + logger.log(`[${timestamp}]`, '\n', url, '\n', qs); // eslint-disable-line no-console |
| 18 | + logger.log('-----'); // eslint-disable-line no-console |
| 19 | + |
| 20 | + let now = new Date().getTime(); |
| 21 | + let elapsed = now - start; |
| 22 | + let timestampDone = new Date().toISOString(); |
| 23 | + |
| 24 | + logger.log('>>>>>>'); // eslint-disable-line no-console |
| 25 | + logger.log(`[${timestampDone} / ${elapsed}]`, '\n', url, '\n', qs, '\n', res); // eslint-disable-line no-console |
| 26 | + logger.log('-----'); // eslint-disable-line no-console |
| 27 | +} |
| 28 | + |
| 29 | +function xdr(method: string, url: string, params: Object, body: string, endpoint: EndpointDefinition, callback: Function): void { |
| 30 | + let status: StatusAnnouncement = {}; |
| 31 | + status.operation = endpoint.operation; |
| 32 | + |
| 33 | + // $FlowFixMe |
| 34 | + return HttpRequest({ |
| 35 | + method, |
| 36 | + url: buildUrl(url, params), |
| 37 | + content: body, |
| 38 | + timeout: endpoint.timeout |
| 39 | + }).then((response) => { |
| 40 | + status.error = false; |
| 41 | + |
| 42 | + if (response.statusCode) { |
| 43 | + status.statusCode = response.statusCode; |
| 44 | + } |
| 45 | + |
| 46 | + return response.content.toJSON(); |
| 47 | + }).then((response) => { |
| 48 | + let resp = response; |
| 49 | + |
| 50 | + if (this._config.logVerbosity) { |
| 51 | + log(url, params, resp); |
| 52 | + } |
| 53 | + |
| 54 | + callback(status, resp); |
| 55 | + }).catch((e) => { |
| 56 | + status.error = true; |
| 57 | + status.errorData = e; |
| 58 | + status.category = this._detectErrorCategory(e); |
| 59 | + callback(status, null); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +export function get(params: Object, endpoint: EndpointDefinition, callback: Function) { |
| 64 | + let url = this.getStandardOrigin() + endpoint.url; |
| 65 | + return xdr.call(this, 'GET', url, params, '', endpoint, callback); |
| 66 | +} |
| 67 | + |
| 68 | +export function post(params: Object, body: string, endpoint: EndpointDefinition, callback: Function) { |
| 69 | + let url = this.getStandardOrigin() + endpoint.url; |
| 70 | + return xdr.call(this, 'POST', url, params, body, endpoint, callback); |
| 71 | +} |
| 72 | + |
| 73 | +export function del(params: Object, endpoint: EndpointDefinition, callback: Function) { |
| 74 | + let url = this.getStandardOrigin() + endpoint.url; |
| 75 | + return xdr.call(this, 'DELETE', url, params, {}, endpoint, callback); |
| 76 | +} |
0 commit comments