36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
95
96
97
98
99
100
101
102
|
# File 'lib/shopify_api/clients/http_client.rb', line 36
def request(request, response_as_struct: false)
request.verify
= @headers
["Content-Type"] = T.must(request.body_type) if request.body_type
= .merge(T.must(request.)) if request.
parsed_uri = URI(request_url(request))
= (, parsed_uri)
tries = 0
response = HttpResponse.new(code: 0, headers: {}, body: "")
while tries < request.tries
tries += 1
res = T.cast(HTTParty.send(
request.http_method,
parsed_uri.to_s,
headers: ,
query: request.query,
body: request.body.class == Hash ? T.unsafe(request.body).to_json : request.body,
), HTTParty::Response)
begin
body = res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
rescue JSON::ParserError
raise if res.code.to_i < 500
body = res.body
end
if response_as_struct && body.is_a?(Hash)
json_body = body.to_json
body = JSON.parse(json_body, object_class: OpenStruct)
end
response = HttpResponse.new(code: res.code.to_i, headers: res..to_h, body: body)
if response.["x-shopify-api-deprecated-reason"]
reason = T.must(response.["x-shopify-api-deprecated-reason"])[0]
Context.logger.warn("Deprecated request to Shopify API at #{request.path}, received reason: #{reason}")
end
break if response.ok?
error_message = serialized_error(response)
unless [429, 500].include?(response.code)
raise ShopifyAPI::Errors::HttpResponseError.new(response: response), error_message
end
if tries == request.tries
raise ShopifyAPI::Errors::HttpResponseError.new(response: response), error_message if request.tries == 1
raise ShopifyAPI::Errors::MaxHttpRetriesExceededError.new(response: response),
"Exceeded maximum retry count of #{request.tries}. Last message: #{error_message}"
end
if response.code == 500 || response.["retry-after"].nil?
sleep(RETRY_WAIT_TIME)
else
sleep(T.must(response.["retry-after"])[0].to_i)
end
end
response
end
|