-
Notifications
You must be signed in to change notification settings - Fork 10
/
HttpService.ts
59 lines (53 loc) · 1.53 KB
/
HttpService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import * as got from 'got';
import Logger from './Logger';
/**
* Wrapper class for the request package. Used to make HTTP calls.
*/
export default class HTTPService {
private debug?: boolean;
private options: got.GotJSONOptions;
constructor(options: got.GotJSONOptions, debug?: boolean) {
this.debug = debug;
this.options = options;
}
/**
* Base method for GET requests
*
* @param {string} url Url to GET from
* @param {string} [authenticationToken] optional authentication token
* @returns {Promise<object>}
* @memberof HTTPService
*/
public get(url: string, authenticationToken?: string): Promise<object> {
if (this.debug) {
Logger.debug(`GET - ${url}${authenticationToken ? ' - OAUTH request' : ''}`);
}
//Deep copy
const authOptions = JSON.parse(JSON.stringify(this.options));
authOptions.headers['Authorization'] = `Bearer ${authenticationToken}`;
return new Promise<object>((resolve, reject) => {
got(url, authenticationToken ? authOptions : this.options)
.then(response => {
resolve(response.body);
})
.catch(err => {
reject(err);
});
});
}
public post(url: string, data: got.GotJSONOptions): Promise<object> {
if (this.debug) {
Logger.debug(`POST - ${url}`);
}
return new Promise<object>((resolve, reject) => {
got
.post(url, data)
.then(response => {
resolve(response.body);
})
.catch(err => {
reject(err);
});
});
}
}