Skip to content

Commit

Permalink
Merge pull request #40 from mattmeyerink/personal/mmeyerink/update-ht…
Browse files Browse the repository at this point in the history
…tp-request-library

feat: remove Axios in favor of request
  • Loading branch information
mattmeyerink committed Oct 20, 2023
2 parents 79d89ae + 7df1535 commit 9c0e004
Show file tree
Hide file tree
Showing 10 changed files with 399 additions and 205 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [4.0.0] - 2023-10-20
### Security
- Removed `request` dependency and replaced it with `Axios`
- Resolved issue in v3.1.3 with PUT/POST requests

## [3.1.4] - 2023-08-22
### Revert
- Reverted changes in release v3.1.3 as it introduced a bug with PUT/POST requests.
Expand Down
84 changes: 46 additions & 38 deletions lib/utils/httpRequestor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.create = function(requestorConfig) {
: requestLogger.empty;

var request = requestorConfig.request ||
Promise.promisifyAll(require("request"), {multiArgs: true});
Promise.promisifyAll(require("axios"), {multiArgs: true});

var handleResponse = requestorConfig.handleResponse ||
require('./responseHandler');
Expand Down Expand Up @@ -83,13 +83,9 @@ exports.create = function(requestorConfig) {
};

var get = (options, callback) =>
methodRequest(options, request.getAsync, 'GET', callback);
methodRequest(options, request.get, 'GET', callback);

var post = (options, callback) => {
var requestExtension = { body: JSON.stringify(options.body) };

return methodRequest(options, request.postAsync, 'POST', callback, requestExtension);
};
var post = (options, callback) => methodRequest(options, request.post, 'POST', callback, options.body);

var getFileBody = (options) => {
if (options.path) {
Expand All @@ -99,59 +95,71 @@ exports.create = function(requestorConfig) {
return options.fileStream;
};

var postFile = (options, callback) => {
var requestExtension = { body: getFileBody(options) };

return methodRequest(options, request.postAsync, 'POST', callback, requestExtension);
};
var postFile = (options, callback) => methodRequest(options, request.post, 'POST', callback, getFileBody(options));

var deleteFunc = (options, callback) =>
methodRequest(options, request.delAsync, 'DELETE', callback);
methodRequest(options, request.delete, 'DELETE', callback);

var put = (options, callback) => {
var requestExtension = { body: JSON.stringify(options.body) };
var put = (options, callback) => methodRequest(options, request.put, 'PUT', callback, options.body);

return methodRequest(options, request.putAsync, 'PUT', callback, requestExtension);
};

var methodRequest = (options, method, methodName, callback, requestExtension) => {
var methodRequest = (options, method, methodName, callback, body) => {
var baseRequestOptions = {
url: buildUrl(options),
headers: buildHeaders(options),
qs: options.queryParameters,
encoding: options.encoding,
gzip: true
params: options.queryParameters,
responseEncoding: options.encoding
};
var requestOptions = _.extend(baseRequestOptions, requestExtension);
var url = buildUrl(options);
var requestOptions = baseRequestOptions;

var retryOptions = _.pick(options, 'maxRetryDurationMillis', 'calcRetryBackoff');

logger.logRequest(methodName, requestOptions);

return makeRequestWithRetries(method, methodName, requestOptions, retryOptions, callback);
return makeRequestWithRetries(url, method, methodName, requestOptions, body, retryOptions, callback);
};

var makeRequestWithRetries = (method, methodName, requestOptions, retryOptions, callback) => {
var makeRequestWithRetries = (url, method, methodName, requestOptions, body, retryOptions, callback) => {
var effectiveRetryOptions = _.defaults(retryOptions, defaultRetryOptions);

effectiveRetryOptions.endRetryTime = Date.now() + effectiveRetryOptions.maxRetryDurationMillis;

return retryHelper(method, methodName, requestOptions, effectiveRetryOptions, 0)
.tap(logger.logSuccessfulResponse)
.tapCatch(error => logger.logErrorResponse(methodName, requestOptions, error))
.get('content')
.catch(error => Promise.reject(_.omit(error, 'headers', 'body')))
.nodeify(callback);
return retryHelper(url, method, methodName, requestOptions, body, effectiveRetryOptions, 0)
.then((response) => {
logger.logSuccessfulResponse(response);

if (callback) {
callback(undefined, response.content);
}

return response.content;
})
.catch((error) => {
logger.logErrorResponse(methodName, requestOptions, error);

if (callback) {
callback(error, undefined);
}

return new Promise.reject(_.omit(error, 'headers', 'body'));
});
};

var retryHelper = (method, methodName, requestOptions, retryOptions, numRetries) =>
makeRequest(method, methodName, requestOptions)
.catch(retryWithBackoffHelper(method, methodName, requestOptions, retryOptions, numRetries));
var retryHelper = (url, method, methodName, requestOptions, body, retryOptions, numRetries) => {
return methodHandler(url, method, methodName, requestOptions, body)
.then(handleResponse)
.catch(retryWithBackoffHelper(url, method, methodName, requestOptions, retryOptions, numRetries));
};

var methodHandler = (url, method, methodName, requestOptions, body) => {
if (methodName === "POST" || methodName === "PUT") {
return method(url, body, requestOptions);
}

var makeRequest = (method, methodName, requestOptions) =>
method(requestOptions).spread(handleResponse);
return method(url, requestOptions);
};

var retryWithBackoffHelper = (method, methodName, requestOptions, retryOptions, numRetries) => {
var retryWithBackoffHelper = (url, method, methodName, requestOptions, retryOptions, numRetries) => {
return error => {
var backoffMillis = retryOptions.calcRetryBackoff(numRetries, error);

Expand All @@ -168,7 +176,7 @@ exports.create = function(requestorConfig) {
var nextRetry = numRetries + 1;
logger.logRetryAttempt(methodName, requestOptions, error, nextRetry);
return Promise.delay(backoffMillis)
.then(() => retryHelper(method, methodName, requestOptions, retryOptions, nextRetry));
.then(() => retryHelper(url, method, methodName, requestOptions, retryOptions, nextRetry));
};
};

Expand Down
31 changes: 14 additions & 17 deletions lib/utils/responseHandler.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
var Promise = require("bluebird");

module.exports = (response, body) => {
module.exports = (response) => {
var outResponse = {
statusCode: response.statusCode,
statusCode: response.status,
headers: response.headers,
body: body
body: response.data
};

if (response.statusCode != 200) {
if (response.status != 200) {
var errorResponse = outResponse;
if (/\bapplication\/json\b/.test(response.headers['content-type'])) {
var responseBody = JSON.parse(body);
errorResponse.errorCode = responseBody.errorCode;
errorResponse.message = responseBody.message;
errorResponse.refId = responseBody.refId;
errorResponse.errorCode = response.data.errorCode;
errorResponse.message = response.data.message;
errorResponse.refId = response.data.refId;

if (responseBody.detail !== undefined) {
errorResponse.detail = responseBody.detail;
if (response.data.detail !== undefined) {
errorResponse.detail = response.data.detail;
}
} else {
errorResponse.message = body;
errorResponse.message = response.data;
}

return new Promise.reject(errorResponse);
} else if (response.headers['content-type'] === 'application/json;charset=UTF-8') {
outResponse.content = JSON.parse(body);
return outResponse;
} else {
outResponse.content = body;
return outResponse;
}

outResponse.content = response.data;

return outResponse;
};
Loading

0 comments on commit 9c0e004

Please sign in to comment.