Skip to content

Commit fe4d003

Browse files
Merge pull request #10 from cphrmky/useragent
Useragent
2 parents f4e9b30 + a23da61 commit fe4d003

4 files changed

Lines changed: 32 additions & 30 deletions

File tree

CHANGELOG.textile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
*1.0.8*
2+
* Set a default User-Agent string to be sent with all requests to SoftLayer API. Provide interface to set a custom User-Agent string.
3+
14
*1.0.7*
25
* Calls to the @getObject@ method of any service should not take parameters. The gem now warns if you make this type of call and ignores the parameters. This prevents @SoftLayer_Virtual_Guest::getObject@ from accidentally creating (billable) CCI instances.
36

@@ -17,4 +20,4 @@
1720
* We have some API routines that start with 'get' but expect arguments anyway. The code now uses HTTP POST to send requests for which the user has provided arguments regardless of the name of the routine.
1821

1922
*1.0*, *1.0.1*
20-
* Initial release of the gem
23+
* Initial release of the gem

lib/softlayer/base.rb

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#
3333

3434
module SoftLayer
35-
VERSION = "1.0.7" # version history at the bottom of the file.
35+
VERSION = "1.0.8" # version history at the bottom of the file.
3636

3737
# The base URL of the SoftLayer API's REST-like endpoints available to the public internet.
3838
API_PUBLIC_ENDPOINT = 'https://api.softlayer.com/rest/v3/'
@@ -56,27 +56,3 @@ module SoftLayer
5656
# The base URL used for the SoftLayer API's
5757
$SL_API_BASE_URL = SoftLayer::API_PUBLIC_ENDPOINT
5858
end # module SoftLayer
59-
60-
#
61-
# History:
62-
#
63-
# 1.0 - 1.0.1 - Initial release. There was some confusion over getting the gem
64-
# posted up to rubygems.org and the 1.0.1 release came about because of that
65-
# confusion. There should be no real functionality differences there.
66-
#
67-
# 1.0.2 - We have some API routines that start with 'get' but expect arguments
68-
# anyway. The code now uses HTTP POST to send requests for which the user
69-
# has provided arguments regardless of the name of the routine.
70-
#
71-
# 1.0.3 - Added a request filter to add result limits to request. Submitted by
72-
# JN. Thanks!
73-
#
74-
# 1.0.4 - Fixed a bug where the result_limit and result_offset object filters were just not working.
75-
#
76-
# 1.0.5 - Fixed a bug where empty hashes and empty arrays would not generate meaningful object masks
77-
#
78-
# 1.0.6 - Make all API calls with either a GET or a POST as the HTTP verb.
79-
#
80-
# 1.0.7 - Calls to the "getObject" method of any service should not take parameters. The gem now
81-
# warns if you make this type of call and ignores the parameters. This prevents
82-
# SoftLayer_Virtual_Guest::getObject from accidentally creating (billable) CCI instances.

lib/softlayer/service.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ def server_result_offset
9898
self.parameters[:result_offset]
9999
end
100100

101-
102101
def method_missing(method_name, *args, &block)
103102
return @target.call_softlayer_api_with_params(method_name, self, args, &block)
104103
end
@@ -134,6 +133,9 @@ class Service
134133
# The base URL for requests that are passed to the server. Cannot be emtpy or nil.
135134
attr_accessor :endpoint_url
136135

136+
# The User-Agent header sent to SoftLayer API.
137+
attr_accessor :user_agent
138+
137139
# Initialize an instance of the Client class. You pass in the service name
138140
# and optionally hash arguments specifying how the client should access the
139141
# SoftLayer API.
@@ -163,11 +165,17 @@ def initialize(service_name, options = {})
163165
# public endpoint
164166
self.endpoint_url = options[:endpoint_url] || $SL_API_BASE_URL || API_PUBLIC_ENDPOINT
165167

168+
@user_agent = {"User-Agent" => options[:user_agent] || "SoftLayer API Ruby Client #{SoftLayer::VERSION}"}
169+
166170
if($DEBUG)
167171
@method_missing_call_depth = 0
168172
end
169173
end #initalize
170174

175+
# Use this to set the user agent string for this API client.
176+
def user_agent=(set)
177+
self.user_agent['User-Agent'] = set
178+
end
171179

172180
# Use this as part of a method call chain to identify a particular
173181
# object as the target of the request. The parameter is the SoftLayer
@@ -327,9 +335,9 @@ def http_request_for_method(method_name, method_url, request_body = nil)
327335
end
328336

329337
if request_body && !request_body.empty?
330-
url_request = Net::HTTP::Post.new(method_url.request_uri(), content_type_header)
338+
url_request = Net::HTTP::Post.new(method_url.request_uri(), content_type_header.merge(self.user_agent))
331339
else
332-
url_request = Net::HTTP::Get.new(method_url.request_uri())
340+
url_request = Net::HTTP::Get.new(method_url.request_uri(), self.user_agent)
333341
end
334342

335343
# This warning should be obsolete as we should be using POST if the user
@@ -455,4 +463,5 @@ def endpoint_url= (new_url)
455463
@endpoint_url = new_url.strip
456464
end
457465
end # class Service
458-
end # module SoftLayer
466+
end # module SoftLayer
467+

spec/Service_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,20 @@
286286
end
287287
end
288288

289+
describe SoftLayer::Service, "@user_agent" do
290+
it "should return a default value if not set" do
291+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
292+
service.user_agent.should_not be_empty
293+
end
294+
295+
it "should return a set value after being set" do
296+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah", :user_agent => "foobar")
297+
service.user_agent.should === {"User-Agent"=>"foobar"}
298+
service.user_agent = "bazbang"
299+
service.user_agent.should === {"User-Agent"=>"bazbang"}
300+
end
301+
end
302+
289303
describe SoftLayer::Service, "#result_limit" do
290304
it "should return an APIParameterFilter with itself as the target" do
291305
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")

0 commit comments

Comments
 (0)