-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added exponential backoff/retry functionality for all methods. - Migrated from certificate authentication to `client_credentials` authentication flow.
- Loading branch information
1 parent
7e4bc94
commit 8cf695b
Showing
15 changed files
with
219 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,74 @@ | ||
package sfdc | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
const DefaultRetryDuration time.Duration = time.Second * 10 | ||
|
||
// Salesforce Client | ||
type Client struct { | ||
httpClient *resty.Client | ||
auth *Auth | ||
timeout time.Duration | ||
backoff backoff.BackOff | ||
} | ||
|
||
func (client *Client) prepare() (err error) { | ||
func (client *Client) prepare() error { | ||
token, err := client.auth.GetAccessToken() | ||
if err != nil { | ||
return | ||
return err | ||
} | ||
client.httpClient.SetAuthToken(token) | ||
return | ||
return nil | ||
} | ||
|
||
// do executes a given resty request method such as Get/Post. If a timeout/backoff is specified, | ||
// the request will be executed and retried within that timeout period. | ||
func (client *Client) Do(doer func(u string) (*resty.Response, error), url string) (*resty.Response, error) { | ||
op := func() (*resty.Response, error) { | ||
return doer(url) | ||
} | ||
if client.timeout == 0 { | ||
return op() | ||
} | ||
return backoff.RetryWithData(op, client.backoff) | ||
} | ||
|
||
// WithRetry specifies a time period in which to retry all requests if a errors are returned. | ||
func (client *Client) WithRetry(timeout time.Duration) *Client { | ||
client.timeout = timeout | ||
client.backoff = backoff.NewExponentialBackOff(backoff.WithMaxElapsedTime(timeout)) | ||
return client | ||
} | ||
|
||
// Create a go-sfdc client and performs initial authentication. | ||
func New( | ||
clientID, privateKey, username, authURL string, | ||
clientID, clientSecret, authURL string, | ||
encryption *string, | ||
getAccessTokenCallback CachedTokenCallback, | ||
setAccessTokenCallback SetTokenCallback, | ||
) (client *Client, err error) { | ||
getToken CachedTokenCallback, | ||
setToken SetTokenCallback, | ||
) (*Client, error) { | ||
|
||
auth, err := NewAuth( | ||
clientID, privateKey, username, authURL, | ||
clientID, clientSecret, authURL, | ||
encryption, | ||
getAccessTokenCallback, | ||
setAccessTokenCallback, | ||
getToken, | ||
setToken, | ||
) | ||
if err != nil { | ||
return | ||
return nil, err | ||
} | ||
httpClient := resty.New() | ||
httpClient.SetBaseURL(auth.InstanceURL.String()) | ||
client = &Client{ | ||
client := &Client{ | ||
httpClient: httpClient, | ||
auth: auth, | ||
timeout: DefaultRetryDuration, | ||
backoff: backoff.NewExponentialBackOff(backoff.WithMaxElapsedTime(DefaultRetryDuration)), | ||
} | ||
return | ||
return client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.