Skip to content

Commit 3e38bb4

Browse files
sidonathjonnyom
authored andcommitted
Make network timeouts configurable (#412)
1 parent d21fa99 commit 3e38bb4

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

lib/intercom/client.rb

+21-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Intercom
22
class MisconfiguredClientError < StandardError; end
33
class Client
44
include Options
5-
attr_reader :base_url, :rate_limit_details, :username_part, :password_part, :handle_rate_limit
5+
attr_reader :base_url, :rate_limit_details, :username_part, :password_part, :handle_rate_limit, :timeouts
66

77
class << self
88
def set_base_url(base_url)
@@ -12,6 +12,17 @@ def set_base_url(base_url)
1212
Proc.new { |obj| set_base_url(old_url).call(o) }
1313
end
1414
end
15+
16+
def set_timeouts(open_timeout: nil, read_timeout: nil)
17+
return Proc.new do |o|
18+
old_timeouts = o.timeouts
19+
timeouts = {}
20+
timeouts[:open_timeout] = open_timeout if open_timeout
21+
timeouts[:read_timeout] = read_timeout if read_timeout
22+
o.send(:timeouts=, timeouts)
23+
Proc.new { |obj| set_timeouts(old_timeouts).call(o) }
24+
end
25+
end
1526
end
1627

1728
def initialize(app_id: 'my_app_id', api_key: 'my_api_key', token: nil, base_url:'https://api.intercom.io', handle_rate_limit: false)
@@ -27,6 +38,10 @@ def initialize(app_id: 'my_app_id', api_key: 'my_api_key', token: nil, base_url:
2738
@base_url = base_url
2839
@rate_limit_details = {}
2940
@handle_rate_limit = handle_rate_limit
41+
@timeouts = {
42+
open_timeout: 30,
43+
read_timeout: 90
44+
}
3045
end
3146

3247
def admins
@@ -110,13 +125,17 @@ def validate_credentials!
110125

111126
def execute_request(request)
112127
request.handle_rate_limit = handle_rate_limit
113-
request.execute(@base_url, username: @username_part, secret: @password_part)
128+
request.execute(@base_url, username: @username_part, secret: @password_part, **timeouts)
114129
ensure
115130
@rate_limit_details = request.rate_limit_details
116131
end
117132

118133
def base_url=(new_url)
119134
@base_url = new_url
120135
end
136+
137+
def timeouts=(timeouts)
138+
@timeouts = @timeouts.merge(timeouts)
139+
end
121140
end
122141
end

lib/intercom/request.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,25 @@ def self.default_headers
4646
{'Accept-Encoding' => 'gzip, deflate', 'Accept' => 'application/vnd.intercom.3+json', 'User-Agent' => "Intercom-Ruby/#{Intercom::VERSION}"}
4747
end
4848

49-
def client(uri)
49+
def client(uri, read_timeout:, open_timeout:)
5050
net = Net::HTTP.new(uri.host, uri.port)
5151
if uri.is_a?(URI::HTTPS)
5252
net.use_ssl = true
5353
net.verify_mode = OpenSSL::SSL::VERIFY_PEER
5454
net.ca_file = File.join(File.dirname(__FILE__), '../data/cacert.pem')
5555
end
56-
net.read_timeout = 90
57-
net.open_timeout = 30
56+
net.read_timeout = read_timeout
57+
net.open_timeout = open_timeout
5858
net
5959
end
6060

61-
def execute(target_base_url=nil, username:, secret: nil)
61+
def execute(target_base_url=nil, username:, secret: nil, read_timeout: 90, open_timeout: 30)
6262
retries = 3
6363
base_uri = URI.parse(target_base_url)
6464
set_common_headers(net_http_method, base_uri)
6565
set_basic_auth(net_http_method, username, secret)
6666
begin
67-
client(base_uri).start do |http|
67+
client(base_uri, read_timeout: read_timeout, open_timeout: open_timeout).start do |http|
6868
begin
6969
response = http.request(net_http_method)
7070
set_rate_limit_details(response)

spec/unit/intercom/client_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ module Intercom
1717
client.base_url.must_equal('https://api.intercom.io')
1818
end
1919

20+
it 'should be able to change the timeouts' do
21+
prev = client.options(Intercom::Client.set_timeouts(open_timeout: 10, read_timeout: 15))
22+
client.timeouts.must_equal(open_timeout: 10, read_timeout: 15)
23+
client.options(prev)
24+
client.timeouts.must_equal(open_timeout: 30, read_timeout: 90)
25+
end
26+
27+
it 'should be able to change the open timeout individually' do
28+
prev = client.options(Intercom::Client.set_timeouts(open_timeout: 50))
29+
client.timeouts.must_equal(open_timeout: 50, read_timeout: 90)
30+
client.options(prev)
31+
client.timeouts.must_equal(open_timeout: 30, read_timeout: 90)
32+
end
33+
34+
it 'should be able to change the read timeout individually' do
35+
prev = client.options(Intercom::Client.set_timeouts(read_timeout: 50))
36+
client.timeouts.must_equal(open_timeout: 30, read_timeout: 50)
37+
client.options(prev)
38+
client.timeouts.must_equal(open_timeout: 30, read_timeout: 90)
39+
end
40+
2041
it 'should raise on nil credentials' do
2142
proc { Client.new(app_id: nil, api_key: nil) }.must_raise MisconfiguredClientError
2243
end

0 commit comments

Comments
 (0)