-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RequestManager.ts
126 lines (108 loc) · 3.18 KB
/
RequestManager.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class RequestManager {
private baseUrl: string;
private authorization: string;
private requestAttempts: number;
private fetchRetryInterval: number;
private cookie: string;
constructor(baseUrl) {
this.baseUrl = baseUrl;
this.authorization = null;
this.requestAttempts = 1;
this.fetchRetryInterval = 1000;
}
setRequestAttempts(requestAttempts) {
this.requestAttempts = requestAttempts;
}
setFetchRetryInterval(interval_ms) {
this.fetchRetryInterval = interval_ms;
}
setAuthorization(authorization) {
this.authorization = authorization;
}
setCookie(cookie) {
this.cookie = cookie;
}
fetch(resource, options) {
const response = this.fetchRaw(resource, options);
const dataAsString = response.getBlob().getDataAsString();
try {
return JSON.parse(dataAsString);
} catch (e) {
return dataAsString;
}
}
fetchRaw(resource, options) {
const authOptions = this.addAuthorization(options);
const authCookieOptions = this.addCookie(authOptions);
authCookieOptions.validateHttpsCertificates = false;
return execute({
context: this,
interval: this.fetchRetryInterval,
attempts: this.requestAttempts,
runnable(context) {
return UrlFetchApp.fetch(context.baseUrl + '/' + resource, authCookieOptions);
},
});
}
post(resource, options) {
const postOptions = Object.assign({}, options, {method: 'post'});
return this.fetch(resource, postOptions);
}
put(resource, options) {
const postOptions = Object.assign({}, options, {method: 'put'});
return this.fetch(resource, postOptions);
}
get(resource, options?: object, queryParams?: object) {
const queryString = this._createQueryString(queryParams);
return this.fetch(resource + queryString, options);
}
remove(resource, id) {
const deleteOptions = {method: 'delete'};
return this.fetch(id ? resource + '/' + id : resource, deleteOptions);
}
postJson(resource, data) {
const options = {
contentType: 'application/json',
payload: JSON.stringify(data),
};
return this.post(resource, options);
}
putJson(resource, data) {
const options = {
contentType: 'application/json',
payload: JSON.stringify(data),
};
return this.put(resource, options);
}
addAuthorization(options) {
return this.addHeader(options, 'Authorization', this.authorization);
}
addCookie(options) {
return this.addHeader(options, 'cookie', this.cookie);
}
addHeader(options, headerName, value) {
const copy = Object.assign({}, options);
if (value) {
const addedHeader = {};
addedHeader[headerName] = value;
copy.headers = Object.assign({}, copy.headers, addedHeader);
}
return copy;
}
_createQueryString(queryParams) {
const queryParamItems = [];
let queryString = '';
if (queryParams) {
Object.keys(queryParams).forEach(key => {
const value = queryParams[key];
if (value) {
queryParamItems.push([key, encodeURIComponent(value)].join('='));
}
});
if (queryParamItems) {
queryString += '?' + queryParamItems.join('&');
}
}
return queryString;
}
}