Commit 80fb055b authored by Sanad Liaquat's avatar Sanad Liaquat Committed by Ramya Authappan

Handle HTTP 429 status gracefully

parent 54cb7317
...@@ -8,9 +8,11 @@ module QA ...@@ -8,9 +8,11 @@ module QA
HTTP_STATUS_NO_CONTENT = 204 HTTP_STATUS_NO_CONTENT = 204
HTTP_STATUS_ACCEPTED = 202 HTTP_STATUS_ACCEPTED = 202
HTTP_STATUS_NOT_FOUND = 404 HTTP_STATUS_NOT_FOUND = 404
HTTP_STATUS_TOO_MANY_REQUESTS = 429
HTTP_STATUS_SERVER_ERROR = 500 HTTP_STATUS_SERVER_ERROR = 500
def post(url, payload, args = {}) def post(url, payload, args = {})
with_retry_on_too_many_requests do
default_args = { default_args = {
method: :post, method: :post,
url: url, url: url,
...@@ -24,8 +26,10 @@ module QA ...@@ -24,8 +26,10 @@ module QA
rescue RestClient::ExceptionWithResponse => e rescue RestClient::ExceptionWithResponse => e
return_response_or_raise(e) return_response_or_raise(e)
end end
end
def get(url, args = {}) def get(url, args = {})
with_retry_on_too_many_requests do
default_args = { default_args = {
method: :get, method: :get,
url: url, url: url,
...@@ -38,8 +42,10 @@ module QA ...@@ -38,8 +42,10 @@ module QA
rescue RestClient::ExceptionWithResponse => e rescue RestClient::ExceptionWithResponse => e
return_response_or_raise(e) return_response_or_raise(e)
end end
end
def put(url, payload = nil) def put(url, payload = nil)
with_retry_on_too_many_requests do
RestClient::Request.execute( RestClient::Request.execute(
method: :put, method: :put,
url: url, url: url,
...@@ -48,8 +54,10 @@ module QA ...@@ -48,8 +54,10 @@ module QA
rescue RestClient::ExceptionWithResponse => e rescue RestClient::ExceptionWithResponse => e
return_response_or_raise(e) return_response_or_raise(e)
end end
end
def delete(url) def delete(url)
with_retry_on_too_many_requests do
RestClient::Request.execute( RestClient::Request.execute(
method: :delete, method: :delete,
url: url, url: url,
...@@ -57,8 +65,10 @@ module QA ...@@ -57,8 +65,10 @@ module QA
rescue RestClient::ExceptionWithResponse => e rescue RestClient::ExceptionWithResponse => e
return_response_or_raise(e) return_response_or_raise(e)
end end
end
def head(url) def head(url)
with_retry_on_too_many_requests do
RestClient::Request.execute( RestClient::Request.execute(
method: :head, method: :head,
url: url, url: url,
...@@ -66,6 +76,26 @@ module QA ...@@ -66,6 +76,26 @@ module QA
rescue RestClient::ExceptionWithResponse => e rescue RestClient::ExceptionWithResponse => e
return_response_or_raise(e) return_response_or_raise(e)
end end
end
def with_retry_on_too_many_requests
response = nil
Support::Retrier.retry_until do
response = yield
if response.code == HTTP_STATUS_TOO_MANY_REQUESTS
wait_seconds = response.headers[:retry_after].to_i
QA::Runtime::Logger.debug("Received 429 - Too many requests. Waiting for #{wait_seconds} seconds.")
sleep wait_seconds
end
response.code != HTTP_STATUS_TOO_MANY_REQUESTS
end
response
end
def parse_body(response) def parse_body(response)
JSON.parse(response.body, symbolize_names: true) JSON.parse(response.body, symbolize_names: true)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment