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

Issue 68: Implement redirect handling. #70

Closed
wants to merge 2 commits into from
Closed
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
67 changes: 60 additions & 7 deletions src/service/rest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ export class RestService {
return request(req, cb);
}

public get(url: string, contentType: string = 'application/json'): Promise<any> {
public get(url: string, json: any = null, contentType: string = 'application/json', accept: string | string[] = [ 'application/json', 'text/plain' ], redirectCount: number = 0): Promise<any> {
Copy link
Contributor

@wwelling wwelling Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For code cleanliness, methods should have two or less arguments. Dyadic is preferred max number of arguments. Triadic is borderline messy. Four or more is in need of code cleanup.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a type or interface with these arguments as properties opposed to adding too many arguments.

return new Promise((resolve, reject) => {
request.get({
url,
headers: {
'X-Okapi-Tenant': config.get('tenant'),
'X-Okapi-Token': config.get('token'),
'Content-Type': contentType
'Content-Type': contentType,
'Accept': accept
}
}, (error: any, response: any, body: any) => {
if (response && response.statusCode >= 200 && response.statusCode <= 299) {
Expand All @@ -24,22 +25,28 @@ export class RestService {
// console.log('failed get', url, error);
reject(error);
} else {
const promise = this.redirectRecurse(url, json, contentType, accept, response, this.get, redirectCount + 1);
if (promise) {
return this.resolveRedirectPromise(promise, resolve, reject);
}

// console.log('failed get', url);
reject(body);
}
});
});
}

public post(url: string, json: any, contentType: string = 'application/json'): Promise<any> {
public post(url: string, json: any, contentType: string = 'application/json', accept: string | string[] = [ 'application/json', 'text/plain' ], redirectCount: number = 0): Promise<any> {
return new Promise((resolve, reject) => {
request.post({
url,
json,
headers: {
'X-Okapi-Tenant': config.get('tenant'),
'X-Okapi-Token': config.get('token'),
'Content-Type': contentType
'Content-Type': contentType,
'Accept': accept
}
}, (error: any, response: any, body: any) => {
if (response && response.statusCode >= 200 && response.statusCode <= 299) {
Expand All @@ -48,22 +55,28 @@ export class RestService {
console.log('failed post', url, error);
reject(error);
} else {
const promise = this.redirectRecurse(url, json, contentType, accept, response, this.post, redirectCount + 1);
if (promise) {
return this.resolveRedirectPromise(promise, resolve, reject);
}

console.log('failed post', url, json);
reject(body);
}
});
});
}

public put(url: string, json: any, contentType: string = 'application/json'): Promise<any> {
public put(url: string, json: any, contentType: string = 'application/json', accept: string | string[] = [ 'application/json', 'text/plain' ], redirectCount: number = 0): Promise<any> {
return new Promise((resolve, reject) => {
request.put({
url,
json,
headers: {
'X-Okapi-Tenant': config.get('tenant'),
'X-Okapi-Token': config.get('token'),
'Content-Type': contentType
'Content-Type': contentType,
'Accept': accept
}
}, (error: any, response: any, body: any) => {
if (response && response.statusCode >= 200 && response.statusCode <= 299) {
Expand All @@ -72,14 +85,19 @@ export class RestService {
console.log('failed put', url, error);
reject(error);
} else {
const promise = this.redirectRecurse(url, json, contentType, accept, response, this.put, redirectCount + 1);
if (promise) {
return this.resolveRedirectPromise(promise, resolve, reject);
}

console.log('failed put', url, json);
reject(body);
}
});
});
}

public delete(url: string, contentType: string = 'application/json', accept: string = 'text/plain'): Promise<any> {
public delete(url: string, json: any = null, contentType: string = 'application/json', accept: string | string[] = 'text/plain', redirectCount: number = 0): Promise<any> {
return new Promise((resolve, reject) => {
request.delete({
url,
Expand All @@ -96,11 +114,46 @@ export class RestService {
// console.log('failed delete', url, error);
reject(error);
} else {
const promise = this.redirectRecurse(url, json, contentType, accept, response, this.delete, redirectCount + 1);
if (promise) {
return this.resolveRedirectPromise(promise, resolve, reject);
}

// console.log('failed delete', url);
reject(body);
}
});
});
}

private redirectRecurse(url: string, json: any, contentType: string, accept: string | string[], response: any, callback: (url: string, json: any, contentType: string, accept: string | string[], redirectCount: number) => Promise<any>, redirectCount: number): Promise<any> | false {
if (response.statusCode != 301 && response.statusCode != 302 || !response.headers || !response.headers.location) {
return false;
}

if (redirectCount > 10) {
console.error('ERROR: Max redirect reached on HTTP', response.statusCode, 'from', url, 'to', response.headers.location, '.');

return false;
}

const logLevel: string = config.get('logLevel');
if (logLevel.toLowerCase() == "debug") {
console.debug('Redirecting (', redirectCount, 'times ) because of HTTP', response.statusCode, 'from', url, 'to', response.headers.location, '.');
}

return callback(response.headers.location, json, contentType, accept, redirectCount + 1);
}

private resolveRedirectPromise(promise: Promise<any>, resolve: any, reject: any): Promise<any> {
promise.then((result) => {
resolve(result);
})
.catch ((err) => {
reject(err);
});

return promise;
}

}