Skip to content

first attempt to migrate from request lib #50

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

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
38 changes: 24 additions & 14 deletions lib/authentication/CookieRestClient.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/* 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 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();
}

private basicResponseCheck(response) {
Expand All @@ -31,18 +32,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 +56,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 +72,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
16 changes: 11 additions & 5 deletions lib/authentication/NoAuthRestClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* 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 removeTrailingSlash from 'remove-trailing-slash';
import removeLeadingSlash from 'remove-leading-slash';

Expand All @@ -12,7 +11,7 @@ export class NoAuthRestClient {
constructor(emitter, cfg) {
this.emitter = emitter;
this.cfg = cfg;
this.request = request;
this.request = axios.create();
}

// @ts-ignore: no-unused-variable
Expand All @@ -21,7 +20,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 +55,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