-
Notifications
You must be signed in to change notification settings - Fork 1
/
extended_httparty.rb
52 lines (45 loc) · 1.31 KB
/
extended_httparty.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
50
51
52
# Reference : https://github.com/mdoyle13/httparty-retry
# Extended HTTParty
# A Wrapper class to control the HTTParty calls, allowing retry capabilities
module ExtendedHttparty
include HTTParty
DEFAULT_TRIES = 3
SLEEP_DURATION = 3
def self.included(base)
HTTParty.included(base)
end
def self.perform_request(http_method, path, options, &block)
options.reverse_merge!(:tries => DEFAULT_TRIES, :raise_errors => false)
tries ||= options[:tries]
result = super
rescue => e
sleep SLEEP_DURATION
if (tries -= 1) > 0
retry
else
log_message("Unable to connect to the api. #{e}")
return nil unless options[:raise_errors]
raise "Can't connect to the API, #{e}"
end
else
if result.code == 200
result
else
if (tries -= 1) > 0
sleep SLEEP_DURATION
perform_request(http_method, path, :tries => tries, :query => options[:query], &block)
else
log_message("Request failed with status #{result.code}")
return nil unless options[:raise_errors]
raise "API responded with non 200, #{result.code}"
end
end
end
def self.log_message(message)
log_options = {
title: 'HTTParty Request failed.',
message: "#{self.class.name} #{message}",
}
# TODO: Log it somewhere ...
end
end