Skip to content

Commit 9ee0c1c

Browse files
committed
Added the ability to establish a network timeout when a client is created.
1 parent aa7f834 commit 9ee0c1c

5 files changed

Lines changed: 36 additions & 4 deletions

File tree

lib/softlayer/Client.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class Client
4545

4646
# A string passsed as the value for the User-Agent header when requests are sent to SoftLayer API.
4747
attr_accessor :user_agent
48+
49+
# An integer value (in seconds). The number of seconds to wait for HTTP requests to the network API
50+
# until they timeout. This value can be nil in which case the timeout will be the default value for
51+
# the library handling network communication (often 30 seconds)
52+
attr_reader :network_timeout
4853

4954
##
5055
# The client class maintains an (optional) default client. The default client
@@ -66,6 +71,7 @@ def self.default_client=(new_default)
6671
# * <b>+:api_key+</b> - The API key used to authenticate the user with the API
6772
# * <b>+:enpoint_url+</b> - The API endpoint the client should connect to. This defaults to API_PUBLIC_ENDPOINT
6873
# * <b>+:user_agent+</b> - A string that is passed along as the user agent when the client sends requests to the server
74+
# * <b>+:timeout+</b> - An integer number of seconds to wait until network requests time out. Corresponds to the network_timeout property of the client
6975
#
7076
# If these arguments are not provided then the client will try to locate them using other
7177
# sources including global variables, and the SoftLayer config file (if one exists)
@@ -85,6 +91,8 @@ def initialize(options = {})
8591
@endpoint_url = settings[:endpoint_url] || API_PUBLIC_ENDPOINT
8692

8793
@user_agent = settings[:user_agent] || "softlayer_api gem/#{SoftLayer::VERSION} (Ruby #{RUBY_PLATFORM}/#{RUBY_VERSION})"
94+
95+
@network_timeout = settings[:timeout] if settings.has_key?(:timeout)
8896

8997
raise "A SoftLayer Client requires a username" if !@username || @username.empty?
9098
raise "A SoftLayer Client requires an api_key" if !@api_key || @api_key.empty?

lib/softlayer/Config.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module SoftLayer
5454
# [softlayer]
5555
# username = joeusername
5656
# api_key = DEADBEEFBADF00D
57+
# timeout = 60
5758
#
5859
# = Environment Variables
5960
#
@@ -110,6 +111,7 @@ def Config.file_settings(*additional_files)
110111
result[:username] = softlayer_section['username'] if softlayer_section['username']
111112
result[:endpoint_url] = softlayer_section['endpoint_url'] if softlayer_section['endpoint_url']
112113
result[:api_key] = softlayer_section['api_key'] if softlayer_section['api_key']
114+
result[:timeout] = softlayer_section['timeout'] if softlayer_section['timeout']
113115
end
114116
end
115117
end

lib/softlayer/Service.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ def to_ary
296296

297297
def xmlrpc_client()
298298
if !@xmlrpc_client
299-
@xmlrpc_client = XMLRPC::Client.new2(URI.join(@client.endpoint_url,@service_name).to_s)
299+
@xmlrpc_client = XMLRPC::Client.new2(URI.join(@client.endpoint_url,@service_name).to_s, nil, @client.network_timeout)
300300

301-
# this is a workaround for a bug in later versions of the XML-RPC client in Ruby Core.
301+
# This is a workaround for a bug in later versions of the XML-RPC client in Ruby Core.
302302
# see https://bugs.ruby-lang.org/issues/8182
303303
@xmlrpc_client.http_header_extra = {
304304
"Accept-Encoding" => "identity",

spec/Client_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@
9595
end.to raise_error
9696
end
9797

98+
it 'initializes by default with nil as the timeout' do
99+
client = SoftLayer::Client.new(:username => 'fake_user', :api_key => 'fake_key', :endpoint_url => 'http://fakeurl.org/')
100+
expect(client.network_timeout).to be_nil
101+
end
102+
103+
it 'Accepts a timeout given as a config parameter' do
104+
client = SoftLayer::Client.new(:username => 'fake_user', :api_key => 'fake_key', :endpoint_url => 'http://fakeurl.org/', :timeout => 60)
105+
expect(client.network_timeout).to eq 60
106+
end
107+
98108
it 'gets the default endpoint even if none is provided' do
99109
$SL_API_BASE_URL = nil
100110
client = SoftLayer::Client.new(:username => 'fake_user', :api_key => 'fake_key')

spec/Service_spec.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,23 @@
7777
client = SoftLayer::Client.new() # authentication is taken from the globals
7878
expect { SoftLayer::Service.new("SoftLayer_Account", :client => client, :username => "sample_username", :api_key => "blah") }.to raise_error(RuntimeError)
7979
end
80-
8180
end #describe #new
8281
end
8382

84-
describe SoftLayer::Service do
83+
describe SoftLayer::Service, "xmlrpc client" do
84+
before(:each) do
85+
SoftLayer::Service.send(:public, :xmlrpc_client)
86+
end
87+
88+
it "Constructs an XMLRPC client with a given timeout value based on the timeout of the client" do
89+
client = SoftLayer::Client.new(:username => 'fake_user', :api_key => 'fake_key', :timeout => 60)
90+
ticket_service = client[:Ticket]
91+
xmlrpc = ticket_service.xmlrpc_client()
92+
expect(xmlrpc.timeout).to eq 60
93+
end
94+
end
95+
96+
describe SoftLayer::Service, "parameter filters" do
8597
let (:service) do
8698
SoftLayer::Service.new("SoftLayer_Ticket", :username => "sample_username", :api_key => "blah")
8799
end

0 commit comments

Comments
 (0)