Skip to content

Commit

Permalink
replace interface{} with any
Browse files Browse the repository at this point in the history
  • Loading branch information
oussama4 committed Mar 17, 2022
1 parent 0f0854c commit 59aedd9
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func WithRetry(tries int) Option {
}

// Body is an API request/response body
type Body map[string]interface{}
type Body map[string]any

// shopify API client
type Client struct {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (c *Client) rest(method string, path string, body Body) (Body, error) {
if err != nil {
return nil, err
}
result := make(map[string]interface{})
result := make(map[string]any)

for t := 1; t <= c.tries; t++ {
res, err := c.client.Do(req)
Expand Down Expand Up @@ -155,10 +155,10 @@ func (c *Client) rest(method string, path string, body Body) (Body, error) {
}

// checks if a graphql response has a rate limit error and return it if exists
func (c *Client) rateLimitError(errors []interface{}) bool {
func (c *Client) rateLimitError(errors []any) bool {
for _, err := range errors {
if ext, ok := err.(map[string]interface{}); ok {
if e, ok := ext["extensions"].(map[string]interface{}); ok {
if ext, ok := err.(map[string]any); ok {
if e, ok := ext["extensions"].(map[string]any); ok {
code, ok := e["code"].(string)
if ok && (code == "MAX_COST_EXCEEDED" || code == "THROTTLED") {
return true
Expand All @@ -170,9 +170,9 @@ func (c *Client) rateLimitError(errors []interface{}) bool {
}

// get the current available rate limit from the graphql response
func (c *Client) graphqlAvailableLimit(extentions map[string]interface{}) int {
if cost, ok := extentions["cost"].(map[string]interface{}); ok {
if throttleStatus, ok := cost["throttleStatus"].(map[string]interface{}); ok {
func (c *Client) graphqlAvailableLimit(extentions map[string]any) int {
if cost, ok := extentions["cost"].(map[string]any); ok {
if throttleStatus, ok := cost["throttleStatus"].(map[string]any); ok {
if currentlyAvailable, ok := throttleStatus["currentlyAvailable"].(float64); ok {
return int(currentlyAvailable)
}
Expand All @@ -198,15 +198,15 @@ func (c *Client) graphql(body Body) (Body, error) {
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected server response: %s", res.Status)
}
result := make(map[string]interface{})
result := make(map[string]any)
dec := json.NewDecoder(res.Body)
if err := dec.Decode(&result); err != nil {
return nil, err
}

// handle errors
if e, ok := result["errors"]; ok {
errs := e.([]interface{})
errs := e.([]any)
// rate limit error
if c.rateLimitError(errs) {
if t == c.tries {
Expand All @@ -215,15 +215,15 @@ func (c *Client) graphql(body Body) (Body, error) {
time.Sleep(2 * time.Second)
continue
} else {
if errMessage, ok := errs[0].(map[string]interface{})["message"].(string); ok {
if errMessage, ok := errs[0].(map[string]any)["message"].(string); ok {
return nil, GraphqlError{msg: errMessage}
}
}
}

// check available rate limit
if e, ok := result["extensions"]; ok {
l := c.graphqlAvailableLimit(e.(map[string]interface{}))
l := c.graphqlAvailableLimit(e.(map[string]any))
if l != -1 {
c.availableLimit = l
}
Expand All @@ -232,7 +232,7 @@ func (c *Client) graphql(body Body) (Body, error) {
time.Sleep(2 * time.Second)
continue
}
return result["data"].(map[string]interface{}), nil
return result["data"].(map[string]any), nil
}
return nil, nil
}
Expand Down Expand Up @@ -262,8 +262,8 @@ func (c *Client) Delete(path string) error {
}

// Graphql sends a graphql query to shopify admin api.
func (c *Client) Graphql(query string, variables map[string]interface{}) (Body, error) {
body := map[string]interface{}{
func (c *Client) Graphql(query string, variables map[string]any) (Body, error) {
body := map[string]any{
"query": query,
}
if variables != nil {
Expand Down

0 comments on commit 59aedd9

Please sign in to comment.