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
|
# File 'lib/shopify_cli/api.rb', line 48
def request(url:, body: nil, headers: {}, method: "POST")
CLI::Kit::Util.begin do
uri = URI.parse(url)
unless uri.is_a?(URI::HTTP)
ctx.abort(ctx.message("core.api.error.invalid_url", url))
end
require "shopify_cli/http_request"
= .merge()
ctx.debug("#{method} #{uri} with X-Request-Id: #{["X-Request-Id"]}")
response = if method == "POST"
HttpRequest.post(uri, body, )
elsif method == "PUT"
HttpRequest.put(uri, body, )
elsif method == "GET"
HttpRequest.get(uri, body, )
elsif method == "DELETE"
HttpRequest.delete(uri, body, )
end
case response.code.to_i
when 200..399
[response.code.to_i, JSON.parse(response.body), response]
when 401
raise APIRequestUnauthorizedError.new("#{response.code}\n#{response.body}", response: response)
when 403
raise APIRequestForbiddenError.new("#{response.code}\n#{response.body}", response: response)
when 404
raise APIRequestNotFoundError.new("#{response.code}\n#{response.body}", response: response)
when 429
raise APIRequestThrottledError.new("#{response.code}\n#{response.body}", response: response)
when 400..499
raise APIRequestClientError.new("#{response.code}\n#{response.body}", response: response)
when 500..599
raise APIRequestServerError.new("#{response.code}\n#{response.body}", response: response)
else
raise APIRequestUnexpectedError.new("#{response.code}\n#{response.body}", response: response)
end
rescue Errno::ETIMEDOUT, Timeout::Error
ctx.debug("timeout in #{method} #{uri} with X-Request-Id: #{["X-Request-Id"]}")
raise APIRequestTimeoutError, "Timeout"
end.retry_after(APIRequestRetriableError, retries: 3) do |e|
sleep(1) if e.is_a?(APIRequestThrottledError)
end
end
|