From 681457d352d641f9f8be42bac7bfa02fd80df307 Mon Sep 17 00:00:00 2001 From: Koushik-Ayila Date: Mon, 3 Aug 2020 11:15:12 +0530 Subject: [PATCH] Fixed bugs --- lib/resources/conferences.js | 2 +- lib/rest/request.js | 159 ++++++++++++++++++++++++----------- 2 files changed, 109 insertions(+), 52 deletions(-) diff --git a/lib/resources/conferences.js b/lib/resources/conferences.js index 285a13b..1073ba7 100644 --- a/lib/resources/conferences.js +++ b/lib/resources/conferences.js @@ -277,7 +277,7 @@ export class Conference extends PlivoResource { * @promise {PlivoGenericResponse} return PlivoGenericResponse if success * @fail {Error} return Error */ - startRecording(params) { + startRecording(params={}) { params.isVoiceRequest = 'true'; return super.executeAction(this.id + '/Record/', 'POST', params); } diff --git a/lib/rest/request.js b/lib/rest/request.js index 33e2194..45ccf87 100644 --- a/lib/rest/request.js +++ b/lib/rest/request.js @@ -36,12 +36,12 @@ export function Request(config) { let isVoiceReq = false; if (params) { if (params.hasOwnProperty('is_call_insights_request')) { - options.url = params.call_insights_base_url + params.call_insights_request_path; - delete params.is_call_insights_request; - delete params.call_insights_base_url; - delete params.call_insights_request_path; - delete options.formData; - options.json = params; + options.url = params.call_insights_base_url + params.call_insights_request_path; + delete params.is_call_insights_request; + delete params.call_insights_base_url; + delete params.call_insights_request_path; + delete options.formData; + options.json = params; } else if (params.hasOwnProperty('is_voice_request')){ options.url = apiVoiceUris[0] + config.authId + '/' + action; @@ -69,33 +69,38 @@ export function Request(config) { reject(error); return; } - if (response.statusCode >= 500 && isVoiceReq === true){ - options.url = apiVoiceUris[1] + config.authId + '/' + action; - if (method === 'GET' && options.formData !== '') { - let query = '?' + queryString.stringify(params); - options.url += query; - } - request(options,(error, response) => { - if(error){ - reject(error); - return; + + if(isVoiceReq === true){ + if (response.statusCode >= 500){ + options.url = apiVoiceUris[1] + config.authId + '/' + action; + if (method === 'GET' && options.formData !== '') { + let query = '?' + queryString.stringify(params); + options.url += query; } - if(response.statusCode >= 500 && isVoiceReq === true){ - options.url = apiVoiceUris[2] + config.authId + '/' + action; - if (method === 'GET' && options.formData !== '') { - let query = '?' + queryString.stringify(params); - options.url += query; + request(options,(error, response) => { + if(error){ + reject(error); + return; } - request(options, (error, response, body) => { - if(error){ - reject(error); - return; + if(response.statusCode>=500){ + options.url = apiVoiceUris[2] + config.authId + '/' + action; + if (method === 'GET' && options.formData !== '') { + let query = '?' + queryString.stringify(params); + options.url += query; } - if(response.statusCode >= 500 && isVoiceReq === true){ + request(options, (error, 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; + if (!_.inRange(response.statusCode, 200, 300)) { body = body || response.body; if (typeof body === 'object') { @@ -104,46 +109,97 @@ export function Request(config) { reject(new exceptionClass(body)); } } - else { - let body = response.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; + } resolve({ response: response, body: body }); - } + // } + }) + } + else { + const exceptionClass = { + 400: Exceptions.InvalidRequestError, + 401: Exceptions.AuthenticationError, + 404: Exceptions.ResourceNotFoundError, + 405: Exceptions.InvalidRequestError, + 500: Exceptions.ServerError, + } [response.statusCode] || Error; + + if (!_.inRange(response.statusCode, 200, 300)) { + body = body || response.body; + 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; } - let bodyOfTwoRetry = response.body resolve({ response: response, - body: bodyOfTwoRetry + body: body }); - }) + } + }) + } + else { + const exceptionClass = { + 400: Exceptions.InvalidRequestError, + 401: Exceptions.AuthenticationError, + 404: Exceptions.ResourceNotFoundError, + 405: Exceptions.InvalidRequestError, + 500: Exceptions.ServerError, + } [response.statusCode] || Error; + + if (!_.inRange(response.statusCode, 200, 300)) { + body = body || response.body; + 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; } - let bodyOfOneRetry = response.body resolve({ response: response, - body: bodyOfOneRetry + body: body }); - }); - } - - const exceptionClass = { - 400: Exceptions.InvalidRequestError, - 401: Exceptions.AuthenticationError, - 404: Exceptions.ResourceNotFoundError, - 405: Exceptions.InvalidRequestError, - 500: Exceptions.ServerError, - } [response.statusCode] || Error; - - if (!_.inRange(response.statusCode, 200, 300)) { - body = body || response.body; - if (typeof body === 'object') { - reject(new exceptionClass(JSON.stringify(body))); - } else { - reject(new exceptionClass(body)); } } + else { + const exceptionClass = { + 400: Exceptions.InvalidRequestError, + 401: Exceptions.AuthenticationError, + 404: Exceptions.ResourceNotFoundError, + 405: Exceptions.InvalidRequestError, + 500: Exceptions.ServerError, + } [response.statusCode] || Error; + + if (!_.inRange(response.statusCode, 200, 300)) { + body = body || response.body; + 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) { @@ -154,6 +210,7 @@ export function Request(config) { body: body }); } + } }); }); };