diff --git a/lib/rest/request.js b/lib/rest/request.js index d0afd8c..2189b04 100644 --- a/lib/rest/request.js +++ b/lib/rest/request.js @@ -67,13 +67,79 @@ export function Request(config) { options.timeout = config.timeout; } + function voiceRequestFunc() { + return new Promise((resolve, reject) => { + request(options, (error, response, body) => { + 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; + + // if (response.statusCode >= 200 && isVoiceReq === true){ + // voiceRetryCount++; + // if (voiceRetryCount === 1){ + // options.url = apiVoiceUris[1] + config.authId + '/' + action; + // console.log(voiceRetryCount,options.url) + // } + // else if (voiceRetryCount === 2){ + // options.url = apiVoiceUris[2] + config.authId + '/' + action; + // console.log(voiceRetryCount,options.url) + // } + // else if (voiceRetryCount > 2){ + // resolve({ + // response: response, + // body: body + // }); + // } + // isVoiceReq = true; + // request(options); + // } + + if (!_.inRange(response.statusCode, 200, 300)) { + body = body || response.body; + // voiceRetryCount = 0; + if (typeof body === 'object') { + reject(new exceptionClass(JSON.stringify(body))); + } else { + reject(new exceptionClass(body)); + } + } + else { + let body = response.body; + let isObj = typeof _body === 'object' && _body !== null && !(_body instanceof Array) && !(_body instanceof Date) + if (isObj) { + _body['statusCode'] = response.statusCode; + } + // voiceRetryCount = 0; + resolve({ + response: response, + body: body + }); + } + }); + }); + } + + if (isVoiceReq === true){ + var req = voiceRequestFunc(); + if (req.status >= 200){ + console.log(1) + } + } + else { return new Promise((resolve, reject) => { request(options, (error, response, body) => { if (error) { reject(error); return; } - console.log(3) const exceptionClass = { 400: Exceptions.InvalidRequestError, 401: Exceptions.AuthenticationError, @@ -81,31 +147,8 @@ export function Request(config) { 405: Exceptions.InvalidRequestError, 500: Exceptions.ServerError, } [response.statusCode] || Error; - - if (response.statusCode >= 200 && isVoiceReq === true){ - voiceRetryCount++; - if (voiceRetryCount === 1){ - options.url = apiVoiceUris[1] + config.authId + '/' + action; - console.log(voiceRetryCount,options.url) - } - else if (voiceRetryCount === 2){ - options.url = apiVoiceUris[2] + config.authId + '/' + action; - console.log(voiceRetryCount,options.url) - } - else if (voiceRetryCount > 2){ - resolve({ - response: response, - body: body - }); - } - isVoiceReq = true; - request(options); - } - if (!_.inRange(response.statusCode, 200, 300)) { - console.log(5) body = body || response.body; - voiceRetryCount = 0; if (typeof body === 'object') { reject(new exceptionClass(JSON.stringify(body))); } else { @@ -113,13 +156,11 @@ export function Request(config) { } } else { - console.log(4) let body = response.body; let isObj = typeof _body === 'object' && _body !== null && !(_body instanceof Array) && !(_body instanceof Date) if (isObj) { _body['statusCode'] = response.statusCode; } - voiceRetryCount = 0; resolve({ response: response, body: body @@ -127,5 +168,6 @@ export function Request(config) { } }); }); + } }; }