Skip to content

Commit

Permalink
Handle rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
andresusanto committed Jan 25, 2022
1 parent b1cfff8 commit 420f9cb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
default: testacc

VERSION=1.0.0
VERSION=1.1.1
REPO=susan.to/terraform/contentful

# Run acceptance tests
Expand Down
34 changes: 34 additions & 0 deletions pkg/contentful/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package contentful
import (
"context"
"io"
"math"
"net/http"
"strconv"
"time"
)

type Client struct {
Expand Down Expand Up @@ -47,6 +49,38 @@ func (c *Client) createRequest(ctx context.Context, method string, path string,
return req, nil
}

func (c *Client) do(ctx context.Context, method string, path string, version int, body io.Reader) (*http.Response, error) {
req, err := c.createRequest(ctx, method, path, version, body)
if err != nil {
return nil, err
}

for attempt := 1; attempt <= 3; attempt++ {
res, err := c.client.Do(req)
if err != nil {
return nil, err
}

if res.StatusCode != 429 || attempt == 3 {
return res, err
}

wait := int(math.Pow(2, float64(attempt)))
resetHeader := res.Header.Get("x-contentful-ratelimit-reset")

if resetHeader != "" {
resetSecond, err := strconv.Atoi(resetHeader)
if err == nil {
wait = resetSecond
}
}

time.Sleep(time.Second * time.Duration(wait))
}

return nil, nil
}

func (c *Client) getEnv(env string) string {
envID := env
if envID == "" {
Expand Down
23 changes: 3 additions & 20 deletions pkg/contentful/content_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ func NewContentTypeService(c *Client) IContentTypeService {

func (s *contentTypeService) Activate(ctx context.Context, spaceID string, env string, id string, version int) (map[string]interface{}, error) {
path := fmt.Sprintf("/spaces/%s/environments/%s/content_types/%s/published", spaceID, s.c.getEnv(env), id)
req, err := s.c.createRequest(ctx, "PUT", path, version, nil)

if err != nil {
return nil, err
}

res, err := s.c.client.Do(req)
res, err := s.c.do(ctx, "PUT", path, version, nil)

if err != nil {
return nil, err
Expand All @@ -54,12 +48,7 @@ func (s *contentTypeService) Activate(ctx context.Context, spaceID string, env s

func (s *contentTypeService) Read(ctx context.Context, spaceID string, env string, id string) (map[string]interface{}, error) {
path := fmt.Sprintf("/spaces/%s/environments/%s/content_types/%s", spaceID, s.c.getEnv(env), id)
req, err := s.c.createRequest(ctx, "GET", path, 0, nil)
if err != nil {
return nil, err
}

res, err := s.c.client.Do(req)
res, err := s.c.do(ctx, "GET", path, 0, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,13 +76,7 @@ func (s *contentTypeService) Put(ctx context.Context, spaceID string, env string
return nil, err
}

req, err := s.c.createRequest(ctx, "PUT", path, version, bytes.NewReader(bodyBytes))

if err != nil {
return nil, err
}

res, err := s.c.client.Do(req)
res, err := s.c.do(ctx, "PUT", path, version, bytes.NewReader(bodyBytes))

if err != nil {
return nil, err
Expand Down

0 comments on commit 420f9cb

Please sign in to comment.