/
bulk.go
352 lines (315 loc) · 11.8 KB
/
bulk.go
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package lib
import (
"encoding/xml"
"errors"
"fmt"
"github.com/ForceCLI/force/lib/internal"
"io"
"net/http"
"strings"
)
type BatchResult struct {
Results []Result
}
type BatchResultChunk struct {
HasCSVHeader bool
Data []byte
}
type BatchInfo struct {
Id string `xml:"id" json:"id"`
JobId string `xml:"jobId" json:"jobId"`
State string `xml:"state" json:"state"`
StateMessage string `xml:"stateMessage" json:"stateMessage"`
CreatedDate string `xml:"createdDate" json:"createdDate"`
SystemModstamp string `xml:"systemModstamp" json:"systemModstamp"`
NumberRecordsProcessed int `xml:"numberRecordsProcessed" json:"numberRecordsProcessed"`
NumberRecordsFailed int `xml:"numberRecordsFailed" json:"numberRecordsFailed"`
}
type JobInfo struct {
XMLName xml.Name `xml:"http://www.force.com/2009/06/asyncapi/dataload jobInfo"`
Id string `xml:"id,omitempty"`
Operation string `xml:"operation,omitempty"`
Object string `xml:"object,omitempty"`
ExternalIdFieldName string `xml:"externalIdFieldName,omitempty"`
CreatedById string `xml:"createdById,omitempty"`
CreatedDate string `xml:"createdDate,omitempty"`
SystemModStamp string `xml:"systemModstamp,omitempty"`
State string `xml:"state,omitempty"`
ConcurrencyMode string `xml:"concurrencyMode,omitempty"`
ContentType string `xml:"contentType,omitempty"`
NumberBatchesQueued int `xml:"numberBatchesQueued,omitempty"`
NumberBatchesInProgress int `xml:"numberBatchesInProgress,omitempty"`
NumberBatchesCompleted int `xml:"numberBatchesCompleted,omitempty"`
NumberBatchesFailed int `xml:"numberBatchesFailed,omitempty"`
NumberBatchesTotal int `xml:"numberBatchesTotal,omitempty"`
NumberRecordsProcessed int `xml:"numberRecordsProcessed,omitempty"`
NumberRetries int `xml:"numberRetries,omitempty"`
ApiVersion string `xml:"apiVersion,omitempty"`
NumberRecordsFailed int `xml:"numberRecordsFailed,omitempty"`
TotalProcessingTime int `xml:"totalProcessingTime,omitempty"`
ApiActiveProcessingTime int `xml:"apiActiveProcessingTime,omitempty"`
ApexProcessingTime int `xml:"apexProcessingTime,omitempty"`
}
func (ji JobInfo) JobContentType() (JobContentType, error) {
switch ji.ContentType {
case "JSON":
return JobContentTypeJson, nil
case "CSV":
return JobContentTypeCsv, nil
case "XML":
return JobContentTypeXml, nil
default:
return "", fmt.Errorf("Invalid content type for bulk API: " + ji.ContentType)
}
}
func (ji JobInfo) HttpContentType() (ContentType, error) {
jct, err := ji.JobContentType()
if err != nil {
return "", err
}
return httpContentTypeForJobContentType[jct], nil
}
type JobContentType string
const (
JobContentTypeCsv JobContentType = "CSV"
JobContentTypeXml JobContentType = "XML"
JobContentTypeJson JobContentType = "JSON"
)
var httpContentTypeForJobContentType = map[JobContentType]ContentType{
JobContentTypeCsv: ContentTypeCsv,
JobContentTypeXml: ContentTypeXml,
JobContentTypeJson: ContentTypeJson,
}
var httpResponseUnmarshalerForJobContentType = map[JobContentType]internal.Unmarshaler{
JobContentTypeCsv: internal.XmlUnmarshal,
JobContentTypeXml: internal.XmlUnmarshal,
JobContentTypeJson: internal.JsonUnmarshal,
}
var InvalidBulkObject = errors.New("Object Does Not Support Bulk API")
func (f *Force) CreateBulkJob(jobInfo JobInfo, requestOptions ...func(*http.Request)) (JobInfo, error) {
xmlbody, err := internal.XmlMarshal(jobInfo)
if err != nil {
return JobInfo{}, fmt.Errorf("Could not create job request: %s", err.Error())
}
url := fmt.Sprintf("%s/services/async/%s/job", f.Credentials.InstanceUrl, apiVersionNumber)
body, err := f.httpPostPatchWithRetry(url, string(xmlbody), ContentTypeXml, HttpMethodPost, requestOptions...)
if err != nil {
if fault, ok := err.(LoginFault); ok && fault.ExceptionCode == "InvalidEntity" {
return JobInfo{}, InvalidBulkObject
}
return JobInfo{}, err
}
var result JobInfo
if err := internal.XmlUnmarshal(body, &result); err != nil {
return result, err
}
return result, nil
}
func (f *Force) CloseBulkJob(jobId string) (JobInfo, error) {
jobInfo := JobInfo{
State: "Closed",
}
xmlbody, err := internal.XmlMarshal(jobInfo)
if err != nil {
return JobInfo{}, err
}
url := fmt.Sprintf("%s/services/async/%s/job/%s", f.Credentials.InstanceUrl, apiVersionNumber, jobId)
body, err := f.httpPostPatchWithRetry(url, string(xmlbody), ContentTypeXml, HttpMethodPost)
if err != nil {
return JobInfo{}, err
}
var result JobInfo
if err := internal.XmlUnmarshal(body, &result); err != nil {
return result, err
}
return result, nil
}
func (f *Force) GetBulkJobs() ([]JobInfo, error) {
url := fmt.Sprintf("%s/services/async/%s/jobs", f.Credentials.InstanceUrl, apiVersionNumber)
resp, err := f.httpGetBulk(url)
if err != nil {
return nil, err
}
var result []JobInfo
if err := internal.XmlUnmarshal(resp.ReadResponseBody, &result); err != nil {
return nil, err
}
return result, err
}
func (f *Force) httpGetBulk(url string) (*Response, error) {
req := NewRequest("GET").AbsoluteUrl(url).WithContent(ContentTypeXml).ReadResponseBody()
return f.ExecuteRequest(req)
}
func (f *Force) BulkQuery(soql string, jobId string, contentType string, requestOptions ...func(*http.Request)) (BatchInfo, error) {
url := fmt.Sprintf("%s/services/async/%s/job/%s/batch", f.Credentials.InstanceUrl, apiVersionNumber, jobId)
jct, err := JobInfo{ContentType: contentType}.JobContentType()
if err != nil {
return BatchInfo{}, err
}
httpCt := httpContentTypeForJobContentType[jct]
unmarshal := httpResponseUnmarshalerForJobContentType[jct]
body, err := f.httpPostPatch(url, soql, httpCt, HttpMethodPost, requestOptions...)
if err != nil {
return BatchInfo{}, err
}
var result BatchInfo
if err := unmarshal(body, &result); err != nil {
return BatchInfo{}, err
}
return result, nil
}
func (f *Force) AddBatchToJob(content string, job JobInfo) (BatchInfo, error) {
jct, err := job.JobContentType()
if err != nil {
return BatchInfo{}, err
}
url := fmt.Sprintf("%s/services/async/%s/job/%s/batch", f.Credentials.InstanceUrl, apiVersionNumber, job.Id)
body, err := f.httpPostPatch(url, content, httpContentTypeForJobContentType[jct], HttpMethodPost)
if err != nil {
return BatchInfo{}, err
}
unmarshal := httpResponseUnmarshalerForJobContentType[jct]
var result BatchInfo
if err := unmarshal(body, &result); err != nil {
return BatchInfo{}, err
}
return result, nil
}
func (f *Force) GetBatchInfo(jobId string, batchId string) (BatchInfo, error) {
url := fmt.Sprintf("%s/services/async/%s/job/%s/batch/%s", f.Credentials.InstanceUrl, apiVersionNumber, jobId, batchId)
resp, err := f.httpGetBulk(url)
if err != nil {
return BatchInfo{}, err
}
var unmarshal internal.Unmarshaler
if resp.ContentType == ContentTypeJson {
unmarshal = internal.JsonUnmarshal
} else {
unmarshal = internal.XmlUnmarshal
}
var result BatchInfo
if err := unmarshal(resp.ReadResponseBody, &result); err != nil {
return BatchInfo{}, err
}
return result, nil
}
func (f *Force) GetBatches(jobId string) (result []BatchInfo, err error) {
url := fmt.Sprintf("%s/services/async/%s/job/%s/batch", f.Credentials.InstanceUrl, apiVersionNumber, jobId)
resp, err := f.httpGetBulk(url)
if err != nil {
return nil, err
}
var batchInfoList struct {
BatchInfos []BatchInfo `xml:"batchInfo" json:"batchInfo"`
}
var unmarshal internal.Unmarshaler
if resp.ContentType == ContentTypeJson {
unmarshal = internal.JsonUnmarshal
} else {
unmarshal = internal.XmlUnmarshal
}
if err := unmarshal(resp.ReadResponseBody, &batchInfoList); err != nil {
return nil, err
}
return batchInfoList.BatchInfos, nil
}
func (f *Force) GetJobInfo(jobId string) (JobInfo, error) {
url := fmt.Sprintf("%s/services/async/%s/job/%s", f.Credentials.InstanceUrl, apiVersionNumber, jobId)
resp, err := f.httpGetBulk(url)
if err != nil {
return JobInfo{}, err
}
var result JobInfo
if err := internal.XmlUnmarshal(resp.ReadResponseBody, &result); err != nil {
return JobInfo{}, err
}
return result, nil
}
func (f *Force) RetrieveBulkQueryResultList(job JobInfo, batchId string) ([]byte, error) {
url := fmt.Sprintf("%s/services/async/%s/job/%s/batch/%s/result", f.Credentials.InstanceUrl, apiVersionNumber, job.Id, batchId)
ct, err := job.HttpContentType()
if err != nil {
return nil, err
}
body, err := f.makeHttpRequestSync(NewRequest("GET").AbsoluteUrl(url).WithContent(ct))
return body, err
}
func (f *Force) RetrieveBulkQuery(jobId string, batchId string) ([]byte, error) {
url := fmt.Sprintf("%s/services/async/%s/job/%s/batch/%s/result", f.Credentials.InstanceUrl, apiVersionNumber, jobId, batchId)
resp, err := f.httpGetBulk(url)
if err != nil {
return nil, err
}
return resp.ReadResponseBody, nil
}
func (f *Force) RetrieveBulkQueryResults(jobId string, batchId string, resultId string) ([]byte, error) {
url := fmt.Sprintf("%s/services/async/%s/job/%s/batch/%s/result/%s", f.Credentials.InstanceUrl, apiVersionNumber, jobId, batchId, resultId)
resp, err := f.httpGetBulk(url)
if err != nil {
return nil, err
}
return resp.ReadResponseBody, nil
}
func (f *Force) RetrieveBulkJobQueryResults(job JobInfo, batchId string, resultId string) ([]byte, error) {
url := fmt.Sprintf("%s/services/async/%s/job/%s/batch/%s/result/%s", f.Credentials.InstanceUrl, apiVersionNumber, job.Id, batchId, resultId)
ct, err := job.HttpContentType()
if err != nil {
return nil, err
}
body, err := f.makeHttpRequestSync(NewRequest("GET").AbsoluteUrl(url).WithContent(ct))
return body, err
}
func (f *Force) RetrieveBulkJobQueryResultsWithCallback(job JobInfo, batchId string, resultId string, callback HttpCallback) error {
url := fmt.Sprintf("%s/services/async/%s/job/%s/batch/%s/result/%s", f.Credentials.InstanceUrl, apiVersionNumber, job.Id, batchId, resultId)
ct, err := job.HttpContentType()
if err != nil {
return err
}
req := NewRequest("GET").AbsoluteUrl(url).WithContent(ct).WithResponseCallback(callback)
_, err = f.ExecuteRequest(req)
return err
}
// Deprecated: Use RetrieveBulkJobQueryResultsWithCallback
func (f *Force) RetrieveBulkJobQueryResultsAndSend(job JobInfo, batchId string, resultId string, results chan<- BatchResultChunk) error {
return f.RetrieveBulkJobQueryResultsWithCallback(job, batchId, resultId, NewBatchResultChannelHttpCallback(results, 0))
}
func (f *Force) RetrieveBulkBatchResults(jobId string, batchId string) (BatchResult, error) {
err := errors.New("this method was never working right, please report an issue in GitHub if you were using it")
return BatchResult{}, err
}
// NewBatchResultChannelHttpCallback returns a new reporter that will send chunks of a read body into
// the results channel. You can control the size of the chunks via bufferSize (defaults to 50mb).
func NewBatchResultChannelHttpCallback(results chan<- BatchResultChunk, bufferSize int) HttpCallback {
if bufferSize <= 1 {
bufferSize = 50 * 1024 * 1024
}
return (&batchResultChannelHttpCallback{results: results, bufferSize: bufferSize}).Report
}
type batchResultChannelHttpCallback struct {
bufferSize int
results chan<- BatchResultChunk
}
func (c *batchResultChannelHttpCallback) Report(res *http.Response) error {
defer res.Body.Close()
buf := make([]byte, c.bufferSize)
contentType := res.Header.Get("Content-Type")
firstChunk := true
isCSV := strings.Contains(contentType, "text/csv")
for {
n, err := io.ReadFull(res.Body, buf)
if n > 0 {
data := make([]byte, n)
copy(data, buf[:n])
c.results <- BatchResultChunk{
HasCSVHeader: firstChunk && isCSV,
Data: data,
}
}
if err == io.EOF || err == io.ErrUnexpectedEOF {
return nil
} else if err != nil {
return err
}
firstChunk = false
}
}