Skip to content

Commit

Permalink
add rate limits to error wrapper (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwestside authored and BrandonRomano committed May 7, 2018
1 parent 52dcea0 commit 0d71f29
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions models/pinterest_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type PinterestError struct {
StatusCode int `json:"status_code"`
Message string `json:"message"`
Limit TypeRatelimit
}

func (e *PinterestError) Error() string {
Expand All @@ -32,6 +33,7 @@ func WrapPinterestError(httpResponse *http.Response, bodyResponse *Response, err
return &PinterestError{
StatusCode: httpResponse.StatusCode,
Message: bodyResponse.Message,
Limit: GetRatelimit(httpResponse),
}
}

Expand Down
33 changes: 33 additions & 0 deletions models/rate_limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package models

import (
"net/http"
"strconv"
"fmt"
)

type TypeRatelimit struct {
Remaining int
Limit int
Refresh int
}

func GetLimit(httpResp *http.Response, key string) int {
if val, ok := httpResp.Header[key]; ok {
rateLimit, err := strconv.Atoi(val[0])
if err == nil {
fmt.Println(err)
return 0
}
return rateLimit
}
return 0
}

func GetRatelimit(httpResp *http.Response) TypeRatelimit {
return TypeRatelimit {
Remaining: GetLimit(httpResp, "X-Ratelimit-Refresh"),
Limit: GetLimit(httpResp, "X-Ratelimit-Limit"),
Refresh: GetLimit(httpResp, "X-Ratelimit-Remaining"),
}
}

0 comments on commit 0d71f29

Please sign in to comment.