|
|
const _ = require('lodash');
|
|
|
const Context = require('./context');
|
|
|
const request = require('request-promise');
|
|
|
const rp = require('request-promise');
|
|
|
const request = require('request');
|
|
|
|
|
|
const logger = global.yoho.logger;
|
|
|
|
|
|
const defaultOptions = {
|
|
|
resolveWithFullResponse: true
|
|
|
};
|
|
|
|
|
|
|
|
|
const API_INTERNAL_ERROR = {
|
|
|
code: 500,
|
...
|
...
|
@@ -44,16 +44,35 @@ class Api extends Context { |
|
|
headers: Object.assign(defaultHeader, headers)
|
|
|
});
|
|
|
}
|
|
|
download() {
|
|
|
proxy(url, data, options) {
|
|
|
let params = _.merge({
|
|
|
url,
|
|
|
headers: {
|
|
|
'Content-Type': 'application/json'
|
|
|
}
|
|
|
}, options);
|
|
|
|
|
|
if (options.method === 'get') {
|
|
|
params.qs = data;
|
|
|
} else {
|
|
|
params.body = JSON.stringify(data);
|
|
|
}
|
|
|
_request(options) {
|
|
|
options = Object.assign(options, defaultOptions);
|
|
|
|
|
|
return request[options.method || 'get'](options).then(response => {
|
|
|
return request(params, (error, response) => {
|
|
|
if (!error) {
|
|
|
logger.info(`api call ${response.statusCode} [${response.request.method}] ${response.request.href} ${response.request.body}`); // eslint-disable-line
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
_request(options) {
|
|
|
options = Object.assign(options, {
|
|
|
resolveWithFullResponse: true
|
|
|
});
|
|
|
return rp[options.method || 'get'](options).then(response => {
|
|
|
let jsonBody = JSON.parse(response.body);
|
|
|
let req = response.request;
|
|
|
|
|
|
logger.info(`api call ${req.statusCode} [${req.method}] ${req.uri.href} ${req.body || ''}`);
|
|
|
logger.info(`api call ${response.statusCode} [${req.method}] ${req.uri.href} ${req.body || ''}`);
|
|
|
return jsonBody;
|
|
|
}).catch(err => {
|
|
|
logger.error(`api call ${err.statusCode} [${err.options.method}]
|
...
|
...
|
|