Skip to content

Commit 3b2d047

Browse files
committed
Add SoftLayer::Service#user_agent_header and sender User-Agent string with every request to SoftLayer API.
1 parent 389e32f commit 3b2d047

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

lib/softlayer/service.rb

Lines changed: 17 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
@@ -168,6 +167,19 @@ def initialize(service_name, options = {})
168167
end
169168
end #initalize
170169

170+
# User this to set or get a user agent string for this API client.
171+
#
172+
# user_agent_string()
173+
# => "SoftLayer API Ruby Client #{SoftLayer::Version}"
174+
#
175+
# user_agent_string('agentName using this gem vX.Y.Z')
176+
# => "agentName using this gem vX.Y.Z"
177+
#
178+
def user_agent_header(set=nil)
179+
@user_agent_header = set unless set.nil?
180+
@user_agent_header ||= "SoftLayer API Ruby Client #{SoftLayer::VERSION}"
181+
{"User-Agent" => @user_agent_header}
182+
end
171183

172184
# Use this as part of a method call chain to identify a particular
173185
# object as the target of the request. The parameter is the SoftLayer
@@ -327,9 +339,9 @@ def http_request_for_method(method_name, method_url, request_body = nil)
327339
end
328340

329341
if request_body && !request_body.empty?
330-
url_request = Net::HTTP::Post.new(method_url.request_uri(), content_type_header)
342+
url_request = Net::HTTP::Post.new(method_url.request_uri(), content_type_header.merge(self.user_agent_header))
331343
else
332-
url_request = Net::HTTP::Get.new(method_url.request_uri())
344+
url_request = Net::HTTP::Get.new(method_url.request_uri(), self.user_agent_header)
333345
end
334346

335347
# This warning should be obsolete as we should be using POST if the user
@@ -455,4 +467,5 @@ def endpoint_url= (new_url)
455467
@endpoint_url = new_url.strip
456468
end
457469
end # class Service
458-
end # module SoftLayer
470+
end # module SoftLayer
471+

0 commit comments

Comments
 (0)