-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTP.js
214 lines (185 loc) · 4.7 KB
/
HTTP.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
/**
* Enum fetch methods.
*
* @enum {string}
*/
const METHODS = {
GET: "get",
PATCH: "patch",
POST: "post",
};
// A base class is defined using the new reserved 'class' keyword
class FetchOptions {
constructor() {
this.method = METHODS.GET;
this.apiVersion = 50;
}
setMethod(method) {
this.method = method;
return this;
}
setQueryParameters(queryParameters) {
this.queryParameters = queryParameters;
return this;
}
setPayload(payload) {
this.payload = payload;
return this;
}
setSObject(sObject) {
this.sObject = sObject;
return this;
}
setSObjectId(sObjectId) {
this.sObjectId = sObjectId;
return this;
}
setApiVersion(apiVersion) {
this.apiVersion = apiVersion;
return this;
}
}
// const test = () => {
// const t = new FetchOptions();
// // t.setMethod(METHODS.GET);
// console.log(t);
// }
const fetch_ = ({
batch = false,
method = METHODS.GET,
queryParameters = {},
payload = {},
sObject = "",
sObjectId = "",
apiVersion = 50,
}) => {
// const fetch_ = (config) => {
try {
// const endpoint = createEndpoint_({ method: config.method, sObject: config.sObject, sObjectId: config.sObjectId, apiVersion: config.apiVersion, queryParameters: config.queryParameters });
const endpoint = createEndpoint_({
batch,
method,
sObject,
sObjectId,
apiVersion,
queryParameters,
});
const accessTokenResponse = requestAccessToken_();
const accessToken = accessTokenResponse.access_token;
const instanceUrl = accessTokenResponse.instance_url;
const options = getOptions_({ accessToken, method, payload });
console.log(instanceUrl + endpoint);
let response = UrlFetchApp.fetch(instanceUrl + endpoint, options);
let json;
if (method === METHODS.GET) {
json = JSON.parse(response.toString());
let records = json["records"];
let nextUrl = json["nextRecordsUrl"];
while (!!nextUrl) {
fetchUrl = instanceUrl + nextUrl;
response = UrlFetchApp.fetch(fetchUrl, options);
json = JSON.parse(response.toString());
nextUrl = json["nextRecordsUrl"];
records = records.concat(json["records"]);
}
console.log(records);
return records;
} else {
if (Object.keys(response).length > 0) {
json = JSON.parse(response.toString());
const hasEntries = json.length > 0;
const hasErrorCode = hasEntries ? !!json[0].errorCode : false;
if (json.hasErrors || hasErrorCode) {
console.log("--- error1 ---");
console.log(JSON.stringify(json));
} else console.log(json);
return json;
} else {
console.log("Success!!!");
}
}
} catch (error) {
console.log("--- error2 ---");
console.log(error);
}
};
const createEndpoint_ = ({
batch,
method,
queryParameters,
sObject,
sObjectId,
apiVersion,
}) => {
let endpoint = URL_BEGINNING + apiVersion + ".0/";
switch (method) {
case METHODS.GET:
query = createSoqlQuery_(queryParameters);
endpoint += "query?q=" + encodeURIComponent(query);
break;
case METHODS.PATCH:
endpoint +=
(batch ? "composite/tree/" : "sobjects/") +
sObject +
"/" +
sObjectId +
".json";
break;
case METHODS.POST:
endpoint += (batch ? "composite/tree/" : "sobjects/") + sObject + "/";
break;
default:
endpoint += "";
}
return endpoint;
};
const getOptions_ = ({
payload,
accessToken,
method,
contentType = "application/json",
}) => {
const headers = {
Authorization: "Bearer " + accessToken,
};
const options = {
method: method,
contentType: contentType,
headers: headers,
muteHttpExceptions: true,
};
if (method === METHODS.PATCH || method === METHODS.POST) {
options.payload = JSON.stringify(payload);
}
return options;
};
/**
* Generates the SOQL query to send to Salesforce
*
* @param {object} queryParameters an object specifying query parameters
* must at least specify the SELECT and FROM clauses
* @return {string} the query string
*/
const createSoqlQuery_ = (queryParameters) => {
// Specifies the order of clauses
const clauses = [
"SELECT",
"FROM",
"WHERE",
"GROUP BY",
"ORDER BY",
"LIMIT",
"OFFSET",
"HAVING",
];
let query = "";
for (let clause of clauses) {
const prop = Helpers.fromTitleCaseToCamelCase(clause);
const parameter = queryParameters[prop];
if (query !== "") clause = " " + clause;
clause += " ";
// if a parameter is not included, it is skipped
query += Helpers.ifFalsy(parameter, "", clause + parameter);
}
return query;
};