Skip to content

Commit

Permalink
Added the ability to establish a network timeout when a client is cre…
Browse files Browse the repository at this point in the history
…ated.
  • Loading branch information
SLsthompson committed Jul 30, 2014
1 parent aa7f834 commit 9ee0c1c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lib/softlayer/Client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class Client

# A string passsed as the value for the User-Agent header when requests are sent to SoftLayer API.
attr_accessor :user_agent

# An integer value (in seconds). The number of seconds to wait for HTTP requests to the network API
# until they timeout. This value can be nil in which case the timeout will be the default value for
# the library handling network communication (often 30 seconds)
attr_reader :network_timeout

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

@user_agent = settings[:user_agent] || "softlayer_api gem/#{SoftLayer::VERSION} (Ruby #{RUBY_PLATFORM}/#{RUBY_VERSION})"

@network_timeout = settings[:timeout] if settings.has_key?(:timeout)

raise "A SoftLayer Client requires a username" if !@username || @username.empty?
raise "A SoftLayer Client requires an api_key" if !@api_key || @api_key.empty?
Expand Down
2 changes: 2 additions & 0 deletions lib/softlayer/Config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module SoftLayer
# [softlayer]
# username = joeusername
# api_key = DEADBEEFBADF00D
# timeout = 60
#
# = Environment Variables
#
Expand Down Expand Up @@ -110,6 +111,7 @@ def Config.file_settings(*additional_files)
result[:username] = softlayer_section['username'] if softlayer_section['username']
result[:endpoint_url] = softlayer_section['endpoint_url'] if softlayer_section['endpoint_url']
result[:api_key] = softlayer_section['api_key'] if softlayer_section['api_key']
result[:timeout] = softlayer_section['timeout'] if softlayer_section['timeout']
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/softlayer/Service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ def to_ary

def xmlrpc_client()
if !@xmlrpc_client
@xmlrpc_client = XMLRPC::Client.new2(URI.join(@client.endpoint_url,@service_name).to_s)
@xmlrpc_client = XMLRPC::Client.new2(URI.join(@client.endpoint_url,@service_name).to_s, nil, @client.network_timeout)

# this is a workaround for a bug in later versions of the XML-RPC client in Ruby Core.
# This is a workaround for a bug in later versions of the XML-RPC client in Ruby Core.
# see https://bugs.ruby-lang.org/issues/8182
@xmlrpc_client.http_header_extra = {
"Accept-Encoding" => "identity",
Expand Down
10 changes: 10 additions & 0 deletions spec/Client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@
end.to raise_error
end

it 'initializes by default with nil as the timeout' do
client = SoftLayer::Client.new(:username => 'fake_user', :api_key => 'fake_key', :endpoint_url => 'http://fakeurl.org/')
expect(client.network_timeout).to be_nil
end

it 'Accepts a timeout given as a config parameter' do
client = SoftLayer::Client.new(:username => 'fake_user', :api_key => 'fake_key', :endpoint_url => 'http://fakeurl.org/', :timeout => 60)
expect(client.network_timeout).to eq 60
end

it 'gets the default endpoint even if none is provided' do
$SL_API_BASE_URL = nil
client = SoftLayer::Client.new(:username => 'fake_user', :api_key => 'fake_key')
Expand Down
16 changes: 14 additions & 2 deletions spec/Service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,23 @@
client = SoftLayer::Client.new() # authentication is taken from the globals
expect { SoftLayer::Service.new("SoftLayer_Account", :client => client, :username => "sample_username", :api_key => "blah") }.to raise_error(RuntimeError)
end

end #describe #new
end

describe SoftLayer::Service do
describe SoftLayer::Service, "xmlrpc client" do
before(:each) do
SoftLayer::Service.send(:public, :xmlrpc_client)
end

it "Constructs an XMLRPC client with a given timeout value based on the timeout of the client" do
client = SoftLayer::Client.new(:username => 'fake_user', :api_key => 'fake_key', :timeout => 60)
ticket_service = client[:Ticket]
xmlrpc = ticket_service.xmlrpc_client()
expect(xmlrpc.timeout).to eq 60
end
end

describe SoftLayer::Service, "parameter filters" do
let (:service) do
SoftLayer::Service.new("SoftLayer_Ticket", :username => "sample_username", :api_key => "blah")
end
Expand Down

0 comments on commit 9ee0c1c

Please sign in to comment.