-
Notifications
You must be signed in to change notification settings - Fork 6
/
iterator.go
80 lines (69 loc) · 1.7 KB
/
iterator.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
package salesforce
import (
"encoding/csv"
"fmt"
"io"
"net/http"
"strconv"
"time"
"github.com/jszwec/csvutil"
)
type IteratorJob interface {
Next() bool
Error() error
Decode(any) error
}
type bulkJobQueryIterator struct {
NumberOfRecords int `json:"Sforce-Numberofrecords"`
Locator string `json:"Sforce-Locator"`
auth *authentication
uri string
err error
reader io.ReadCloser
}
func newBulkJobQueryIterator(auth *authentication, bulkJobId string) (*bulkJobQueryIterator, error) {
pollErr := waitForJobResults(auth, bulkJobId, queryJobType, (time.Second / 2))
if pollErr != nil {
return nil, pollErr
}
return &bulkJobQueryIterator{
auth: auth,
uri: "/jobs/query/" + bulkJobId + "/results",
}, nil
}
func (it *bulkJobQueryIterator) Next() bool {
if it.reader != nil {
it.err = it.reader.Close()
if it.Locator == "" {
return false
}
}
uri := it.uri
if it.Locator != "" {
uri += "/?locator=" + it.Locator
}
resp, err := doRequest(it.auth, requestPayload{method: http.MethodGet, uri: uri, content: jsonType})
if err != nil {
it.err = err
return false
}
it.reader = resp.Body
it.NumberOfRecords, _ = strconv.Atoi(resp.Header["Sforce-Numberofrecords"][0])
if resp.Header["Sforce-Locator"][0] != "null" {
it.Locator = resp.Header["Sforce-Locator"][0]
}
return true
}
func (it *bulkJobQueryIterator) Decode(val any) error {
dec, err := csvutil.NewDecoder(csv.NewReader(it.reader))
if err != nil {
return fmt.Errorf("NewDecoder: %w", err)
}
if err := dec.Decode(val); err != nil && err != io.EOF {
return fmt.Errorf("Decode: %w", err)
}
return nil
}
func (it *bulkJobQueryIterator) Error() error {
return it.err
}