From 06980d58b4b4c3b1e1d89f7498d78aaf3614c89d Mon Sep 17 00:00:00 2001 From: Koushik-Ayila Date: Mon, 13 Jul 2020 13:55:38 +0530 Subject: [PATCH 1/9] Node SDK retry - under development --- lib/resources/call.js | 7 +++++++ lib/rest/request.js | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/resources/call.js b/lib/resources/call.js index 8ea073d..0aa7b8c 100644 --- a/lib/resources/call.js +++ b/lib/resources/call.js @@ -6,6 +6,9 @@ import * as _ from "lodash"; const clientKey = Symbol(); const action = 'Call/'; const idField = 'callUuid'; +// const voiceBaseUri = 'https://api-qa.voice.plivodev.com/v1/Account/' +// const voiceFallback1 = 'https://api-qa-usw1.voice.plivodev.com/v1/Account/' +// const voiceFallback2 = 'https://api-qa-use1.voice.plivodev.com/v1/Account/' /** * Represents a Call @@ -289,6 +292,10 @@ export class CallInterface extends PlivoResourceInterface { params.from = from; params.to = _.isArray(to) ? _.join(to, '<') : to; params.answer_url = answerUrl; + // params.voiceBaseUri = voiceBaseUri; + // params.voiceFallback1 = voiceFallback1; + // params.voiceFallback2 = voiceFallback2; + params.isVoiceRequest = ""; return super.create(params); } diff --git a/lib/rest/request.js b/lib/rest/request.js index 2bb23fc..d0afd8c 100644 --- a/lib/rest/request.js +++ b/lib/rest/request.js @@ -32,7 +32,9 @@ export function Request(config) { headers: headers, json: true }; - + var apiVoiceUris = ['https://api-qa.voice.plivodev.com/v1/Account/','https://api-qa-usw1.voice.plivodev.com/v1/Account/','https://api-qa-use1.voice.plivodev.com/v1/Account/']; + var isVoiceReq = false; + var voiceRetryCount = 0; if (params) { if (params.hasOwnProperty('is_call_insights_request')) { options.url = params.call_insights_base_url + params.call_insights_request_path; @@ -42,6 +44,14 @@ export function Request(config) { delete options.formData; options.json = params; } + else if (params.hasOwnProperty('is_voice_request')){ + options.url = apiVoiceUris[0] + config.authId + '/' + action; + delete params.isVoiceRequest; + isVoiceReq = true; + + // console.log(options.url); + } + // console.log(apiVoiceUris); } if (method === 'GET' && options.formData !== '') { @@ -63,7 +73,7 @@ export function Request(config) { reject(error); return; } - + console.log(3) const exceptionClass = { 400: Exceptions.InvalidRequestError, 401: Exceptions.AuthenticationError, @@ -72,19 +82,44 @@ export function Request(config) { 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 { reject(new exceptionClass(body)); } - } else { + } + 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 From 65964657138a895653affa3ed7376366ab764844 Mon Sep 17 00:00:00 2001 From: Koushik-Ayila Date: Tue, 14 Jul 2020 11:43:36 +0530 Subject: [PATCH 2/9] Node SDK retry - under development --- lib/rest/request.js | 94 ++++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 26 deletions(-) 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) { } }); }); + } }; } From 306022a280b1e26b86283a3d422bd1dbecf22c8a Mon Sep 17 00:00:00 2001 From: Koushik-Ayila Date: Tue, 14 Jul 2020 13:36:02 +0530 Subject: [PATCH 3/9] Voice Retry Logic - Under Development --- lib/rest/request.js | 157 ++++++++++++++++++++++++++------------------ 1 file changed, 93 insertions(+), 64 deletions(-) diff --git a/lib/rest/request.js b/lib/rest/request.js index 2189b04..e9c7290 100644 --- a/lib/rest/request.js +++ b/lib/rest/request.js @@ -67,71 +67,40 @@ 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) - } + voiceRequestFunc(options).then(function (response) { + var noRetry = response.response + if(noRetry.statusCode >= 500 ){ + voiceRetryCount++; + if(voiceRetryCount === 1){ + options.url = apiVoiceUris[1] + config.authId + '/' + action; + } + isVoiceReq = true; + voiceRequestFunc(options).then(function (response) { + var firstRetry = response.response; + if(firstRetry.statusCode >= 500 ) { + voiceRetryCount++; + if (voiceRetryCount === 2) { + options.url = apiVoiceUris[2] + config.authId + '/' + action; + } + isVoiceReq = true; + voiceRequestFunc(options).then(function (response) { + var secondRetry = response.response; + if(secondRetry.statusCode >= 500){ + console.log("d"); + return response; + } + console.log(secondRetry.statusCode,"c") + return response; + }) + } + console.log(firstRetry.statusCode, "b") + return response; + }) + } + console.log(noRetry.statusCode, "a") + return response; + }) } else { return new Promise((resolve, reject) => { @@ -171,3 +140,63 @@ export function Request(config) { } }; } + +function voiceRequestFunc(options) { + 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 + }); + } + }); + }); +} From d989ffe836b239528c32c7f13c193e5733525fbd Mon Sep 17 00:00:00 2001 From: Koushik-Ayila Date: Wed, 15 Jul 2020 15:34:49 +0530 Subject: [PATCH 4/9] Changed base url and added retry logic for voice requests --- lib/resources/applications.js | 8 +- lib/resources/call.js | 42 +++++---- lib/resources/conferences.js | 61 +++++++++---- lib/resources/endpoints.js | 11 ++- lib/resources/recordings.js | 8 +- lib/rest/request.js | 165 +++++++++++++--------------------- 6 files changed, 153 insertions(+), 142 deletions(-) diff --git a/lib/resources/applications.js b/lib/resources/applications.js index e340aee..ff0d99d 100644 --- a/lib/resources/applications.js +++ b/lib/resources/applications.js @@ -43,6 +43,7 @@ export class Application extends PlivoResource { * @fail {Error} return Error */ update(params) { + params.isVoiceRequest = 'true'; return super.update(params); } @@ -59,6 +60,7 @@ export class Application extends PlivoResource { if (typeof params.cascade === 'boolean') { params.cascade = params.cascade.toString(); } + params.isVoiceRequest = 'true'; return super.delete(params); } @@ -86,7 +88,9 @@ export class ApplicationInterface extends PlivoResourceInterface { * @fail {Error} return Error */ get(id) { - return super.get(id); + let params = {} + params.isVoiceRequest = 'true' + return super.get(id, params); } /** @@ -122,7 +126,7 @@ export class ApplicationInterface extends PlivoResourceInterface { } params.app_name = appName; - + params.isVoiceRequest = 'true'; return super.create(params); } diff --git a/lib/resources/call.js b/lib/resources/call.js index 0aa7b8c..655c0ac 100644 --- a/lib/resources/call.js +++ b/lib/resources/call.js @@ -6,9 +6,6 @@ import * as _ from "lodash"; const clientKey = Symbol(); const action = 'Call/'; const idField = 'callUuid'; -// const voiceBaseUri = 'https://api-qa.voice.plivodev.com/v1/Account/' -// const voiceFallback1 = 'https://api-qa-usw1.voice.plivodev.com/v1/Account/' -// const voiceFallback2 = 'https://api-qa-use1.voice.plivodev.com/v1/Account/' /** * Represents a Call @@ -35,7 +32,9 @@ export class Call extends PlivoResource { * @fail {Error} return Error */ hangup() { - return super.delete(); + let params = {} + params.isVoiceRequest = 'true'; + return super.delete(params); } /** @@ -51,6 +50,7 @@ export class Call extends PlivoResource { * @fail {Error} return Error */ transfer(params) { + params.isVoiceRequest = 'true'; return super.update(params); } /** @@ -72,6 +72,7 @@ export class Call extends PlivoResource { * @fail {Error} return Error */ startRecording(params) { + params.isVoiceRequest = 'true'; return super.executeAction(this.id + '/Record/', 'POST', params); } /** @@ -82,6 +83,7 @@ export class Call extends PlivoResource { * @fail {Error} return Error */ stopRecording(params) { + params.isVoiceRequest = 'true'; return super.executeAction(this.id + '/Record/', 'DELETE', params); } @@ -107,6 +109,7 @@ export class Call extends PlivoResource { startPlayingMusic(urls, optionalParams) { let params = optionalParams || {}; params.urls = urls; + params.isVoiceRequest = 'true'; let errors = validate([ {field: 'urls', value: urls, validators: ['isRequired', 'isString']} @@ -125,7 +128,9 @@ export class Call extends PlivoResource { * @fail {Error} returns Error */ stopPlayingMusic() { - return super.executeAction(this.id + '/Play/', 'DELETE'); + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Play/', 'DELETE', params); } /** @@ -157,6 +162,7 @@ export class Call extends PlivoResource { let params = optionalParams || {}; params.text = text; + params.isVoiceRequest = 'true'; return super.executeAction(this.id + '/Speak/', 'POST', params); } @@ -168,7 +174,9 @@ export class Call extends PlivoResource { * @fail {Error} returns Error */ stopSpeakingText() { - return super.executeAction(this.id + '/Speak/', 'DELETE'); + let params = {} + params.isVoiceRequest = 'true'; + return super.executeAction(this.id + '/Speak/', 'DELETE', params); } /** @@ -188,7 +196,7 @@ export class Call extends PlivoResource { let params = optionalParams || {}; params.digits = digits; - + params.isVoiceRequest = 'true'; return super.executeAction(this.id + '/DTMF/', 'POST', params); } @@ -199,7 +207,9 @@ export class Call extends PlivoResource { * @fail {Error} returns Error */ cancel() { - return super.executeAction('Request/' + this.id + '/', 'DELETE', {}, ''); + let params = {}; + params.isVoiceRequest = 'true'; + return super.executeAction('Request/' + this.id + '/', 'DELETE', params, ''); } } @@ -236,7 +246,9 @@ export class CallInterface extends PlivoResourceInterface { if (errors) { return errors; } - return super.get(id); + let params = {} + params.isVoiceRequest = 'true'; + return super.get(id, params); } /** @@ -247,6 +259,7 @@ export class CallInterface extends PlivoResourceInterface { * @fail {Error} returns Error */ list(params) { + params.isVoiceRequest = 'true'; return super.list(params); } @@ -292,10 +305,7 @@ export class CallInterface extends PlivoResourceInterface { params.from = from; params.to = _.isArray(to) ? _.join(to, '<') : to; params.answer_url = answerUrl; - // params.voiceBaseUri = voiceBaseUri; - // params.voiceFallback1 = voiceFallback1; - // params.voiceFallback2 = voiceFallback2; - params.isVoiceRequest = ""; + params.isVoiceRequest = 'true'; return super.create(params); } @@ -589,6 +599,7 @@ class LiveCallInterface extends PlivoResourceInterface { } return super.get(id, { status: 'live', + isVoiceRequest: 'true' }); } @@ -598,7 +609,7 @@ class LiveCallInterface extends PlivoResourceInterface { params = {} } params.status = 'live' - + params.isVoiceRequest = 'true' return new Promise((resolve, reject) => { client('GET', action, params) .then(response => { @@ -647,6 +658,7 @@ class QueuedCallInterface extends PlivoResourceInterface { } return super.get(id, { status: 'queued', + isVoiceRequest: 'true' }); } @@ -654,7 +666,7 @@ class QueuedCallInterface extends PlivoResourceInterface { let client = this[clientKey]; return new Promise((resolve, reject) => { - client('GET', action, {status: 'queued'}) + client('GET', action, {status: 'queued', isVoiceRequest: 'true'}) .then(response => { let calls = []; response.body.calls.forEach(callUuid => { diff --git a/lib/resources/conferences.js b/lib/resources/conferences.js index 1e506f3..285a13b 100644 --- a/lib/resources/conferences.js +++ b/lib/resources/conferences.js @@ -30,7 +30,9 @@ export class Conference extends PlivoResource { * @fail {Error} return Error */ hangup() { - return super.delete(); + let params = {} + params.isVoiceRequest = 'true' + return super.delete(params); } /** @@ -48,7 +50,9 @@ export class Conference extends PlivoResource { if (errors) { return errors; } - return super.executeAction(this.id + '/Member/' + memberId + '/', 'DELETE'); + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Member/' + memberId + '/', 'DELETE', params); } /** @@ -66,8 +70,9 @@ export class Conference extends PlivoResource { if (errors) { return errors; } - - return super.executeAction(this.id + '/Member/' + memberId + '/Kick/', 'POST'); + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Member/' + memberId + '/Kick/', 'POST',params); } /** @@ -85,7 +90,9 @@ export class Conference extends PlivoResource { if (errors) { return errors; } - return super.executeAction(this.id + '/Member/' + memberId + '/Mute/', 'POST'); + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Member/' + memberId + '/Mute/', 'POST',params); } /** @@ -103,8 +110,9 @@ export class Conference extends PlivoResource { if (errors) { return errors; } - - return super.executeAction(this.id + '/Member/' + memberId + '/Mute/', 'DELETE'); + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Member/' + memberId + '/Mute/', 'DELETE', params); } /** @@ -122,7 +130,9 @@ export class Conference extends PlivoResource { if (errors) { return errors; } - return super.executeAction(this.id + '/Member/' + memberId + '/Deaf/', 'POST'); + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Member/' + memberId + '/Deaf/', 'POST', params); } /** @@ -140,7 +150,9 @@ export class Conference extends PlivoResource { if (errors) { return errors; } - return super.executeAction(this.id + '/Member/' + memberId + '/Deaf/', 'DELETE'); + let params = {} + params.isVoiceRequest = 'true' + return super.executeAction(this.id + '/Member/' + memberId + '/Deaf/', 'DELETE', params); } /** @@ -161,6 +173,7 @@ export class Conference extends PlivoResource { return errors; } let params = {url: url}; + params.isVoiceRequest = 'true'; return super.executeAction(this.id + '/Member/' + memberId + '/Play/', 'POST', params); } @@ -179,7 +192,9 @@ export class Conference extends PlivoResource { if (errors) { return errors; } - return super.executeAction(this.id + '/Member/' + memberId + '/Play/', 'DELETE'); + let params = {}; + params.isVoiceRequest = 'true'; + return super.executeAction(this.id + '/Member/' + memberId + '/Play/', 'DELETE', params); } /** @@ -204,7 +219,7 @@ export class Conference extends PlivoResource { } let params = optionalParams || {}; params.text = text; - + params.isVoiceRequest = 'true'; return super.executeAction(this.id + '/Member/' + memberId + '/Speak/', 'POST', params); } @@ -223,6 +238,8 @@ export class Conference extends PlivoResource { if (errors) { return errors; } + let params = {}; + params.isVoiceRequest = 'true'; return super.executeAction(this.id + '/Member/' + memberId + '/Speak/', 'DELETE'); } @@ -261,6 +278,7 @@ export class Conference extends PlivoResource { * @fail {Error} return Error */ startRecording(params) { + params.isVoiceRequest = 'true'; return super.executeAction(this.id + '/Record/', 'POST', params); } @@ -271,7 +289,9 @@ export class Conference extends PlivoResource { * @fail {Error} return Error */ stopRecording() { - return super.executeAction(this.id + '/Record/', 'DELETE'); + let params = {}; + params.isVoiceRequest = 'true'; + return super.executeAction(this.id + '/Record/', 'DELETE', params); } } @@ -305,7 +325,9 @@ export class ConferenceInterface extends PlivoResourceInterface { if (errors) { return errors; } - return super.get(id); + let params = {}; + params.isVoiceRequest = 'true'; + return super.get(id, params); } /** @@ -316,9 +338,10 @@ export class ConferenceInterface extends PlivoResourceInterface { */ list() { let client = this[clientKey]; - + let params = {} + params.isVoiceRequest = 'true'; return new Promise((resolve, reject) => { - client('GET', action) + client('GET', action, params) .then(response => { let conferences = []; response.body.conferences.forEach(conference => { @@ -349,9 +372,11 @@ export class ConferenceInterface extends PlivoResourceInterface { if (errors) { return errors; } + let params = {}; + params.isVoiceRequest = 'true'; return new Conference(this[clientKey], { id: conferenceName - }).delete(); + }).delete(params); } /** @@ -361,8 +386,10 @@ export class ConferenceInterface extends PlivoResourceInterface { * @fail {Error} return Error */ hangupAll() { + let params = {}; + params.isVoiceRequest = 'true'; return new Conference(this[clientKey]) - .executeAction('', 'DELETE'); + .executeAction('', 'DELETE', params); } /** diff --git a/lib/resources/endpoints.js b/lib/resources/endpoints.js index d3103b2..b545840 100644 --- a/lib/resources/endpoints.js +++ b/lib/resources/endpoints.js @@ -36,6 +36,7 @@ export class Endpoint extends PlivoResource { * @fail {Error} return Error */ update(params) { + params.isVoiceRequest = 'true'; return super.update(params); } @@ -46,7 +47,9 @@ export class Endpoint extends PlivoResource { * @fail {Error} return Error */ delete() { - return super.delete(); + let params = {}; + params.isVoiceRequest = 'true'; + return super.delete(params); } } @@ -74,7 +77,9 @@ export class EndpointInterface extends PlivoResourceInterface { * @fail {Error} return Error */ get(id) { - return super.get(id); + let params = {}; + params.isVoiceRequest = 'true'; + return super.get(id, params); } /** @@ -106,7 +111,7 @@ export class EndpointInterface extends PlivoResourceInterface { if (appId) { params.app_id = appId; } - + params.isVoiceRequest = 'true'; return super.create(params); } diff --git a/lib/resources/recordings.js b/lib/resources/recordings.js index 8f615b3..df47d04 100644 --- a/lib/resources/recordings.js +++ b/lib/resources/recordings.js @@ -28,7 +28,9 @@ export class Recording extends PlivoResource { * @fail {Error} return Error */ delete() { - return super.delete(); + let params = {}; + params.isVoiceRequest = 'true'; + return super.delete(params); } } @@ -63,7 +65,9 @@ export class RecordingInterface extends PlivoResourceInterface { if (errors) { return errors; } - return super.get(id); + let params = {}; + params.isVoiceRequest = 'true'; + return super.get(id, params); } /** diff --git a/lib/rest/request.js b/lib/rest/request.js index e9c7290..8b90876 100644 --- a/lib/rest/request.js +++ b/lib/rest/request.js @@ -32,9 +32,8 @@ export function Request(config) { headers: headers, json: true }; - var apiVoiceUris = ['https://api-qa.voice.plivodev.com/v1/Account/','https://api-qa-usw1.voice.plivodev.com/v1/Account/','https://api-qa-use1.voice.plivodev.com/v1/Account/']; - var isVoiceReq = false; - var voiceRetryCount = 0; + let apiVoiceUris = ['https://api-voice.plivo.com/v1/Account/','https://api-voice-usw1.plivo.com/v1/Account/','https://api-voice-use1.plivo.com/v1/Account/']; + let isVoiceReq = false; if (params) { if (params.hasOwnProperty('is_call_insights_request')) { options.url = params.call_insights_base_url + params.call_insights_request_path; @@ -48,10 +47,7 @@ export function Request(config) { options.url = apiVoiceUris[0] + config.authId + '/' + action; delete params.isVoiceRequest; isVoiceReq = true; - - // console.log(options.url); } - // console.log(apiVoiceUris); } if (method === 'GET' && options.formData !== '') { @@ -67,48 +63,71 @@ export function Request(config) { options.timeout = config.timeout; } - if (isVoiceReq === true){ - voiceRequestFunc(options).then(function (response) { - var noRetry = response.response - if(noRetry.statusCode >= 500 ){ - voiceRetryCount++; - if(voiceRetryCount === 1){ - options.url = apiVoiceUris[1] + config.authId + '/' + action; - } - isVoiceReq = true; - voiceRequestFunc(options).then(function (response) { - var firstRetry = response.response; - if(firstRetry.statusCode >= 500 ) { - voiceRetryCount++; - if (voiceRetryCount === 2) { - options.url = apiVoiceUris[2] + config.authId + '/' + action; - } - isVoiceReq = true; - voiceRequestFunc(options).then(function (response) { - var secondRetry = response.response; - if(secondRetry.statusCode >= 500){ - console.log("d"); - return response; - } - console.log(secondRetry.statusCode,"c") - return response; - }) - } - console.log(firstRetry.statusCode, "b") - return response; - }) - } - console.log(noRetry.statusCode, "a") - return response; - }) - } - else { return new Promise((resolve, reject) => { request(options, (error, response, body) => { if (error) { 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(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, body) => { + if(error){ + reject(error); + return; + } + if(response.statusCode >= 500 && isVoiceReq === true){ + if(error){ + reject(error); + return; + } + 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 + resolve({ + response: response, + body: body + }); + } + } + let bodyOfTwoRetry = response.body + resolve({ + response: response, + body: bodyOfTwoRetry + }); + }) + } + let bodyOfOneRetry = response.body + resolve({ + response: response, + body: bodyOfOneRetry + }); + }); + } + const exceptionClass = { 400: Exceptions.InvalidRequestError, 401: Exceptions.AuthenticationError, @@ -116,6 +135,7 @@ export function Request(config) { 405: Exceptions.InvalidRequestError, 500: Exceptions.ServerError, } [response.statusCode] || Error; + if (!_.inRange(response.statusCode, 200, 300)) { body = body || response.body; if (typeof body === 'object') { @@ -137,66 +157,5 @@ export function Request(config) { } }); }); - } }; } - -function voiceRequestFunc(options) { - 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 - }); - } - }); - }); -} From 9aa9078a52fd29c23d891c5a2e0f0960559f1bc7 Mon Sep 17 00:00:00 2001 From: Koushik-Ayila Date: Fri, 17 Jul 2020 10:58:24 +0530 Subject: [PATCH 5/9] Modified list functions of Applications, Recordings and Endpoints --- lib/base.js | 3 +++ lib/resources/applications.js | 15 ++++++++++++++- lib/resources/endpoints.js | 6 ++++++ lib/resources/recordings.js | 15 +++++++++++++++ lib/rest/request.js | 7 +++---- 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/base.js b/lib/base.js index d687ac6..98d0f49 100644 --- a/lib/base.js +++ b/lib/base.js @@ -35,6 +35,9 @@ export class PlivoResource { client('POST', action + id + '/', params) .then(response => { extend(that, response.body); + if (params.hasOwnProperty('isVoiceRequest')){ + delete params.isVoiceRequest; + } extend(that, params); resolve(that); }) diff --git a/lib/resources/applications.js b/lib/resources/applications.js index ff0d99d..a06355a 100644 --- a/lib/resources/applications.js +++ b/lib/resources/applications.js @@ -60,7 +60,7 @@ export class Application extends PlivoResource { if (typeof params.cascade === 'boolean') { params.cascade = params.cascade.toString(); } - params.isVoiceRequest = 'true'; + params.isVoiceRequest = 'true'; return super.delete(params); } @@ -93,6 +93,19 @@ export class ApplicationInterface extends PlivoResourceInterface { return super.get(id, params); } + /** + * list applications + * @method + * @param {object} params - params to list applications + * @param {string} [params.subaccount] - ID of the subaccount if present + * @param {integer} [params.limit] - To display no of results per page + * @param {integer} [params.offset] - No of value items by which results should be offset + */ + list(params= {}) { + params.isVoiceRequest = 'true'; + return super.list(params); + } + /** * create Application * @method diff --git a/lib/resources/endpoints.js b/lib/resources/endpoints.js index b545840..13e371b 100644 --- a/lib/resources/endpoints.js +++ b/lib/resources/endpoints.js @@ -82,6 +82,12 @@ export class EndpointInterface extends PlivoResourceInterface { return super.get(id, params); } + list() { + let params = {}; + params.isVoiceRequest = 'true'; + return super.list(params); + } + /** * Create Endpoint * @method diff --git a/lib/resources/recordings.js b/lib/resources/recordings.js index df47d04..d5299b4 100644 --- a/lib/resources/recordings.js +++ b/lib/resources/recordings.js @@ -70,6 +70,21 @@ export class RecordingInterface extends PlivoResourceInterface { return super.get(id, params); } + /** + * list recordings + * @method + * @param {object} params - params to list recordings + * @param {string} [params.subaccount] - ID of the subaccount if present + * @param {string} [params.callUuid] - Call UUID of the call to filter recordings associated with it + * @param {string} [params.addTime] - Filter based on the timings they were added + * @param {string} [params.limit] - Display no of results per page + * @param {string} [params.offset] - No of value items by which results should be offset + */ + list(params= {}) { + params.isVoiceRequest = 'true'; + return super.list(params); + } + /** * Delete recording by id * @method diff --git a/lib/rest/request.js b/lib/rest/request.js index 8b90876..241d5d2 100644 --- a/lib/rest/request.js +++ b/lib/rest/request.js @@ -32,7 +32,7 @@ export function Request(config) { headers: headers, json: true }; - let apiVoiceUris = ['https://api-voice.plivo.com/v1/Account/','https://api-voice-usw1.plivo.com/v1/Account/','https://api-voice-use1.plivo.com/v1/Account/']; + let apiVoiceUris = ['https://api-qa.voice.plivodev.com/v1/Account/','https://api-qa-usw1.voice.plivodev.com/v1/Account/','https://api-qa-use1.voice.plivodev.com/v1/Account/']; let isVoiceReq = false; if (params) { if (params.hasOwnProperty('is_call_insights_request')) { @@ -45,10 +45,10 @@ export function Request(config) { } else if (params.hasOwnProperty('is_voice_request')){ options.url = apiVoiceUris[0] + config.authId + '/' + action; - delete params.isVoiceRequest; + delete params.is_voice_request; isVoiceReq = true; } - } + } if (method === 'GET' && options.formData !== '') { let query = '?' + queryString.stringify(params); @@ -69,7 +69,6 @@ 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 !== '') { From 06973cff428d197d04d59e27abe5d86ffef806d2 Mon Sep 17 00:00:00 2001 From: Koushik-Ayila Date: Fri, 17 Jul 2020 11:12:35 +0530 Subject: [PATCH 6/9] Change URLs to prod from QA --- lib/rest/request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rest/request.js b/lib/rest/request.js index 241d5d2..ac72236 100644 --- a/lib/rest/request.js +++ b/lib/rest/request.js @@ -32,7 +32,7 @@ export function Request(config) { headers: headers, json: true }; - let apiVoiceUris = ['https://api-qa.voice.plivodev.com/v1/Account/','https://api-qa-usw1.voice.plivodev.com/v1/Account/','https://api-qa-use1.voice.plivodev.com/v1/Account/']; + let apiVoiceUris = ['https://api-voice.plivo.com/v1/Account/','https://api-voice-usw1.plivo.com/v1/Account/','https://api-voice-use1.plivo.com/v1/Account/']; let isVoiceReq = false; if (params) { if (params.hasOwnProperty('is_call_insights_request')) { From e05bd27bff0c5f0fde4916883850964b0569587e Mon Sep 17 00:00:00 2001 From: Koushik-Ayila Date: Thu, 23 Jul 2020 10:36:53 +0530 Subject: [PATCH 7/9] Change URLs and increase minor version --- CHANGELOG.md | 3 +++ lib/rest/request.js | 2 +- package.json | 5 +++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1aa8556..d44c975 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Change Log +## [4.8.0](https://github.com/plivo/plivo-node/releases/tag/v4.8.0)(2020-07-23) +- Add retries to multiple regions for voice requests. + ## [4.7.0](https://github.com/plivo/plivo-node/releases/tag/v4.7.0)(2020-05-28) - Add JWT helper functions. diff --git a/lib/rest/request.js b/lib/rest/request.js index ac72236..33e2194 100644 --- a/lib/rest/request.js +++ b/lib/rest/request.js @@ -32,7 +32,7 @@ export function Request(config) { headers: headers, json: true }; - let apiVoiceUris = ['https://api-voice.plivo.com/v1/Account/','https://api-voice-usw1.plivo.com/v1/Account/','https://api-voice-use1.plivo.com/v1/Account/']; + let apiVoiceUris = ['https://voice.plivo.com/v1/Account/','https://voice-usw1.plivo.com/v1/Account/','https://voice-use1.plivo.com/v1/Account/']; let isVoiceReq = false; if (params) { if (params.hasOwnProperty('is_call_insights_request')) { diff --git a/package.json b/package.json index bda1ac2..59d4c4b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "plivo", - "version": "4.7.0", + "name": "plivo-node", + "version": "4.8.0", "description": "A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML", "homepage": "https://github.com/plivo/plivo-node", "files": [ @@ -61,6 +61,7 @@ "build-url": "^1.0.10", "jsonwebtoken": "^8.5.1", "lodash": "^4.17.4", + "plivo": "^4.7.0", "querystring": "^0.2.0", "request": "^2.81.0", "uri-parser": "^1.0.0", From 681457d352d641f9f8be42bac7bfa02fd80df307 Mon Sep 17 00:00:00 2001 From: Koushik-Ayila Date: Mon, 3 Aug 2020 11:15:12 +0530 Subject: [PATCH 8/9] 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 }); } + } }); }); }; From 450aa573fd16b18e1e99eff84c4d1acff0e3ddd6 Mon Sep 17 00:00:00 2001 From: Koushik-Ayila Date: Mon, 3 Aug 2020 12:50:55 +0530 Subject: [PATCH 9/9] Change package.json --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 59d4c4b..c435957 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "plivo-node", + "name": "plivo", "version": "4.8.0", "description": "A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML", "homepage": "https://github.com/plivo/plivo-node", @@ -61,7 +61,6 @@ "build-url": "^1.0.10", "jsonwebtoken": "^8.5.1", "lodash": "^4.17.4", - "plivo": "^4.7.0", "querystring": "^0.2.0", "request": "^2.81.0", "uri-parser": "^1.0.0",