Class: ShopifyClient::Response

Inherits:
Struct
  • Object
show all
Defined in:
lib/shopify-client/response.rb,
lib/shopify-client/response.rb,
lib/shopify-client/response.rb

Overview

NOTE: Reopened for proper scoping of error classes.

Defined Under Namespace

Classes: Error

Constant Summary collapse

ClientError =

Client errors in the 4xx range.

Class.new(Error)
ServerError =

Server errors in the 5xx range.

Class.new(Error)
InvalidAccessTokenError =

The access token was not accepted.

Class.new(ClientError)
ShopError =

The shop is frozen/locked/unavailable.

Class.new(ClientError)
TooManyRequestsError =

The app is making too many requests to the API.

Class.new(ClientError)
GraphQLClientError =

The GraphQL API always responds with a status code of 200.

Class.new(ClientError) do
  def message
    case
    when response.errors?
      "bad response: #{response.errors.messages.first}"
    when response.user_errors?
      "bad response: #{response.user_errors.messages.first}"
    else
      "bad response"
    end
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataHash

Returns:

  • (Hash)


14
# File 'lib/shopify-client/response.rb', line 14

Response = Struct.new(:request, :status_code, :headers, :data)

#headersHash

Returns:

  • (Hash)


14
# File 'lib/shopify-client/response.rb', line 14

Response = Struct.new(:request, :status_code, :headers, :data)

#requestRequest

Returns:



14
# File 'lib/shopify-client/response.rb', line 14

Response = Struct.new(:request, :status_code, :headers, :data)

#status_codeInteger

Returns:

  • (Integer)


14
# File 'lib/shopify-client/response.rb', line 14

Response = Struct.new(:request, :status_code, :headers, :data)

Class Method Details

.from_faraday_response(faraday_response, client = nil) ⇒ Response

Parameters:

  • faraday_response (Faraday::Response)
  • client (Client) (defaults to: nil)

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/shopify-client/response.rb', line 23

def from_faraday_response(faraday_response, client = nil)
  uri = Addressable::URI.parse(faraday_response.env[:url])

  new(
    Request.new(
      # Merchant myshopify.domain.
      uri.host,
      # Merchant access token.
      faraday_response.env[:request_headers]['X-Shopify-Access-Token'],
      # Request HTTP method.
      faraday_response.env[:method],
      # Request path.
      uri.path,
      # Request params.
      uri.query_values,
      # Request headers.
      faraday_response.env[:request_headers],
      # Request data.
      faraday_response.env[:request_body],
      # Client used for the request.
      client,
    ),
    faraday_response.status,
    faraday_response.headers,
    faraday_response.body || {},
  ).tap(&:assert!)
end

Instance Method Details

#assert!Object

Raises:

See Also:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/shopify-client/response.rb', line 56

def assert!
  case status_code
  when 401
    if errors.message?([/access token/i])
      raise InvalidAccessTokenError.new(request, self), 'Invalid access token'
    else
      raise ClientError.new(request, self)
    end
  when 402
    raise ShopError.new(request, self), 'Shop is frozen, awaiting payment'
  when 403
    # NOTE: Not sure what this one means (undocumented).
    if errors.message?([/unavailable shop/i])
      raise ShopError.new(request, self), 'Shop is unavailable'
    else
      raise ClientError.new(request, self)
    end
  when 423
    raise ShopError.new(request, self), 'Shop is locked'
  when 430
    # NOTE: This is an unofficial code used by Shopify. See:
    #
    # https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#Unofficial_codes
    #
    # It's undocumented unfortunately, but seems to be like a 429 response,
    # except where the app is making too many API calls (rather than hitting
    # the per store rate limit).
    raise TooManyRequestsError.new(request, self), 'Too many requests'
  when 400..499
    raise ClientError.new(request, self)
  when 500..599
    raise ServerError.new(request, self)
  end

  # GraphQL always has status 200.
  if request.graphql? && (errors? || user_errors?)
    raise GraphQLClientError.new(request, self)
  end
end

#errorsResponseErrors

Response errors (usually included with a 422 response).

Returns:



130
131
132
# File 'lib/shopify-client/response.rb', line 130

def errors
  @errors ||= ResponseErrors.from_response_data(data)
end

#errors?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/shopify-client/response.rb', line 135

def errors?
  errors.any?
end

#inspectString

Returns:

  • (String)


156
157
158
# File 'lib/shopify-client/response.rb', line 156

def inspect
  "#<ShopifyClient::Response (#{status_code}, #{request.inspect})>"
end

#next_page(client = request.client) ⇒ Response?

Request the next page for a GET request, if any.

Parameters:

Returns:

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
# File 'lib/shopify-client/response.rb', line 106

def next_page(client = request.client)
  raise ArgumentError, 'missing client' if client.nil?

  return nil unless link[:next]

  client.get(request.path, link[:next])
end

#previous_page(client = request.client) ⇒ Response?

Request the next page for a GET request, if any.

Parameters:

Returns:

Raises:

  • (ArgumentError)


119
120
121
122
123
124
125
# File 'lib/shopify-client/response.rb', line 119

def previous_page(client = request.client)
  raise ArgumentError, 'missing client' if client.nil?

  return nil unless link[:previous]

  client.get(request.path, link[:previous])
end

#user_errorsResponseUserErrors?

GraphQL user errors (errors in mutation input).

Returns:



142
143
144
145
146
# File 'lib/shopify-client/response.rb', line 142

def user_errors
  return nil unless request.graphql?

  @user_errors ||= ResponseUserErrors.from_response_data(data)
end

#user_errors?Boolean

Returns:

  • (Boolean)


149
150
151
152
153
# File 'lib/shopify-client/response.rb', line 149

def user_errors?
  return false unless request.graphql?

  user_errors.any?
end