Skip to content

Commit

Permalink
Make API client respect Retries (#3)
Browse files Browse the repository at this point in the history
* Add retry mechanism to API client

* fix go.mod
  • Loading branch information
davidji99 authored Oct 19, 2020
1 parent fe2ce6a commit 512bd79
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
31 changes: 28 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package api

import (
"github.com/davidji99/simpleresty"
"github.com/go-resty/resty/v2"
"net/http"
"strconv"
"sync"
"time"
)
Expand All @@ -13,8 +16,17 @@ const (
// DefaultUserAgent is the user agent used when making API calls.
DefaultUserAgent = "asana-go"

// DefaultContentTypeHeader is the default and Content-Type header.
// DefaultContentTypeHeader is the default Content-Type header.
DefaultContentTypeHeader = "application/json"

// DefaultAcceptHeader is the default Accept header.
DefaultAcceptHeader = "application/json"

// DefaultMaxRetryCount is the number of times to retry the request.
DefaultMaxRetryCount = 3

// DefaultMaxRetryWaitTime is the number of seconds a retry can wait for.
DefaultMaxRetryWaitTime = 300 * time.Second
)

// A Client manages communication with the Asana API.
Expand Down Expand Up @@ -69,6 +81,19 @@ func New(opts ...Option) (*Client, error) {
accessToken: "",
}

// Set Retries
c.http.SetRetryCount(DefaultMaxRetryCount)
c.http.SetRetryMaxWaitTime(DefaultMaxRetryWaitTime)
c.http.AddRetryCondition(
func(r *resty.Response, err error) bool {
// Set the retry wait time using the value obtained from the ['Retry-After'] header.
retryAfterValue, _ := strconv.Atoi(r.Header().Get("Retry-After"))
c.http.SetRetryWaitTime(time.Duration(retryAfterValue) * time.Second)

return r.StatusCode() == http.StatusTooManyRequests
},
)

// Define any user custom Client settings
if optErr := c.parseOptions(opts...); optErr != nil {
return nil, optErr
Expand Down Expand Up @@ -99,8 +124,8 @@ func (c *Client) setupClient() {
Per Rollbar API documentation, each individual resource will set the access_token parameter when constructing
the full API endpoint URL.
*/
c.http.SetHeader("Content-type", "application/json").
SetHeader("Accept", "application/json").
c.http.SetHeader("Content-type", DefaultContentTypeHeader).
SetHeader("Accept", DefaultAcceptHeader).
SetHeader("User-Agent", c.userAgent).
SetTimeout(1 * time.Minute).
SetAllowGetMethodPayload(true)
Expand Down
7 changes: 7 additions & 0 deletions api/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ func AccessToken(token string) Option {
return nil
}
}

func RetryCount(count int) Option {
return func(c *Client) error {
c.http.SetRetryCount(count)
return nil
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.14

require (
github.com/davidji99/simpleresty v0.2.3
github.com/go-resty/resty/v2 v2.3.0 // indirect
github.com/go-resty/resty/v2 v2.3.0
github.com/golang/protobuf v1.4.3 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.4
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0 // indirect
Expand Down

0 comments on commit 512bd79

Please sign in to comment.