Skip to content

Commit

Permalink
chore: prevent too many requests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Mar 29, 2024
1 parent c4e4ce1 commit b1ae40b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 12 additions & 1 deletion api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,18 @@ func (c *client) GetUsersHydratedTimeEntries(p GetUserTimeEntriesParam) ([]dto.T
return timeEntries, err
}

user, err := c.GetUser(GetUser{p.Workspace, p.UserID})
var user dto.User
tries := 0
for tries < 5 {
tries++
user, err = c.GetUser(GetUser{p.Workspace, p.UserID})
if err == nil || !errors.Is(err, ErrorTooManyRequests) {
break

Check warning on line 454 in api/client.go

View check run for this annotation

Codecov / codecov/patch

api/client.go#L448-L454

Added lines #L448 - L454 were not covered by tests
}

time.Sleep(time.Duration(5))

Check warning on line 457 in api/client.go

View check run for this annotation

Codecov / codecov/patch

api/client.go#L457

Added line #L457 was not covered by tests
}

if err != nil {
return timeEntries, err
}
Expand Down
7 changes: 7 additions & 0 deletions api/httpClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ var ErrorNotFound = dto.Error{Message: "Nothing was found", Code: 404}
// ErrorForbidden Forbidden
var ErrorForbidden = dto.Error{Message: "Forbidden", Code: 403}

// ErrorTooManyRequests Too Many Requests
var ErrorTooManyRequests = dto.Error{Message: "Too Many Requests", Code: 429}

type transport struct {
apiKey string
next http.RoundTripper
Expand Down Expand Up @@ -112,6 +115,10 @@ func (c *client) Do(
apiErr = ErrorForbidden
}

if r.StatusCode == 429 && apiErr.Message == "" {
apiErr = ErrorTooManyRequests

Check warning on line 119 in api/httpClient.go

View check run for this annotation

Codecov / codecov/patch

api/httpClient.go#L119

Added line #L119 was not covered by tests
}

if apiErr.Message == "" {
apiErr.Message = "No response"
}
Expand Down

0 comments on commit b1ae40b

Please sign in to comment.