forked from doubledutch/go-force
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
300 lines (265 loc) · 11.5 KB
/
api.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
package force
import (
"errors"
"fmt"
"net/url"
)
const (
limitsKey = "limits"
queryKey = "query"
queryAllKey = "queryAll"
sObjectsKey = "sobjects"
sObjectKey = "sobject"
sObjectDescribeKey = "describe"
rowTemplateKey = "rowTemplate"
idKey = "{ID}"
resourcesUri = "/services/data/%v/"
versionsUri = "/services/data"
)
type ForceApiInterface interface {
BulkInsertSObjects(table string, in []SObject) ([]*SObjectResponse, error)
BulkUpdateSObjects(table string, in []SObject) ([]*SObjectResponse, error)
Delete(path string, params url.Values) error
DeleteSObject(id string, in SObject) (err error)
DeleteSObjectByExternalId(id string, in SObject) (err error)
DescribeSObject(in SObject) (resp *SObjectDescription, err error)
DescribeSObjects() (map[string]*SObjectMetaData, error)
Get(path string, params url.Values, out interface{}) error
GetAccessToken() string
GetInstanceURL() string
GetLimits() (limits *Limits, err error)
GetSObject(id string, fields []string, out SObject) (err error)
GetSObjectByExternalId(id string, fields []string, out SObject) (err error)
InsertSObject(in SObject) (resp *SObjectResponse, err error)
Patch(path string, params url.Values, payload, out interface{}) error
Post(path string, params url.Values, payload, out interface{}) error
Put(path string, params url.Values, payload, out interface{}) error
Query(query string, out interface{}) (err error)
QueryAll(query string, out interface{}) (err error)
QueryNext(uri string, out interface{}) (err error)
RefreshToken() error
TraceOff()
TraceOn(prefix string, logger ForceApiLogger)
UpdateSObject(id string, in SObject) (err error)
UpsertSObjectByExternalId(id string, in SObject) (resp *SObjectResponse, err error)
}
type ForceApi struct {
OAuth *ForceOauth
apiVersion string
apiVersions []*Version
apiResources map[string]string
apiSObjects map[string]*SObjectMetaData
apiSObjectDescriptions map[string]*SObjectDescription
apiMaxBatchSize int64
logger ForceApiLogger
logPrefix string
}
type Version struct {
Label string `json:"label"`
URL string `json:"url"`
Version string `json:"version"`
}
type RefreshTokenResponse struct {
ID string `json:"id"`
IssuedAt string `json:"issued_at"`
Signature string `json:"signature"`
AccessToken string `json:"access_token"`
}
type SObjectApiResponse struct {
Encoding string `json:"encoding"`
MaxBatchSize int64 `json:"maxBatchSize"`
SObjects []*SObjectMetaData `json:"sobjects"`
}
type SObjectMetaData struct {
Name string `json:"name"`
Label string `json:"label"`
KeyPrefix string `json:"keyPrefix"`
LabelPlural string `json:"labelPlural"`
Custom bool `json:"custom"`
Layoutable bool `json:"layoutable"`
Activateable bool `json:"activateable"`
URLs map[string]string `json:"urls"`
Searchable bool `json:"searchable"`
Updateable bool `json:"updateable"`
Createable bool `json:"createable"`
DeprecatedAndHidden bool `json:"deprecatedAndHidden"`
CustomSetting bool `json:"customSetting"`
Deletable bool `json:"deletable"`
FeedEnabled bool `json:"feedEnabled"`
Mergeable bool `json:"mergeable"`
Queryable bool `json:"queryable"`
Replicateable bool `json:"replicateable"`
Retrieveable bool `json:"retrieveable"`
Undeletable bool `json:"undeletable"`
Triggerable bool `json:"triggerable"`
}
type SObjectDescription struct {
Name string `json:"name"`
Fields []*SObjectField `json:"fields"`
KeyPrefix string `json:"keyPrefix"`
Layoutable bool `json:"layoutable"`
Activateable bool `json:"activateable"`
LabelPlural string `json:"labelPlural"`
Custom bool `json:"custom"`
CompactLayoutable bool `json:"compactLayoutable"`
Label string `json:"label"`
Searchable bool `json:"searchable"`
URLs map[string]string `json:"urls"`
Queryable bool `json:"queryable"`
Deletable bool `json:"deletable"`
Updateable bool `json:"updateable"`
Createable bool `json:"createable"`
CustomSetting bool `json:"customSetting"`
Undeletable bool `json:"undeletable"`
Mergeable bool `json:"mergeable"`
Replicateable bool `json:"replicateable"`
Triggerable bool `json:"triggerable"`
FeedEnabled bool `json:"feedEnabled"`
Retrievable bool `json:"retrieveable"`
SearchLayoutable bool `json:"searchLayoutable"`
LookupLayoutable bool `json:"lookupLayoutable"`
Listviewable bool `json:"listviewable"`
DeprecatedAndHidden bool `json:"deprecatedAndHidden"`
RecordTypeInfos []*RecordTypeInfo `json:"recordTypeInfos"`
ChildRelationsips []*ChildRelationship `json:"childRelationships"`
AllFields string `json:"-"` // Not from force.com API. Used to generate SELECT * queries.
}
type SObjectField struct {
Length float64 `json:"length"`
Name string `json:"name"`
Type string `json:"type"`
DefaultValue interface{} `json:"defaultValue"`
RestrictedPicklist bool `json:"restrictedPicklist"`
NameField bool `json:"nameField"`
ByteLength float64 `json:"byteLength"`
Precision float64 `json:"precision"`
Filterable bool `json:"filterable"`
Sortable bool `json:"sortable"`
Unique bool `json:"unique"`
CaseSensitive bool `json:"caseSensitive"`
Calculated bool `json:"calculated"`
Scale float64 `json:"scale"`
Label string `json:"label"`
NamePointing bool `json:"namePointing"`
Custom bool `json:"custom"`
HtmlFormatted bool `json:"htmlFormatted"`
DependentPicklist bool `json:"dependentPicklist"`
Permissionable bool `json:"permissionable"`
ReferenceTo []string `json:"referenceTo"`
RelationshipOrder float64 `json:"relationshipOrder"`
SoapType string `json:"soapType"`
CalculatedValueFormula string `json:"calculatedValueFormula"`
DefaultValueFormula string `json:"defaultValueFormula"`
DefaultedOnCreate bool `json:"defaultedOnCreate"`
Digits float64 `json:"digits"`
Groupable bool `json:"groupable"`
Nillable bool `json:"nillable"`
InlineHelpText string `json:"inlineHelpText"`
WriteRequiresMasterRead bool `json:"writeRequiresMasterRead"`
PicklistValues []*PicklistValue `json:"picklistValues"`
Updateable bool `json:"updateable"`
Createable bool `json:"createable"`
DeprecatedAndHidden bool `json:"deprecatedAndHidden"`
DisplayLocationInDecimal bool `json:"displayLocationInDecimal"`
CascadeDelete bool `json:"cascasdeDelete"`
RestrictedDelete bool `json:"restrictedDelete"`
ControllerName string `json:"controllerName"`
ExternalId bool `json:"externalId"`
IdLookup bool `json:"idLookup"`
AutoNumber bool `json:"autoNumber"`
RelationshipName string `json:"relationshipName"`
}
type PicklistValue struct {
Value string `json:"value"`
DefaulValue bool `json:"defaultValue"`
ValidFor string `json:"validFor"`
Active bool `json:"active"`
Label string `json:"label"`
}
type RecordTypeInfo struct {
Name string `json:"name"`
Available bool `json:"available"`
RecordTypeId string `json:"recordTypeId"`
URLs map[string]string `json:"urls"`
DefaultRecordTypeMapping bool `json:"defaultRecordTypeMapping"`
}
type ChildRelationship struct {
Field string `json:"field"`
ChildSObject string `json:"childSObject"`
DeprecatedAndHidden bool `json:"deprecatedAndHidden"`
CascadeDelete bool `json:"cascadeDelete"`
RestrictedDelete bool `json:"restrictedDelete"`
RelationshipName string `json:"relationshipName"`
}
func (forceApi *ForceApi) GetApiSObjectDescription(name string) (*SObjectDescription, error) {
if desc, ok := forceApi.apiSObjectDescriptions[name]; ok {
return desc, nil
} else {
if sObject, ok := forceApi.apiSObjects[name]; ok {
uri := sObject.URLs[sObjectDescribeKey]
desc := &SObjectDescription{}
err := forceApi.Get(uri, nil, desc)
if err != nil {
return nil, err
}
forceApi.apiSObjectDescriptions[name] = desc
return desc, nil
} else {
return nil, errors.New("Not found")
}
}
}
func (forceApi *ForceApi) getApiVersions() error {
return forceApi.Get(versionsUri, nil, &forceApi.apiVersions)
}
func (forceApi *ForceApi) getApiResources() error {
uri := fmt.Sprintf(resourcesUri, forceApi.apiVersion)
return forceApi.Get(uri, nil, &forceApi.apiResources)
}
func (forceApi *ForceApi) getApiSObjects() error {
uri := forceApi.apiResources[sObjectsKey]
list := &SObjectApiResponse{}
err := forceApi.Get(uri, nil, list)
if err != nil {
return err
}
forceApi.apiMaxBatchSize = list.MaxBatchSize
// The API doesn't return the list of sobjects in a map. Convert it.
for _, object := range list.SObjects {
forceApi.apiSObjects[object.Name] = object
}
return nil
}
func (forceApi *ForceApi) getApiSObjectDescriptions() error {
for name, metaData := range forceApi.apiSObjects {
uri := metaData.URLs[sObjectDescribeKey]
desc := &SObjectDescription{}
err := forceApi.Get(uri, nil, desc)
if err != nil {
return err
}
forceApi.apiSObjectDescriptions[name] = desc
}
return nil
}
func (forceApi *ForceApi) GetInstanceURL() string {
return forceApi.OAuth.InstanceUrl
}
func (forceApi *ForceApi) GetAccessToken() string {
return forceApi.OAuth.AccessToken
}
func (forceApi *ForceApi) RefreshToken() error {
res := &RefreshTokenResponse{}
payload := map[string]string{
"grant_type": "refresh_token",
"refresh_token": forceApi.OAuth.refreshToken,
"client_id": forceApi.OAuth.clientId,
"client_secret": forceApi.OAuth.clientSecret,
}
err := forceApi.Post("/services/oauth2/token", nil, payload, res)
if err != nil {
return err
}
forceApi.OAuth.AccessToken = res.AccessToken
return nil
}