plivo-node/lib/rest/request.js
nixonsam a4e3f9fd47
Add PHLO support (#112)
* Added phlo client.

* Finished implementaiton of initializing phlo client.

* Finished logic for v2 methods of multiparty call(not members).

* Added test cases for multiparty call.

* Finished logic for phlo get api.

* Added member method structure.

* Finished implementation of phlo.run().

* Finished api calls for multi party(except abort transfer).

* Deleted old rest.js file.

* Finished abortTranfer implementation with test cases.

* Finished implementation of member.resumeCall().

* Finished all member functions.

* Reverted debugging logic.

* Removed console.log

* Added samples for all apis. method structure change for abort transfer.

* Resolved nsp issue.

* Audit fix.

* Package Lock Fix.

* Added test proxy for test cases.

* Resolved issue in request.js json format.

* Improved comments.

* Added test cases for .get() methods.

* Removed async await to support older browsers.

* Modified examples of phlo, removed async await.

* Resolved example issues, remove auth credentials.

* Removed es6 declaration syntaxes from phlo example.

* Added logic to pass payload.

* changed beta version 4.1-beta.1

* changed beta version 4.1-beta.1

* changed beta version 4.1-beta.1

* changes in changelog & readme- Add PHLO support
2019-03-11 15:43:57 +05:30

67 lines
1.8 KiB
JavaScript

import request from 'request';
import queryString from 'querystring';
import * as Exceptions from '../utils/exceptions';
import * as _ from "lodash";
export function Request(config) {
let auth = 'Basic ' + new Buffer(config.authId + ':' + config.authToken)
.toString('base64');
let headers = {
Authorization: auth,
'User-Agent': config.userAgent,
'Content-Type': 'application/json'
};
return (method, action, params) => {
var options = {
url: config.url + '/' + action,
method: method,
formData: params || '',
headers: headers,
json: true
};
if (method === 'GET' && options.formData !== '') {
let query = '?' + queryString.stringify(params);
options.url += query;
}
if (typeof config.proxy !== 'undefined') {
options.proxy = config.proxy;
}
if (typeof config.timeout !== 'undefined') {
options.timeout = config.timeout;
}
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 (!_.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 });
}
});
});
};
}