diff --git a/lib/rest/axios.js b/lib/rest/axios.js index a38901d..af70693 100644 --- a/lib/rest/axios.js +++ b/lib/rest/axios.js @@ -69,45 +69,49 @@ export function Axios(config) { } return new Promise((resolve, reject) => { - axios(options, (error, response, body) => { - if (error) { - reject(error); - return; - } + // axios(options, (error, response, body) => { + axios(options).then(response => { + // if (error) { + // reject(error); + // return; + // } if(isVoiceReq === true){ - if (response.statusCode >= 500){ + if (response.status >= 500){ options.url = apiVoiceUris[1] + config.authId + '/' + action; if (method === 'GET' && options.formData !== '') { let query = '?' + queryString.stringify(params); options.url += query; } - axios(options,(error, response) => { - if(error){ - reject(error); - return; - } - if(response.statusCode>=500){ + // axios(options,(error, response) => { + axios(options).then(response => { + // if(error){ + // reject(error); + // return; + // } + if(response.status>=500){ options.url = apiVoiceUris[2] + config.authId + '/' + action; if (method === 'GET' && options.formData !== '') { let query = '?' + queryString.stringify(params); options.url += query; } - axios(options, (error, response) => { - if(error){ - reject(error); - return; - } + // axios(options, (error, response) => { + axios(options).then(response => { + // if(error){ + // reject(error); + // return; + // } const exceptionClass = { 400: Exceptions.InvalidRequestError, 401: Exceptions.AuthenticationError, 404: Exceptions.ResourceNotFoundError, 405: Exceptions.InvalidRequestError, 500: Exceptions.ServerError, - } [response.statusCode] || Error; + } [response.status] || Error; - if (!_.inRange(response.statusCode, 200, 300)) { - body = body || response.body; + if (!_.inRange(response.status, 200, 300)) { + // body = body || response.body; + let body = response.data; if (typeof body === 'object') { reject(new exceptionClass(JSON.stringify(body))); } else { @@ -115,10 +119,11 @@ export function Axios(config) { } } // else { - let body = response.body; + // let body = response.body; + let body = response.data; let isObj = typeof _body === 'object' && _body !== null && !(_body instanceof Array) && !(_body instanceof Date) if (isObj) { - _body['statusCode'] = response.statusCode; + _body['statusCode'] = response.status; } resolve({ response: response, @@ -134,10 +139,11 @@ export function Axios(config) { 404: Exceptions.ResourceNotFoundError, 405: Exceptions.InvalidRequestError, 500: Exceptions.ServerError, - } [response.statusCode] || Error; + } [response.status] || Error; - if (!_.inRange(response.statusCode, 200, 300)) { - body = body || response.body; + if (!_.inRange(response.status, 200, 300)) { + // body = body || response.body; + let body = response.data; if (typeof body === 'object') { reject(new exceptionClass(JSON.stringify(body))); } else { @@ -145,10 +151,11 @@ export function Axios(config) { } } // else { - let body = response.body; + // let body = response.body; + let body = response.data; let isObj = typeof _body === 'object' && _body !== null && !(_body instanceof Array) && !(_body instanceof Date) if (isObj) { - _body['statusCode'] = response.statusCode; + _body['statusCode'] = response.status; } resolve({ response: response, @@ -164,10 +171,11 @@ export function Axios(config) { 404: Exceptions.ResourceNotFoundError, 405: Exceptions.InvalidRequestError, 500: Exceptions.ServerError, - } [response.statusCode] || Error; + } [response.status] || Error; - if (!_.inRange(response.statusCode, 200, 300)) { - body = body || response.body; + if (!_.inRange(response.status, 200, 300)) { + // body = body || response.body; + let body = response.data; if (typeof body === 'object') { reject(new exceptionClass(JSON.stringify(body))); } else { @@ -175,10 +183,11 @@ export function Axios(config) { } } // else { - let body = response.body; + // let body = response.body; + let body = response.data; let isObj = typeof _body === 'object' && _body !== null && !(_body instanceof Array) && !(_body instanceof Date) if (isObj) { - _body['statusCode'] = response.statusCode; + _body['statusCode'] = response.status; } resolve({ response: response, @@ -194,10 +203,11 @@ export function Axios(config) { 404: Exceptions.ResourceNotFoundError, 405: Exceptions.InvalidRequestError, 500: Exceptions.ServerError, - } [response.statusCode] || Error; + } [response.status] || Error; - if (!_.inRange(response.statusCode, 200, 300)) { - body = body || response.body; + if (!_.inRange(response.status, 200, 300)) { + // body = body || response.body; + let body = response.data; if (typeof body === 'object') { reject(new exceptionClass(JSON.stringify(body))); } else { @@ -205,10 +215,11 @@ export function Axios(config) { } } else { - let body = response.body; + // let body = response.body; + let body = response.data; let isObj = typeof _body === 'object' && _body !== null && !(_body instanceof Array) && !(_body instanceof Date) if (isObj) { - _body['statusCode'] = response.statusCode; + _body['statusCode'] = response.status; } resolve({ response: response, @@ -216,7 +227,12 @@ export function Axios(config) { }); } } - }); + }) + .catch(function (error) { + // handle error + // console.log(error); + reject(error); + }) }); }; } diff --git a/lib/rest/client.js b/lib/rest/client.js index 3dac6fe..69225eb 100644 --- a/lib/rest/client.js +++ b/lib/rest/client.js @@ -1,6 +1,5 @@ // import { Request } from "./request.js"; -// import { Axios } from "./axios.js"; -import { Got } from "./got.js"; +import { Axios } from "./axios.js"; import { camelCaseRequestWrapper } from "./utils"; import { name, version } from "../../package.json"; import { Phlo, PhloInterface } from "../resources/phlo"; @@ -68,8 +67,8 @@ export class Client { }, options ); - console.log(options,"12311122222222222") - let client = camelCaseRequestWrapper(Got(options)); + let client = camelCaseRequestWrapper(Axios(options)); + // let client = camelCaseRequestWrapper(Request(options)); this.calls = new CallInterface(client); this.accounts = new AccountInterface(client); @@ -127,7 +126,8 @@ export class PhloClient { options ); - let client = camelCaseRequestWrapper(Got(options)); + let client = camelCaseRequestWrapper(Axios(options)); + // let client = camelCaseRequestWrapper(Request(options)); this.phlo = function(phloId) { let dd = new Phlo(client, { phloId: phloId, authId: authId });