-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathiterator.go
84 lines (73 loc) · 1.81 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
81
82
83
84
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
config Configuration
}
func newBulkJobQueryIterator(sf *Salesforce, bulkJobId string) (*bulkJobQueryIterator, error) {
pollErr := waitForJobResults(sf, bulkJobId, queryJobType, (time.Second / 2))
if pollErr != nil {
return nil, pollErr
}
return &bulkJobQueryIterator{
auth: sf.auth,
uri: "/jobs/query/" + bulkJobId + "/results",
config: sf.Config,
}, 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, compress: it.config.CompressionHeaders})
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]
} else {
it.Locator = ""
}
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
}