-
Notifications
You must be signed in to change notification settings - Fork 69
/
client.go
240 lines (201 loc) · 6.76 KB
/
client.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
/*
Copyright 2022 TriggerMesh Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strconv"
"sync"
"github.com/triggermesh/triggermesh/pkg/targets/adapter/salesforcetarget/auth"
"go.uber.org/zap"
)
const (
// base URL for Salesforce Lighting Platform
salesforceAPIData = "/services/data/"
)
// salesforceVersion is returned from Salesforce when
// successfuly querying the data services endpoint.
type salesforceVersion struct {
Version string
Label string
URL string
}
// SalesforceClient is the implementation of the Salesforce client
type SalesforceClient struct {
auth *auth.JWTAuthenticator
creds *auth.Credentials
apiVersion string
servicesDataPath string
client *http.Client
logger *zap.SugaredLogger
mutex sync.RWMutex
}
// Options for the Salesforce client
type Options func(*SalesforceClient)
// New creates a default Salesforce API client.
func New(authenticator *auth.JWTAuthenticator, logger *zap.SugaredLogger, opts ...Options) *SalesforceClient {
sfc := &SalesforceClient{
auth: authenticator,
logger: logger,
}
for _, opt := range opts {
opt(sfc)
}
if sfc.client == nil {
sfc.client = http.DefaultClient
}
return sfc
}
// WithAPIVersion sets a specific API version at the Salesforce client. If
// version is an empty string the client will choose latest upon authentication.
func WithAPIVersion(version string) Options {
return func(c *SalesforceClient) {
c.apiVersion = version
}
}
// WithHTTPClient sets the HTTP client to be used.
func WithHTTPClient(httpClient *http.Client) Options {
return func(c *SalesforceClient) {
c.client = httpClient
}
}
// Authenticate and performs checks regarding
// the Salesforce version
func (c *SalesforceClient) Authenticate(ctx context.Context) error {
creds, err := c.auth.CreateOrRenewCredentials()
c.mutex.Lock()
defer c.mutex.Unlock()
if err != nil {
// If there was an error with refresh, let's try to
// use a new access token next time
c.creds = nil
return fmt.Errorf("could not perform authentication: %w", err)
}
c.creds = creds
// If version is hardcoded
if c.apiVersion != "" {
c.servicesDataPath = path.Join(salesforceAPIData, c.apiVersion)
return nil
}
// we already have the lock
res, err := c.doCall(ctx, http.MethodGet, salesforceAPIData, nil, nil)
if err != nil {
return fmt.Errorf("could not retrieve available versions: %w", err)
}
if res.StatusCode != 200 {
return c.manageAPIError(res)
}
var versions []salesforceVersion
err = json.NewDecoder(res.Body).Decode(&versions)
if err != nil {
return fmt.Errorf("cannot decode Salesforce versions: %w", err)
}
var maxV float64 = 0
for _, v := range versions {
i, err := strconv.ParseFloat(v.Version, 64)
if err != nil {
// ignore errors and parse next version
continue
}
if i > maxV {
maxV = i
c.servicesDataPath = v.URL
}
}
return nil
}
// Do method will use the Salesforce API using the passed parameters
// adding only authentication header and the host. It is the caller responsability
// to add all toher elements to the call.
//
// This can be useful when a previous API call returned an URL that contains the
// full path to an element or a pagination.
func (c *SalesforceClient) Do(ctx context.Context, sfr SalesforceAPIRequest) (*http.Response, error) {
return c.doRetriableCall(ctx, string(sfr.Action), path.Join(c.servicesDataPath, sfr.ResourcePath, sfr.ObjectPath, sfr.RecordPath), sfr.Query, []byte(sfr.Payload))
}
// doRetriableCall executes a Salesforce API call in a thread safe manner. If authentication fails
// the first time, it will authenticate and retry a second time.
func (c *SalesforceClient) doRetriableCall(ctx context.Context, method, urlPath string, query map[string]string, payload []byte) (res *http.Response, err error) {
for i := 0; i < 2; i++ {
res, err = c.doLockingCall(ctx, method, urlPath, query, payload)
if err != nil || i != 0 || (res.StatusCode != http.StatusUnauthorized && res.StatusCode != http.StatusForbidden) {
break
}
if err = c.Authenticate(ctx); err != nil {
return
}
}
return
}
// doLockingCall executes a Salesforce API call in a thread safe manner.
func (c *SalesforceClient) doLockingCall(ctx context.Context, method, urlPath string, query map[string]string, payload []byte) (*http.Response, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.doCall(ctx, method, urlPath, query, payload)
}
// doCall is not thread safe and should only be used if the client lock has been previously acquired.
func (c *SalesforceClient) doCall(ctx context.Context, method, urlPath string, query map[string]string, payload []byte) (*http.Response, error) {
u, err := url.Parse(c.creds.InstanceURL)
if err != nil {
return nil, fmt.Errorf("base URL %q is not parseable: %w", c.creds.InstanceURL, err)
}
u.Path = path.Join(u.Path, urlPath)
if len(query) > 0 {
q := url.Values{}
for k, v := range query {
q.Add(k, v)
}
u.RawQuery = q.Encode()
}
req, err := http.NewRequest(method, u.String(), bytes.NewBuffer(payload))
if err != nil {
return nil, fmt.Errorf("could not build request: %w", err)
}
req.Header.Add("Content-Type", "application/json")
if c.creds != nil {
req.Header.Add("Authorization", "Bearer "+c.creds.Token)
}
req = req.WithContext(ctx)
return c.client.Do(req)
}
type salesforceError struct {
Fields []string
Message string
ErrorCode string
}
// doNonLockingCall is not thread safe and should only be used if the client lock has been previously acquired.
func (c *SalesforceClient) manageAPIError(res *http.Response) error {
msg := fmt.Sprintf("API returned an error (%d): ", res.StatusCode)
body := io.NopCloser(res.Body)
// try to use the docummented Salesforce format.
// See: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/errorcodes.htm
se := &salesforceError{}
err := json.NewDecoder(body).Decode(se)
if err == nil {
return fmt.Errorf(msg+"(%s) %s. Fields %v", se.ErrorCode, se.Message, se.Fields)
}
// write raw response as a string
b, err := io.ReadAll(res.Body)
if err == nil {
return fmt.Errorf(msg+"%s", string(b))
}
// last choice when all previous fail
c.logger.Warnf("Could not read error message from API: %v", err)
return fmt.Errorf(msg+"%s", string(b))
}