From e9c1a8e230ee39b468ac808b8ad1dd7d9d611732 Mon Sep 17 00:00:00 2001 From: Kishan B Date: Sat, 7 Mar 2020 01:39:36 +0530 Subject: [PATCH] Handle rate limiting and internal state errors from shopify --- internal/shopify/resource.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/internal/shopify/resource.go b/internal/shopify/resource.go index 7252796..d6629b7 100644 --- a/internal/shopify/resource.go +++ b/internal/shopify/resource.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "net/http" "net/url" "strconv" @@ -41,14 +40,22 @@ func (resource *resource) GetProducts(noOfRecordsPerPage int, page int) (Product return ProductsResponse{}, fmt.Errorf("unable to form products request: %w", err) } else if response, err := resource.httpClient.Do(request); err != nil { return ProductsResponse{}, fmt.Errorf("unable to fetch products: %w", err) - } else if data, err := ioutil.ReadAll(response.Body); err != nil { - log.Println("shopify is blocking requests. Waiting for a couple of minutes before the next request.") - log.Println(err) - time.Sleep(2 * time.Minute) + } else if response.StatusCode != http.StatusOK { + resource.handleAPIErrorsUsingAppropriateDelays(response.StatusCode) return resource.GetProducts(noOfRecordsPerPage, page) + } else if data, err := ioutil.ReadAll(response.Body); err != nil { + return ProductsResponse{}, fmt.Errorf("unable to read products response: %w", err) } else if err = json.Unmarshal(data, &responseForPage); err != nil { return ProductsResponse{}, fmt.Errorf("unable to parse json: %w", err) } else { return responseForPage, nil } } + +func (resource *resource) handleAPIErrorsUsingAppropriateDelays(httpStatusCode int) { + if httpStatusCode >= 400 && httpStatusCode < 500 { + time.Sleep(2 * time.Minute) // Handle Rate Limiters + } else if httpStatusCode >= 500 && httpStatusCode < 600 { + time.Sleep(20 * time.Second) // Handle internal errors + } +}