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

Handle HTTP 429 status gracefully

parent 54cb7317
...@@ -8,63 +8,93 @@ module QA ...@@ -8,63 +8,93 @@ 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 = {})
default_args = { with_retry_on_too_many_requests do
method: :post, default_args = {
url: url, method: :post,
payload: payload, url: url,
verify_ssl: false payload: payload,
} verify_ssl: false
}
RestClient::Request.execute(
default_args.merge(args) RestClient::Request.execute(
) default_args.merge(args)
rescue RestClient::ExceptionWithResponse => e )
return_response_or_raise(e) rescue RestClient::ExceptionWithResponse => e
return_response_or_raise(e)
end
end end
def get(url, args = {}) def get(url, args = {})
default_args = { with_retry_on_too_many_requests do
method: :get, default_args = {
url: url, method: :get,
verify_ssl: false url: url,
} verify_ssl: false
}
RestClient::Request.execute(
default_args.merge(args) RestClient::Request.execute(
) default_args.merge(args)
rescue RestClient::ExceptionWithResponse => e )
return_response_or_raise(e) rescue RestClient::ExceptionWithResponse => e
return_response_or_raise(e)
end
end end
def put(url, payload = nil) def put(url, payload = nil)
RestClient::Request.execute( with_retry_on_too_many_requests do
method: :put, RestClient::Request.execute(
url: url, method: :put,
payload: payload, url: url,
verify_ssl: false) payload: payload,
rescue RestClient::ExceptionWithResponse => e verify_ssl: false)
return_response_or_raise(e) rescue RestClient::ExceptionWithResponse => e
return_response_or_raise(e)
end
end end
def delete(url) def delete(url)
RestClient::Request.execute( with_retry_on_too_many_requests do
method: :delete, RestClient::Request.execute(
url: url, method: :delete,
verify_ssl: false) url: url,
rescue RestClient::ExceptionWithResponse => e verify_ssl: false)
return_response_or_raise(e) rescue RestClient::ExceptionWithResponse => e
return_response_or_raise(e)
end
end end
def head(url) def head(url)
RestClient::Request.execute( with_retry_on_too_many_requests do
method: :head, RestClient::Request.execute(
url: url, method: :head,
verify_ssl: false) url: url,
rescue RestClient::ExceptionWithResponse => e verify_ssl: false)
return_response_or_raise(e) rescue RestClient::ExceptionWithResponse => e
return_response_or_raise(e)
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 end
def parse_body(response) def parse_body(response)
......
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