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 10 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.7 (August 18, 2021)
* Migrate from `request` to `axios` as main lib
* Keep alive connection by default

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

Expand Down
43 changes: 29 additions & 14 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 @@ -31,18 +37,22 @@ export class CookieRestClient extends NoAuthRestClient {

async login() {
this.emitter.logger.info('Performing Login ...');
const loginResponse = await requestCall({
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.');
Expand All @@ -51,10 +61,13 @@ export class CookieRestClient extends NoAuthRestClient {
async logout() {
if (this.cfg.logoutUrl && this.loggedIn) {
this.emitter.logger.info('Performing Logout...');
const logoutResponse = await requestCall({
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;
Expand All @@ -64,8 +77,10 @@ export class CookieRestClient extends NoAuthRestClient {
}
}

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
21 changes: 16 additions & 5 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,7 +25,7 @@ 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}`);
Expand Down Expand Up @@ -56,7 +60,14 @@ export class NoAuthRestClient {
// 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
Loading