-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
280 lines (248 loc) · 8.97 KB
/
index.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env node
'use strict';
const axios = require('axios');
const querystring = require('querystring');
/*
Provides most of the functionality of the Salesforce Bulk API 2.0.
https://developer.salesforce.com/docs/atlas.en-us.api_bulk_v2.meta/api_bulk_v2/introduction_bulk_api_2.htm
Each instance of BulkApi is meant to work with a single Job. To work with a
different job, create a new BulkApi instance.
*/
class BulkApi {
constructor(options) {
/*
Required options.
*/
if (!options.url) throw new Error('options.url required');
if (!options.username) throw new Error('options.username required');
if (!options.password) throw new Error('options.password required');
if (!options.token) throw new Error('options.token required');
if (!options.apiVersion) throw new Error('options.apiVersion required');
if (!options.consumerKey) throw new Error('options.consumerKey required');
if (!options.consumerSecret) throw new Error('options.consumerSecret required');
this.options = options;
/*
The following are needed to create a job.
*/
this.options.object = options.object || null;
// operation options: insert, upsert, update, delete, query, queryAll
this.options.operation = options.operation || null;
// externalIdFieldName only needed for upsert jobs.
this.options.externalIdFieldName = options.externalIdFieldName || null;
/*
The following are set on login/getUrl.
*/
this.authToken = null;
this.idUrl = null;
this.url = null;
/*
jobId is set on createJob.
*/
this.jobId = null;
}
async uploadJobData(data) {
await this.createJob();
await axios.put(
`${this.url}/services/data/v${this.options.apiVersion}/jobs/ingest/${this.jobId}/batches`,
data, {
headers: {
authorization: `Bearer ${this.authToken}`,
'Content-Type': 'text/csv'
}
});
}
async login() {
if (!this.authToken) {
const body = querystring.stringify({
grant_type: 'password',
client_id: this.options.consumerKey,
client_secret: this.options.consumerSecret,
username: this.options.username,
password: this.options.password + this.options.token
});
const response = await axios.post(
`${this.options.url}/services/oauth2/token`, body);
this.authToken = response.data.access_token;
this.idUrl = response.data.id;
}
}
async getUrl() {
if (!this.url) {
await this.login();
const response = await axios.get(
this.idUrl, {
headers: {
authorization: `Bearer ${this.authToken}`
}
});
this.url = response.data.urls.profile;
this.url = this.url.substring(0, this.url.lastIndexOf('/'));
}
}
async createJob() {
await this.getUrl();
const body = {
object: this.options.object,
operation: this.options.operation
};
if (this.options.externalIdFieldName) {
body.externalIdFieldName = this.options.externalIdFieldName;
}
const response = await axios.post(
`${this.url}/services/data/v${this.options.apiVersion}/jobs/ingest`,
body, {
headers: {
authorization: `Bearer ${this.authToken}`
}
});
this.jobId = response.data.id;
}
async getJobInfo(id) {
id = id || this.jobId;
await this.getUrl();
const response = await axios.get(
`${this.url}/services/data/v${this.options.apiVersion}/jobs/ingest/${id}`, {
headers: {
authorization: `Bearer ${this.authToken}`
}
});
return response.data;
}
async abortJob(id) {
return await this.abortComplete(id, 'Aborted');
}
async closeJob(id) {
return await this.abortComplete(id, 'UploadComplete');
}
async abortComplete(id, myState) {
id = id || this.jobId;
await this.getUrl();
const body = {
state: myState
};
const response = await axios.patch(
`${this.url}/services/data/v${this.options.apiVersion}/jobs/ingest/${id}`,
body, {
headers: {
authorization: `Bearer ${this.authToken}`,
Accept: 'application/json'
}
});
return response.data;
}
async deleteJob(id) {
id = id || this.jobId;
await this.getUrl();
await axios.delete(
`${this.url}/services/data/v${this.options.apiVersion}/jobs/ingest/${id}`, {
headers: {
authorization: `Bearer ${this.authToken}`
}
});
}
async getSuccessfulResults(id) {
return await this.getResults(id, 'successfulResults');
}
async getFailedResults(id) {
return await this.getResults(id, 'failedResults');
}
async getUnprocessedRecords(id) {
return await this.getResults(id, 'unprocessedrecords');
}
async getResults(id, resultType) {
id = id || this.jobId;
await this.getUrl();
return axios.get(
`${this.url}/services/data/v${this.options.apiVersion}/jobs/ingest/${id}/${resultType}/`, {
responseType: 'stream',
headers: {
authorization: `Bearer ${this.authToken}`
}
});
}
}
exports = module.exports = BulkApi;
// ## OAUTH
// https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_username_password_oauth_flow.htm
// ### Get Access Token
// ```bash
// curl -X POST \
// ${BULK2_URL}/services/oauth2/token \
// -H 'content-type: application/x-www-form-urlencoded' \
// --data-urlencode "grant_type=password" \
// --data-urlencode "client_id=${BULK2_CONSUMER_KEY}" \
// --data-urlencode "client_secret=${BULK2_CONSUMER_SECRET}" \
// --data-urlencode "username=${BULK2_USERNAME}" \
// --data-urlencode "password=${BULK2_PASSWORD}${BULK2_TOKEN}"
// {
// "access_token": "***",
// "id": "https://login.salesforce.com/id/00D36000000arMLEAY/00536000000IigRAAS",
// "instance_url": "https://na30.salesforce.com",
// "issued_at": "1511895199410",
// "signature": "mbYmf6mq3OcLltEtSgKLNDiJwZhbmB3S7xPf3GGbqp4=",
// "token_type": "Bearer"
// }
// ```
// ### Get URL for REST Calls
// The following shows how to get the URL for REST calls. Really just need the
// https://na30.salesforce.com bit out of one of the URLs below.
// ```bash
// curl -X GET \
// https://login.salesforce.com/id/00D36000000arMLEAY/00536000000IigRAAS \
// -H 'authorization: Bearer ***'
// {
// "active": true,
// "addr_city": null,
// "addr_country": "US",
// "addr_state": null,
// "addr_street": null,
// "addr_zip": null,
// "asserted_user": true,
// "display_name": "Pipe Dream",
// "email": "sfadmin@candoris.com",
// "email_verified": true,
// "first_name": "Pipe",
// "id": "https://login.salesforce.com/id/00D36000000arMLEAY/00536000000IigRAAS",
// "is_app_installed": true,
// "is_lightning_login_user": false,
// "language": "en_US",
// "last_modified_date": "2016-12-09T04:02:56.000+0000",
// "last_name": "Dream",
// "locale": "en_US",
// "mobile_phone": null,
// "mobile_phone_verified": false,
// "nick_name": "pipedream1.457990354178172E12",
// "organization_id": "00D36000000arMLEAY",
// "photos": {
// "picture": "https://c.na30.content.force.com/profilephoto/005/F",
// "thumbnail": "https://c.na30.content.force.com/profilephoto/005/T"
// },
// "status": {
// "body": null,
// "created_date": null
// },
// "timezone": "America/Los_Angeles",
// "urls": {
// "enterprise": "https://na30.salesforce.com/services/Soap/c/{version}/00D36000000arML",
// "feed_elements": "https://na30.salesforce.com/services/data/v{version}/chatter/feed-elements",
// "feed_items": "https://na30.salesforce.com/services/data/v{version}/chatter/feed-items",
// "feeds": "https://na30.salesforce.com/services/data/v{version}/chatter/feeds",
// "groups": "https://na30.salesforce.com/services/data/v{version}/chatter/groups",
// "metadata": "https://na30.salesforce.com/services/Soap/m/{version}/00D36000000arML",
// "partner": "https://na30.salesforce.com/services/Soap/u/{version}/00D36000000arML",
// "profile": "https://na30.salesforce.com/00536000000IigRAAS",
// "query": "https://na30.salesforce.com/services/data/v{version}/query/",
// "recent": "https://na30.salesforce.com/services/data/v{version}/recent/",
// "rest": "https://na30.salesforce.com/services/data/v{version}/",
// "search": "https://na30.salesforce.com/services/data/v{version}/search/",
// "sobjects": "https://na30.salesforce.com/services/data/v{version}/sobjects/",
// "tooling_rest": "https://na30.salesforce.com/services/data/v{version}/tooling/",
// "tooling_soap": "https://na30.salesforce.com/services/Soap/T/{version}/00D36000000arML",
// "users": "https://na30.salesforce.com/services/data/v{version}/chatter/users"
// },
// "user_id": "00536000000IigRAAS",
// "user_type": "STANDARD",
// "username": "pipedream@candoris.com",
// "utcOffset": -28800000
// }
// ```