From fc932b0184ff36147ceba0a319159a085e4cbda5 Mon Sep 17 00:00:00 2001 From: alexunit42 Date: Mon, 7 May 2018 16:39:30 +0300 Subject: [PATCH 1/6] add rate limits to error wrapper --- models/pinterest_error.go | 2 ++ models/rate_limit.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 models/rate_limit.go diff --git a/models/pinterest_error.go b/models/pinterest_error.go index 2f7b35d..dc472f5 100644 --- a/models/pinterest_error.go +++ b/models/pinterest_error.go @@ -12,6 +12,7 @@ import ( type PinterestError struct { StatusCode int `json:"status_code"` Message string `json:"message"` + Limit TypeRatelimit } func (e *PinterestError) Error() string { @@ -32,6 +33,7 @@ func WrapPinterestError(httpResponse *http.Response, bodyResponse *Response, err return &PinterestError{ StatusCode: httpResponse.StatusCode, Message: bodyResponse.Message, + Limit: GetRatelimit(httpResponse), } } diff --git a/models/rate_limit.go b/models/rate_limit.go new file mode 100644 index 0000000..d35f4e6 --- /dev/null +++ b/models/rate_limit.go @@ -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"), + } +} \ No newline at end of file From 3d2ac8a90f633e89f078b8b35cbf33c5dee9203d Mon Sep 17 00:00:00 2001 From: Rizhiy Olexandr Date: Fri, 11 May 2018 15:47:53 +0300 Subject: [PATCH 2/6] Update rate_limit.go --- models/rate_limit.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/rate_limit.go b/models/rate_limit.go index d35f4e6..2c36119 100644 --- a/models/rate_limit.go +++ b/models/rate_limit.go @@ -15,7 +15,7 @@ type TypeRatelimit struct { func GetLimit(httpResp *http.Response, key string) int { if val, ok := httpResp.Header[key]; ok { rateLimit, err := strconv.Atoi(val[0]) - if err == nil { + if err != nil { fmt.Println(err) return 0 } @@ -30,4 +30,4 @@ func GetRatelimit(httpResp *http.Response) TypeRatelimit { Limit: GetLimit(httpResp, "X-Ratelimit-Limit"), Refresh: GetLimit(httpResp, "X-Ratelimit-Remaining"), } -} \ No newline at end of file +} From 0d921c1b490835f0d653ffe7ce3d011369df219d Mon Sep 17 00:00:00 2001 From: Rizhiy Olexandr Date: Fri, 11 May 2018 16:05:17 +0300 Subject: [PATCH 3/6] Replace Refresh <--->Remaining Replace Refresh <--->Remaining --- models/rate_limit.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/rate_limit.go b/models/rate_limit.go index 2c36119..80b3716 100644 --- a/models/rate_limit.go +++ b/models/rate_limit.go @@ -26,8 +26,8 @@ func GetLimit(httpResp *http.Response, key string) int { func GetRatelimit(httpResp *http.Response) TypeRatelimit { return TypeRatelimit { - Remaining: GetLimit(httpResp, "X-Ratelimit-Refresh"), + Remaining: GetLimit(httpResp, "X-Ratelimit-Remaining"), Limit: GetLimit(httpResp, "X-Ratelimit-Limit"), - Refresh: GetLimit(httpResp, "X-Ratelimit-Remaining"), + Refresh: GetLimit(httpResp, "X-Ratelimit-Refresh"), } } From e1fdb18006aa3078505a29b43e87752e25c7d4a7 Mon Sep 17 00:00:00 2001 From: Rizhiy Olexandr Date: Fri, 11 May 2018 16:20:17 +0300 Subject: [PATCH 4/6] Update rate_limit.go --- models/rate_limit.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/rate_limit.go b/models/rate_limit.go index 80b3716..ceec3e9 100644 --- a/models/rate_limit.go +++ b/models/rate_limit.go @@ -15,7 +15,7 @@ type TypeRatelimit struct { func GetLimit(httpResp *http.Response, key string) int { if val, ok := httpResp.Header[key]; ok { rateLimit, err := strconv.Atoi(val[0]) - if err != nil { + if err == nil { fmt.Println(err) return 0 } @@ -26,8 +26,8 @@ func GetLimit(httpResp *http.Response, key string) int { func GetRatelimit(httpResp *http.Response) TypeRatelimit { return TypeRatelimit { - Remaining: GetLimit(httpResp, "X-Ratelimit-Remaining"), + Remaining: GetLimit(httpResp, "X-Ratelimit-Refresh"), Limit: GetLimit(httpResp, "X-Ratelimit-Limit"), - Refresh: GetLimit(httpResp, "X-Ratelimit-Refresh"), + Refresh: GetLimit(httpResp, "X-Ratelimit-Remaining"), } } From 24ac5ec64032eaed4c7f2afdc72fea9ac52547f4 Mon Sep 17 00:00:00 2001 From: alexunit42 Date: Tue, 15 May 2018 10:32:32 +0300 Subject: [PATCH 5/6] fix model/rate_linit.go --- models/rate_limit.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 models/rate_limit.go diff --git a/models/rate_limit.go b/models/rate_limit.go new file mode 100644 index 0000000..7d7d24c --- /dev/null +++ b/models/rate_limit.go @@ -0,0 +1,34 @@ +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-Remaining"), + Limit: GetLimit(httpResp, "X-Ratelimit-Limit"), + Refresh: GetLimit(httpResp, "X-Ratelimit-Refresh"), + } +} + From 8065eb92c768f1cd5fa371e4443ff55f7140b578 Mon Sep 17 00:00:00 2001 From: Aleksandr Ryzhyi Date: Mon, 15 Apr 2019 14:20:57 +0300 Subject: [PATCH 6/6] add cursor to BoardsPinsController --- controllers/boards_pins_controller.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/controllers/boards_pins_controller.go b/controllers/boards_pins_controller.go index 6005262..d9f2354 100644 --- a/controllers/boards_pins_controller.go +++ b/controllers/boards_pins_controller.go @@ -26,20 +26,25 @@ type BoardsPinsFetchOptionals struct { // Fetch loads a board from the board_spec (username/board-slug) // Endpoint: [GET] /v1/boards//pins/ -func (bpc *BoardsPinsController) Fetch(boardSpec string, optionals *BoardsPinsFetchOptionals) (*[]models.Pin, error) { +func (bpc *BoardsPinsController) Fetch(boardSpec string, optionals *BoardsPinsFetchOptionals) (*[]models.Pin, *models.Page, error) { // Build + execute request resp := new(models.Response) resp.Data = &[]models.Pin{} - httpResp, err := bpc.wreckerClient.Get("/boards/"+boardSpec+"/pins/"). + request := bpc.wreckerClient.Get("/boards/"+boardSpec+"/pins/"). URLParam("fields", models.PIN_FIELDS). - Into(resp). - Execute() + Into(resp) + + if optionals.Cursor != "" { + request.URLParam("cursor", optionals.Cursor) + } + + httpResp, err := request.Execute() // Check Error if err = models.WrapPinterestError(httpResp, resp, err); err != nil { - return nil, err + return nil, nil, err } // OK - return resp.Data.(*[]models.Pin), nil + return resp.Data.(*[]models.Pin), &resp.Page, nil }