-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBatchRequests.js
250 lines (237 loc) · 8.55 KB
/
BatchRequests.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* GitHub https://github.com/tanaikech/BatchRequest<br>
* Run BatchRequest<br>
* @param {Object} Object Object
* @return {Object} Return Object
*/
function Do(object) {
return new BatchRequest(object).Do();
}
/**
* Run enhanced "Do" method of BatchRequest. Requests more than 100 can be used and the result values are parsed.<br>
* @param {Object} Object Object
* @return {Object} Return Object
*/
function EDo(object) {
return new BatchRequest(object).EDo();
}
/**
* Get batch path for using batch requests. On August 12, 2020, in order to use batch requests, the batch path is required to be used to the endpoint of the batch requests..<br>
* @param {string} name Name of Google API you want to use. For example, when you want to use Drive API, please put "drive".
* @param {string} version Version of Google API you want to use. For example, when you want to use Drive API v2, please put "v2". When this is not used, the latest version is used as the default.
* @return {Object} Return Object
*/
function getBatchPath(name, version) {
return new BatchRequest("getBatchPath").getBatchPath(name, version);
}
;
(function(r) {
var BatchRequest;
BatchRequest = (function() {
var createRequest, parser, parserAsBinary, splitByteArrayBySearchData;
BatchRequest.name = "BatchRequest";
function BatchRequest(p_) {
var bP, batchPath;
if (typeof p_ === "object") {
if (!p_.hasOwnProperty("requests")) {
throw new Error("'requests' property was not found in object.");
}
this.p = p_.requests.slice();
this.url = "https://www.googleapis.com/batch";
if (p_.batchPath) {
bP = p_.batchPath.trim();
batchPath = "";
if (~bP.indexOf("batch/")) {
batchPath = bP.replace("batch", "");
} else {
batchPath = bP.slice(0, 1) === "/" ? bP : "/" + bP;
}
this.url += batchPath;
}
this.at = p_.accessToken || ScriptApp.getOAuthToken();
this.lb = "\r\n";
this.boundary = "xxxxxxxxxx";
this.useFetchAll = "useFetchAll" in p_ ? p_.useFetchAll : false;
this.exportDataAsBlob = "exportDataAsBlob" in p_ ? p_.exportDataAsBlob : false;
}
}
BatchRequest.prototype.Do = function() {
var e, params, res;
try {
params = createRequest.call(this, this.p);
res = UrlFetchApp.fetch(this.url, params);
} catch (error) {
e = error;
throw new Error(e);
}
return res;
};
BatchRequest.prototype.EDo = function() {
var e, i, k, l, limit, obj, params, ref, ref1, reqs, res, split;
try {
if (this.useFetchAll) {
limit = 100;
split = Math.ceil(this.p.length / limit);
reqs = [];
for (i = k = 0, ref = split; 0 <= ref ? k < ref : k > ref; i = 0 <= ref ? ++k : --k) {
params = createRequest.call(this, this.p.splice(0, limit));
params.url = this.url;
reqs.push(params);
}
r = UrlFetchApp.fetchAll(reqs);
res = r.reduce(function(ar, e) {
var obj;
if (e.getResponseCode() !== 200) {
ar.push(e.getContentText());
} else {
obj = this.exportDataAsBlob ? parserAsBinary.call(this, e) : parser.call(this, e.getContentText());
ar = ar.concat(obj);
}
return ar;
}, []);
} else {
limit = 100;
split = Math.ceil(this.p.length / limit);
res = [];
for (i = l = 0, ref1 = split; 0 <= ref1 ? l < ref1 : l > ref1; i = 0 <= ref1 ? ++l : --l) {
params = createRequest.call(this, this.p.splice(0, limit));
r = UrlFetchApp.fetch(this.url, params);
if (r.getResponseCode() !== 200) {
res.push(r.getContentText());
} else {
obj = this.exportDataAsBlob ? parserAsBinary.call(this, r) : parser.call(this, r.getContentText());
res = res.concat(obj);
}
}
}
} catch (error) {
e = error;
throw new Error(e);
}
return res;
};
BatchRequest.prototype.getBatchPath = function(name, version) {
var batchPath, discoveryRestUrl, obj1, obj2, res1, res2, url;
version = version === void 0 ? "" : version;
if (!name) {
throw new Error("Please set API name you want to search.");
}
url = "https://www.googleapis.com/discovery/v1/apis?preferred=" + (version ? "false" : "true") + "&name=" + encodeURIComponent(name.toLowerCase());
res1 = UrlFetchApp.fetch(url, {
muteHttpExceptions: true
});
if (res1.getResponseCode() !== 200) {
throw new Error("Batch path cannot be found.");
}
obj1 = JSON.parse(res1.getContentText());
if (!obj1.items) {
throw new Error("Batch path cannot be found.");
}
discoveryRestUrl = ((version.toString() === "" ? obj1.items[0] : (obj1.items.filter(function(e) {
return e.version === version;
}))[0]) || {}).discoveryRestUrl;
if (!discoveryRestUrl) {
throw new Error("Batch path cannot be found.");
}
res2 = UrlFetchApp.fetch(discoveryRestUrl, {
muteHttpExceptions: true
});
if (res2.getResponseCode() !== 200) {
throw new Error("Batch path cannot be found.");
}
obj2 = JSON.parse(res2);
batchPath = obj2.batchPath;
return batchPath;
};
parser = function(d_) {
var regex, temp;
temp = d_.split("--batch");
regex = /{[\S\s]+}/g;
if (!d_.match(regex)) {
return d_;
}
return temp.slice(1, temp.length - 1).map(function(e) {
if (regex.test(e)) {
return JSON.parse(e.match(regex)[0]);
}
return e;
});
};
splitByteArrayBySearchData = function(baseData_, searchData_) {
var bLen, idx, res, search;
search = searchData_.join("");
bLen = searchData_.length;
res = [];
idx = 0;
while (idx !== -1) {
idx = baseData_.findIndex(function(_, i, a) {
return (Array(bLen).fill(null).map(function(_, j) {
return a[j + i];
})).join("") === search;
});
if (idx !== -1) {
res.push(baseData_.splice(0, idx));
baseData_.splice(0, bLen);
} else {
res.push(baseData_.splice(0));
}
}
return res;
};
parserAsBinary = function(d_) {
var baseData, blobs, check, res1, search, searchData;
check = d_.getContentText().match(/--batch.*/);
if (!check) {
throw new Error("Valid response value is not returned.");
}
search = check[0];
baseData = d_.getContent();
searchData = Utilities.newBlob(search).getBytes();
res1 = splitByteArrayBySearchData.call(this, baseData, searchData);
res1.shift();
res1.pop();
blobs = res1.map(function(e, i) {
var data, dataSize, metadata, rrr;
rrr = splitByteArrayBySearchData.call(this, e, [13, 10, 13, 10]);
data = rrr.pop();
metadata = Utilities.newBlob(rrr.flat()).getDataAsString();
dataSize = Number(metadata.match(/Content-Length:(.*)/)[1]);
return Utilities.newBlob(data.splice(0, dataSize)).setName("blob" + (i + 1));
});
return blobs;
};
createRequest = function(d_) {
var contentId, data, e, params;
try {
contentId = 0;
data = "--" + this.boundary + this.lb;
d_.forEach((function(_this) {
return function(e) {
data += "Content-Type: application/http" + _this.lb;
data += "Content-ID: " + ++contentId + _this.lb + _this.lb;
data += e.method + " " + e.endpoint + _this.lb;
data += e.accessToken ? "Authorization: Bearer " + e.accessToken + _this.lb : "";
data += e.requestBody ? "Content-Type: application/json; charset=utf-8" + _this.lb + _this.lb : _this.lb;
data += e.requestBody ? JSON.stringify(e.requestBody) + _this.lb : "";
return data += "--" + _this.boundary + _this.lb;
};
})(this));
params = {
muteHttpExceptions: true,
method: "post",
contentType: "multipart/mixed; boundary=" + this.boundary,
payload: Utilities.newBlob(data).getBytes(),
headers: {
Authorization: 'Bearer ' + this.at
}
};
} catch (error) {
e = error;
throw new Error(e);
}
return params;
};
return BatchRequest;
})();
return r.BatchRequest = BatchRequest;
})(this);