mirror of
https://github.com/donl/plivo-node.git
synced 2026-06-30 06:12:08 -06:00
Added voice retry for axios library
This commit is contained in:
parent
b569c90678
commit
07106e961d
2 changed files with 42 additions and 150 deletions
|
|
@ -1031,4 +1031,4 @@ class QueuedCallInterface extends PlivoResourceInterface {
|
|||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,21 @@ export function Axios(config) {
|
|||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
const retryWrapper = (axios, options) => {
|
||||
const max_time = options.retryTime;
|
||||
let counter = 0;
|
||||
axios.interceptors.response.use(null, (error) => {
|
||||
const config = error.config
|
||||
if (counter < max_time && error.response.status >= 500) {
|
||||
counter++
|
||||
return new Promise((resolve) => {
|
||||
resolve(axios(config))
|
||||
})
|
||||
}
|
||||
return Promise.reject(error)
|
||||
})
|
||||
}
|
||||
|
||||
return (method, action, params) => {
|
||||
if (typeof (params) != 'undefined' && typeof (params.file) != 'undefined') {
|
||||
var files = []
|
||||
|
|
@ -69,112 +84,24 @@ export function Axios(config) {
|
|||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// axios(options, (error, response, body) => {
|
||||
axios(options).then(response => {
|
||||
// if (error) {
|
||||
// reject(error);
|
||||
// return;
|
||||
// }
|
||||
|
||||
if(isVoiceReq === true){
|
||||
if (response.status >= 500){
|
||||
options.url = apiVoiceUris[1] + config.authId + '/' + action;
|
||||
if (method === 'GET' && options.data !== '') {
|
||||
let query = '?' + queryString.stringify(params);
|
||||
options.url += query;
|
||||
}
|
||||
// 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.data !== '') {
|
||||
let query = '?' + queryString.stringify(params);
|
||||
options.url += query;
|
||||
}
|
||||
// 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.status] || Error;
|
||||
|
||||
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 {
|
||||
reject(new exceptionClass(body));
|
||||
}
|
||||
}
|
||||
// else {
|
||||
// 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.status;
|
||||
}
|
||||
resolve({
|
||||
response: response,
|
||||
body: body
|
||||
});
|
||||
// }
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
// console.log(error);
|
||||
reject(error.stack + '\n' + JSON.stringify(error.response.data));
|
||||
})
|
||||
}
|
||||
else {
|
||||
const exceptionClass = {
|
||||
400: Exceptions.InvalidRequestError,
|
||||
401: Exceptions.AuthenticationError,
|
||||
404: Exceptions.ResourceNotFoundError,
|
||||
405: Exceptions.InvalidRequestError,
|
||||
500: Exceptions.ServerError,
|
||||
} [response.status] || Error;
|
||||
|
||||
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 {
|
||||
reject(new exceptionClass(body));
|
||||
}
|
||||
}
|
||||
// else {
|
||||
// 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.status;
|
||||
}
|
||||
resolve({
|
||||
response: response,
|
||||
body: body
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
// console.log(error);
|
||||
reject(error.stack + '\n' + JSON.stringify(error.response.data));
|
||||
})
|
||||
}
|
||||
else {
|
||||
if (isVoiceReq) {
|
||||
retryWrapper(axios, {retryTime: 2})
|
||||
let retryAttempt;
|
||||
for (retryAttempt = 0; retryAttempt < 3; retryAttempt++) {
|
||||
options.url = apiVoiceUris[retryAttempt] + config.authId + '/' + action;
|
||||
axios(options).then(response => {
|
||||
resolve({
|
||||
response: response,
|
||||
body: response.data
|
||||
});
|
||||
})
|
||||
.catch(function (error) {
|
||||
reject(error.stack+ "\r\n" + JSON.stringify(error.response.data));
|
||||
})
|
||||
}
|
||||
}
|
||||
else {
|
||||
axios(options).then(response => {
|
||||
const exceptionClass = {
|
||||
400: Exceptions.InvalidRequestError,
|
||||
401: Exceptions.AuthenticationError,
|
||||
|
|
@ -184,16 +111,15 @@ export function Axios(config) {
|
|||
} [response.status] || Error;
|
||||
|
||||
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 {
|
||||
}
|
||||
else {
|
||||
reject(new exceptionClass(body));
|
||||
}
|
||||
}
|
||||
// else {
|
||||
// let body = response.body;
|
||||
else {
|
||||
let body = response.data;
|
||||
let isObj = typeof _body === 'object' && _body !== null && !(_body instanceof Array) && !(_body instanceof Date)
|
||||
if (isObj) {
|
||||
|
|
@ -204,45 +130,11 @@ export function Axios(config) {
|
|||
body: body
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
const exceptionClass = {
|
||||
400: Exceptions.InvalidRequestError,
|
||||
401: Exceptions.AuthenticationError,
|
||||
404: Exceptions.ResourceNotFoundError,
|
||||
405: Exceptions.InvalidRequestError,
|
||||
500: Exceptions.ServerError,
|
||||
} [response.status] || Error;
|
||||
|
||||
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 {
|
||||
reject(new exceptionClass(body));
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 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.status;
|
||||
}
|
||||
resolve({
|
||||
response: response,
|
||||
body: body
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(function (error) {
|
||||
// handle error
|
||||
// console.log(error);
|
||||
reject(error.stack + '\n' + JSON.stringify(error.response.data));
|
||||
})
|
||||
})
|
||||
.catch(function (error) {
|
||||
reject(error.stack + '\n' + JSON.stringify(error.response.data));
|
||||
})
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue