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

first attempt to migrate from request lib #50

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.1.7 (August 18, 2021)
* Migrate from `request` to `axios` as main lib
* Keep alive connection by default
* Reduce logging level to trace

## 1.1.6 (July 29, 2021)
* Now `getAttachment` from `AttachmentProcessor` may retrieve items from `Maester`

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ A number of REST Client classes are available to use and extend to create Client

Each of the REST Clients extends from the `NoAuthRestClient`, overriding the relevant methods.

By default enabled [keepAlive](https://nodejs.org/api/http.html#http_new_agent_options) for all HTTP(S) requests

### NoAuthRestClient
[NoAuthRestClient](https://github.com/elasticio/component-commons-library/blob/master/lib/authentication/NoAuthRestClient.ts) class to make rest requests no no auth APIs by provided options.

Expand All @@ -51,6 +53,24 @@ options expects the following sub-variables:
- headers: Any HTTP headers to add to the request. Defaults to {}
- urlIsSegment: Whether to append to the base server url or if the provided URL is an absolute path. Defaults to true
- isJson: If the request is in JSON format. Defaults to true
- axiosOptions: object, that will be implemented in request as additional parameters according [axios documentation](https://axios-http.com/docs/req_config). Defaults to {}

Example:
```javascript
options = {
url: 'https://example.com/',
method: 'get',
headers: {
foo: 'bar'
},
urlIsSegment: false,
isJson: true,
axiosOptions: {
timeout: 1000,
responseType: 'arraybuffer'
}
}
```


Class can be extended to have custom authentication
Expand Down
53 changes: 34 additions & 19 deletions lib/authentication/CookieRestClient.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
/* eslint-disable no-param-reassign, no-underscore-dangle, class-methods-use-this */
import { promisify } from 'util';
import request from 'request';
import axios, { AxiosInstance } from 'axios';
import http from 'http';
import https from 'https';
import querystring from 'querystring';
import toughCookie, { CookieJar } from 'tough-cookie';
import { NoAuthRestClient } from './NoAuthRestClient';

const requestCall = promisify(request);

export class CookieRestClient extends NoAuthRestClient {
loggedIn: boolean;
jar: any;
jar: CookieJar;
requestCall: AxiosInstance;

constructor(emitter, cfg) {
super(emitter, cfg);
this.jar = request.jar();
this.jar = new toughCookie.CookieJar();
this.loggedIn = false;
this.requestCall = axios.create({
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
});
}

private basicResponseCheck(response) {
Expand All @@ -30,42 +36,51 @@ export class CookieRestClient extends NoAuthRestClient {
}

async login() {
this.emitter.logger.info('Performing Login ...');
const loginResponse = await requestCall({
this.emitter.logger.trace('Performing Login ...');
const loginResponse = await this.requestCall({
method: 'POST',
url: this.cfg.loginUrl,
form: {
data: querystring.stringify({
username: this.cfg.username,
password: this.cfg.password,
},
}),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
jar: this.jar,
withCredentials: true,
});

loginResponse.headers['set-cookie']
.forEach(async (cookie) => { await this.jar.setCookie(cookie, loginResponse.config.url!); });

this.handleLoginResponse(loginResponse);
this.loggedIn = true;
this.emitter.logger.info('Login Complete.');
this.emitter.logger.trace('Login Complete.');
}

async logout() {
if (this.cfg.logoutUrl && this.loggedIn) {
this.emitter.logger.info('Performing Logout...');
const logoutResponse = await requestCall({
this.emitter.logger.trace('Performing Logout...');
const logoutResponse = await this.requestCall({
method: this.cfg.logoutMethod,
url: this.cfg.logoutUrl,
jar: this.jar,
withCredentials: true,
headers: {
Cookie: await this.jar.getCookieString(this.cfg.logoutUrl),
},
});
this.handleLogoutResponse(logoutResponse);
this.loggedIn = false;
this.emitter.logger.info('Logout complete.');
this.emitter.logger.trace('Logout complete.');
} else {
this.emitter.logger.info('Nothing to logout');
this.emitter.logger.trace('Nothing to logout');
}
}

protected addAuthenticationToRequestOptions(requestOptions) {
requestOptions.jar = this.jar;
protected async addAuthenticationToRequestOptions(requestOptions) {
if (!requestOptions.headers) requestOptions.headers = {};
requestOptions.headers.Cookie = await this.jar.getCookieString(requestOptions.url);
requestOptions.withCredentials = true;
}

async makeRequest(options) {
Expand Down
41 changes: 30 additions & 11 deletions lib/authentication/NoAuthRestClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-param-reassign, no-underscore-dangle, class-methods-use-this */
import { promisify } from 'util';
const request = promisify(require('request'));
import axios from 'axios';
import http from 'http';
import https from 'https';
import removeTrailingSlash from 'remove-trailing-slash';
import removeLeadingSlash from 'remove-leading-slash';

Expand All @@ -12,7 +13,10 @@ export class NoAuthRestClient {
constructor(emitter, cfg) {
this.emitter = emitter;
this.cfg = cfg;
this.request = request;
this.request = axios.create({
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
});
}

// @ts-ignore: no-unused-variable
Expand All @@ -21,10 +25,10 @@ export class NoAuthRestClient {

protected handleRestResponse(response) {
if (response.statusCode >= 400) {
throw new Error(`Error in making request to ${response.request.uri.href} Status code: ${response.statusCode}, Body: ${JSON.stringify(response.body)}`);
throw new Error(`Error in making request to ${response.config.url}/ Status code: ${response.statusCode}, Body: ${JSON.stringify(response.body)}`);
}

this.emitter.logger.debug(`Response statusCode: ${response.statusCode}`);
this.emitter.logger.trace(`Response statusCode: ${response.statusCode}`);
return response.body;
}

Expand All @@ -37,26 +41,41 @@ export class NoAuthRestClient {
// if the provided URL is an absolute path. Defaults to true
async makeRequest(options) {
const {
url, method, body, headers = {}, urlIsSegment = true, isJson = true, responseHandler,
axiosOptions = {},
body,
headers = {},
isJson = true,
method,
responseHandler,
url,
urlIsSegment = true,
} = options;
const urlToCall = urlIsSegment
? `${removeTrailingSlash(this.cfg.resourceServerUrl.trim())}/${removeLeadingSlash(url.trim())}` // Trim trailing or leading '/'
: url.trim();

this.emitter.logger.debug(`Making ${method} request...`);
this.emitter.logger.trace(`Making ${method} request...`);

const requestOptions = {
method,
body,
...axiosOptions,
headers,
method,
data: body,
url: urlToCall,
json: isJson,
};
if (isJson) requestOptions.headers['Content-type'] = 'application/json';

// eslint-disable-next-line no-underscore-dangle
await this.addAuthenticationToRequestOptions(requestOptions);

const response = await this.request(requestOptions);
let response;
try {
response = await this.request(requestOptions);
} catch (err) {
response = err.response || err;
}
response.body = response.data;
response.statusCode = response.status;

if (responseHandler) {
return responseHandler(response, this.handleRestResponse.bind(this));
Expand Down
3 changes: 3 additions & 0 deletions lib/authentication/NtlmRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export class NtlmRestClient extends NoAuthRestClient {
headers: requestOptions.headers,
},
});
response.data = response.body;
response.status = response.statusCode;
response.config = { url: response.request.uri.href.replace(/\/+$/, '') };
return response;
};
}
Expand Down
6 changes: 3 additions & 3 deletions lib/authentication/OAuth2AuthorizationCodeRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const request = promisify(require('request'));

export class OAuth2RestClient extends NoAuthRestClient {
private async fetchNewToken() {
this.emitter.logger.info('Fetching new token...');
this.emitter.logger.trace('Fetching new token...');
const authTokenResponse = await request({
uri: this.cfg.authorizationServerTokenEndpointUrl,
method: 'POST',
Expand All @@ -21,7 +21,7 @@ export class OAuth2RestClient extends NoAuthRestClient {
},
});

this.emitter.logger.info('New token fetched...');
this.emitter.logger.trace('New token fetched...');

if (authTokenResponse.statusCode >= 400) {
throw new Error(`Error in authentication. Status code: ${authTokenResponse.statusCode}, Body: ${JSON.stringify(authTokenResponse.body)}`);
Expand All @@ -37,7 +37,7 @@ export class OAuth2RestClient extends NoAuthRestClient {
const tokenExpiryTime = new Date(this.cfg.oauth2.tokenExpiryTime);
const now = new Date();
if (now < tokenExpiryTime) {
this.emitter.logger.info('Previously valid token found.');
this.emitter.logger.trace('Previously valid token found.');
return this.cfg.oauth2.access_token;
}

Expand Down
Loading