-
Notifications
You must be signed in to change notification settings - Fork 6
/
query.go
62 lines (52 loc) · 1.49 KB
/
query.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
package salesforce
import (
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"github.com/go-viper/mapstructure/v2"
)
type queryResponse struct {
TotalSize int `json:"totalSize"`
Done bool `json:"done"`
NextRecordsUrl string `json:"nextRecordsUrl"`
Records []map[string]any `json:"records"`
}
func performQuery(auth *authentication, query string, sObject any) error {
query = url.QueryEscape(query)
queryResp := &queryResponse{
Done: false,
NextRecordsUrl: "/query/?q=" + query,
}
for !queryResp.Done {
resp, err := doRequest(auth, requestPayload{
method: http.MethodGet,
uri: queryResp.NextRecordsUrl,
content: jsonType,
})
if err != nil {
return err
}
respBody, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return readErr
}
tempQueryResp := &queryResponse{}
queryResponseError := json.Unmarshal(respBody, &tempQueryResp)
if queryResponseError != nil {
return queryResponseError
}
queryResp.TotalSize = queryResp.TotalSize + tempQueryResp.TotalSize
queryResp.Records = append(queryResp.Records, tempQueryResp.Records...)
queryResp.Done = tempQueryResp.Done
if !tempQueryResp.Done && tempQueryResp.NextRecordsUrl != "" {
queryResp.NextRecordsUrl = strings.TrimPrefix(tempQueryResp.NextRecordsUrl, "/services/data/"+apiVersion)
}
}
sObjectError := mapstructure.Decode(queryResp.Records, sObject)
if sObjectError != nil {
return sObjectError
}
return nil
}