forked from nimajalali/go-force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
force.go
164 lines (141 loc) · 4.27 KB
/
force.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
// A Go package that provides bindings to the force.com REST API
//
// See http://www.salesforce.com/us/developer/docs/api_rest/
package force
import (
"fmt"
"os"
)
const (
testVersion = "v36.0"
testClientId = "3MVG9A2kN3Bn17hs8MIaQx1voVGy662rXlC37svtmLmt6wO_iik8Hnk3DlcYjKRvzVNGWLFlGRH1ryHwS217h"
testClientSecret = "4165772184959202901"
testUserName = "go-force@jalali.net"
testPassword = "golangrocks3"
testSecurityToken = "kAlicVmti9nWRKRiWG3Zvqtte"
testEnvironment = "production"
)
func Create(version, clientId, clientSecret, userName, password, securityToken,
environment string) (*ForceApi, error) {
oauth := &forceOauth{
clientId: clientId,
clientSecret: clientSecret,
userName: userName,
password: password,
securityToken: securityToken,
environment: environment,
}
forceApi := &ForceApi{
apiResources: make(map[string]string),
apiSObjects: make(map[string]*SObjectMetaData),
apiSObjectDescriptions: make(map[string]*SObjectDescription),
apiVersion: version,
oauth: oauth,
}
// Init oauth
err := forceApi.oauth.Authenticate()
if err != nil {
return nil, err
}
// Init Api Resources
err = forceApi.getApiResources()
if err != nil {
return nil, err
}
err = forceApi.getApiSObjects()
if err != nil {
return nil, err
}
return forceApi, nil
}
func CreateWithAccessToken(version, clientId, accessToken, instanceUrl string) (*ForceApi, error) {
oauth := &forceOauth{
clientId: clientId,
AccessToken: accessToken,
InstanceUrl: instanceUrl,
}
forceApi := &ForceApi{
apiResources: make(map[string]string),
apiSObjects: make(map[string]*SObjectMetaData),
apiSObjectDescriptions: make(map[string]*SObjectDescription),
apiVersion: version,
oauth: oauth,
}
// We need to check for oath correctness here, since we are not generating the token ourselves.
if err := forceApi.oauth.Validate(); err != nil {
return nil, err
}
// Init Api Resources
err := forceApi.getApiResources()
if err != nil {
return nil, err
}
err = forceApi.getApiSObjects()
if err != nil {
return nil, err
}
return forceApi, nil
}
func CreateWithRefreshToken(version, clientId, accessToken, instanceUrl string) (*ForceApi, error) {
oauth := &forceOauth{
clientId: clientId,
AccessToken: accessToken,
InstanceUrl: instanceUrl,
}
forceApi := &ForceApi{
apiResources: make(map[string]string),
apiSObjects: make(map[string]*SObjectMetaData),
apiSObjectDescriptions: make(map[string]*SObjectDescription),
apiVersion: version,
oauth: oauth,
}
// obtain access token
if err := forceApi.RefreshToken(); err != nil {
return nil, err
}
// We need to check for oath correctness here, since we are not generating the token ourselves.
if err := forceApi.oauth.Validate(); err != nil {
return nil, err
}
// Init Api Resources
err := forceApi.getApiResources()
if err != nil {
return nil, err
}
err = forceApi.getApiSObjects()
if err != nil {
return nil, err
}
return forceApi, nil
}
// Used when running tests.
func createTest() *ForceApi {
forceApi, err := Create(testVersion, testClientId, testClientSecret, testUserName, testPassword, testSecurityToken, testEnvironment)
if err != nil {
fmt.Printf("Unable to create ForceApi for test: %v", err)
os.Exit(1)
}
return forceApi
}
type ForceApiLogger interface {
Printf(format string, v ...interface{})
}
// TraceOn turns on logging for this ForceApi. After this is called, all
// requests, responses, and raw response bodies will be sent to the logger.
// If prefix is a non-empty string, it will be written to the front of all
// logged strings, which can aid in filtering log lines.
//
// Use TraceOn if you want to spy on the ForceApi requests and responses.
//
// Note that the base log.Logger type satisfies ForceApiLogger, but adapters
// can easily be written for other logging packages (e.g., the
// golang-sanctioned glog framework).
func (forceApi *ForceApi) TraceOn(prefix string, logger ForceApiLogger) {
forceApi.logPrefix = prefix
forceApi.logger = logger
}
// TraceOff turns off tracing. It is idempotent.
func (forceApi *ForceApi) TraceOff() {
forceApi.logger = nil
forceApi.logPrefix = ""
}