Class: SoftLayer::Client

Inherits:
Object
  • Object
show all
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 service

  • :api_key - a non-empty string providing the api key to use for requests to the service

  • :endpoint_url - a non-empty string providing the endpoint URL to use for requests to the service

If any of the options above are missing then the constructor will try to use the corresponding global variable declared in the SoftLayer Module:

  • $SL_API_USERNAME

  • $SL_API_KEY

  • $SL_API_BASE_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/softlayer/Client.rb', line 30

def initialize(options = {})
  @services = { }

  settings = Config.client_settings(options)

  # 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_keyObject (readonly)

An API key passed as part of the authentication of each request. Cannot be emtpy or nil.



22
23
24
# File 'lib/softlayer/Client.rb', line 22

def api_key
  @api_key
end

#endpoint_urlObject (readonly)

The base URL for requests that are passed to the server. Cannot be emtpy or nil.



25
26
27
# File 'lib/softlayer/Client.rb', line 25

def endpoint_url
  @endpoint_url
end

#user_agentObject

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



28
29
30
# File 'lib/softlayer/Client.rb', line 28

def user_agent
  @user_agent
end

#usernameObject (readonly)

A username passed as authentication for each request. Cannot be emtpy or nil.



19
20
21
# File 'lib/softlayer/Client.rb', line 19

def username
  @username
end

Instance Method Details

#[](service_name) ⇒ Object



96
97
98
# File 'lib/softlayer/Client.rb', line 96

def [](service_name)
  service_named(service_name)
end

#authentication_headersObject

return a hash of the authentication headers for the client



52
53
54
55
56
57
58
59
# File 'lib/softlayer/Client.rb', line 52

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

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/softlayer/Client.rb', line 74

def service_named(service_name, service_options = {})
  raise ArgumentError,"Please provide a service name" if service_name.nil? || service_name.empty?

  # strip whitespace from service_name and
  # ensure that it start with "SoftLayer_".
  #
  # if it does not, then add it
  service_name.strip!
  if not service_name =~ /\ASoftLayer_/
    service_name = "SoftLayer_#{service_name}"
  end

  # if we've already created this service, just return it
  # otherwise create a new service
  service_key = service_name.to_sym
  if !@services.has_key?(service_key)
    @services[service_key] = SoftLayer::Service.new(service_name, {:client => self}.merge(service_options))
  end

  @services[service_key]
end