Class: SoftLayer::Client
- Inherits:
-
Object
- Object
- SoftLayer::Client
- Defined in:
- lib/softlayer/Client.rb
Overview
Initialize an instance of the Client class. You pass in the service name and optionally hash arguments specifying how the client should access the SoftLayer API.
The following symbols can be used as hash arguments to pass options to the constructor:
-
:username- a non-empty string providing the username to use for requests to the client -
:api_key- a non-empty string providing the api key to use for requests to the client -
:endpoint_url- a non-empty string providing the endpoint URL to use for requests to the client
If any of these are missing then the Client class will look to the SoftLayer::Config class to provide the missing information. Please see that class for details.
Constant Summary collapse
- @@default_client =
The client class maintains an (optional) default client. The default client will be used by many methods if you do not provide an explicit client.
nil
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
An API key passed as part of the authentication of each request.
-
#endpoint_url ⇒ Object
readonly
The base URL for requests that are passed to the server.
-
#user_agent ⇒ Object
A string passsed as the value for the User-Agent header when requests are sent to SoftLayer API.
-
#username ⇒ Object
readonly
A username passed as authentication for each request.
Class Method Summary collapse
Instance Method Summary collapse
- #[](service_name) ⇒ Object
-
#authentication_headers ⇒ Object
return a hash of the authentication headers for the client.
-
#initialize(options = {}) ⇒ Client
constructor
Clients are built with a number of settings: *
:username- The username of the account you wish to access through the API *:api_key- The API key used to authenticate the user with the API *:enpoint_url- The API endpoint the client should connect to. -
#service_named(service_name, service_options = {}) ⇒ Object
Returns a service with the given name.
Constructor Details
#initialize(options = {}) ⇒ Client
Clients are built with a number of settings:
-
:username- The username of the account you wish to access through the API -
:api_key- The API key used to authenticate the user with the API -
:enpoint_url- The API endpoint the client should connect to. This defaults to API_PUBLIC_ENDPOINT -
:user_agent- A string that is passed along as the user agent when the client sends requests to the server
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)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/softlayer/Client.rb', line 73 def initialize( = {}) @services = { } settings = Config.client_settings() # pick up the username from the options, the global, or assume no username @username = settings[:username] || "" # do a similar thing for the api key @api_key = settings[:api_key] || "" # and the endpoint url @endpoint_url = settings[:endpoint_url] || API_PUBLIC_ENDPOINT @user_agent = settings[:user_agent] || "softlayer_api gem/#{SoftLayer::VERSION} (Ruby #{RUBY_PLATFORM}/#{RUBY_VERSION})" 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? raise "A SoftLayer Clietn requires an enpoint URL" if !@endpoint_url || @endpoint_url.empty? end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
An API key passed as part of the authentication of each request. Cannot be emtpy or nil.
41 42 43 |
# File 'lib/softlayer/Client.rb', line 41 def api_key @api_key end |
#endpoint_url ⇒ Object (readonly)
The base URL for requests that are passed to the server. Cannot be emtpy or nil.
44 45 46 |
# File 'lib/softlayer/Client.rb', line 44 def endpoint_url @endpoint_url end |
#user_agent ⇒ Object
A string passsed as the value for the User-Agent header when requests are sent to SoftLayer API.
47 48 49 |
# File 'lib/softlayer/Client.rb', line 47 def user_agent @user_agent end |
#username ⇒ Object (readonly)
A username passed as authentication for each request. Cannot be emtpy or nil.
38 39 40 |
# File 'lib/softlayer/Client.rb', line 38 def username @username end |
Class Method Details
.default_client ⇒ Object
54 55 56 |
# File 'lib/softlayer/Client.rb', line 54 def self.default_client return @@default_client end |
.default_client=(new_default) ⇒ Object
58 59 60 |
# File 'lib/softlayer/Client.rb', line 58 def self.default_client=(new_default) @@default_client = new_default end |
Instance Method Details
#[](service_name) ⇒ Object
136 137 138 |
# File 'lib/softlayer/Client.rb', line 136 def [](service_name) service_named(service_name) end |
#authentication_headers ⇒ Object
return a hash of the authentication headers for the client
95 96 97 98 99 100 101 102 |
# File 'lib/softlayer/Client.rb', line 95 def authentication_headers { "authenticate" => { "username" => @username, "apiKey" => @api_key } } end |
#service_named(service_name, service_options = {}) ⇒ Object
Returns a service with the given name.
If a service has already been created by this client that same service will be returned each time it is called for by name. Otherwise the system will try to construct a new service object and return that.
If the service has to be created then the service_options will be passed along to the creative function. However, when returning a previously created Service, the service_options will be ignored.
If the service_name provided does not start with ‘SoftLayer_’ that prefix will be added
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/softlayer/Client.rb', line 116 def service_named(service_name, = {}) raise ArgumentError,"Please provide a service name" if service_name.nil? || service_name.empty? # Strip whitespace from service_name and ensure that it starts with "SoftLayer_". # If it does not, then add the prefix. full_name = service_name.to_s.strip if not full_name =~ /\ASoftLayer_/ full_name = "SoftLayer_#{service_name}" end # if we've already created this service, just return it # otherwise create a new service service_key = full_name.to_sym if !@services.has_key?(service_key) @services[service_key] = SoftLayer::Service.new(full_name, {:client => self}.merge()) end @services[service_key] end |