Skip to content

Commit

Permalink
Handle rate limiting and internal state errors from shopify
Browse files Browse the repository at this point in the history
  • Loading branch information
kishaningithub committed Mar 6, 2020
1 parent 5492c2e commit e9c1a8e
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions internal/shopify/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -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
}
}

0 comments on commit e9c1a8e

Please sign in to comment.