Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate from deprecated request module to axios #542

Merged
merged 2 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions lib/base/RequestClient.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
'use strict';

var _ = require('lodash');
var http = require('request');
var axios = require('axios');
var fs = require('fs');
var http = require('http');
var https = require('https');
var Q = require('q');
var qs = require('qs');
var Response = require('../http/response');
var Request = require('../http/request');

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

var RequestClient = function() {};

/**
Expand Down Expand Up @@ -47,11 +52,12 @@ RequestClient.prototype.request = function(opts) {

var options = {
timeout: opts.timeout || 30000,
followRedirect: opts.allowRedirects || false,
maxRedirects: opts.allowRedirects ? 10 : 0, // Same number of allowed redirects as request module default
url: opts.uri,
method: opts.method,
headers: opts.headers,
forever: opts.forever === false ? false : true,
httpAgent: opts.forever ? new http.Agent({ keepAlive: true }) : undefined,
httpsAgent: opts.forever ? new https.Agent({ keepAlive: true }) : undefined,
};

if (process.env.TWILIO_CA_BUNDLE !== undefined) {
Expand All @@ -62,20 +68,22 @@ RequestClient.prototype.request = function(opts) {
}

if (!_.isNull(opts.data)) {
options.formData = opts.data;
options.data = qs.stringify(opts.data, { arrayFormat: 'repeat'});
}

if (!_.isNull(opts.params)) {
options.qs = opts.params;
options.useQuerystring = true;
options.params = opts.params;
options.paramSerializer = (params) => {
return qs.stringify(params, { arrayFormat: 'repeat' });
};
}

var optionsRequest = {
method: options.method,
url: options.url,
auth: b64Auth || null,
params: options.qs,
data: options.formData,
params: options.params,
data: opts.data,
headers: options.headers,
ca: options.ca
};
Expand All @@ -84,17 +92,15 @@ RequestClient.prototype.request = function(opts) {
this.lastResponse = undefined;
this.lastRequest = new Request(optionsRequest);

http(options, function(error, response) {
if (error) {
_this.lastResponse = undefined;
deferred.reject(error);
} else {
_this.lastResponse = new Response(response.statusCode, response.body);
deferred.resolve({
statusCode: response.statusCode,
body: response.body,
});
}
axios(options).then((response) => {
_this.lastResponse = new Response(response.status, response.data);
deferred.resolve({
statusCode: response.status,
body: response.data,
});
}).catch((error) => {
_this.lastResponse = undefined;
deferred.reject(error);
});

return deferred.promise;
Expand Down
Loading