mirror of
https://github.com/donl/plivo-node.git
synced 2026-06-30 06:12:08 -06:00
Voice Retry Logic - Under Development
This commit is contained in:
parent
6596465713
commit
306022a280
1 changed files with 93 additions and 64 deletions
|
|
@ -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
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue