Class: OracleBMC::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/oraclebmc/api_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, signer) ⇒ ApiClient

Returns a new instance of ApiClient.



32
33
34
35
36
37
38
39
40
# File 'lib/oraclebmc/api_client.rb', line 32

def initialize(config, signer)
  fail "Missing the required parameter 'config' when initializing ApiClient." if config.nil?
  fail "Missing the required parameter 'signer' when initializing ApiClient." if signer.nil?

  @config = config
  @signer = signer
  @default_headers = {}
  @request_option_overrides = {}
end

Instance Attribute Details

#configObject

The Config object holding settings to be used in the API client.



17
18
19
# File 'lib/oraclebmc/api_client.rb', line 17

def config
  @config
end

#default_headersHash

Defines the headers to be used in HTTP requests of all API calls by default.

Returns:

  • (Hash)


22
23
24
# File 'lib/oraclebmc/api_client.rb', line 22

def default_headers
  @default_headers
end

#request_option_overridesHash

Request options to be sent with each Typhoeus request. These options will override any defaults normally set by ApiClient. See https://github.com/typhoeus/ethon/blob/master/lib/ethon/curls/options.rb for some of the available options.

Returns:

  • (Hash)


30
31
32
# File 'lib/oraclebmc/api_client.rb', line 30

def request_option_overrides
  @request_option_overrides
end

Instance Method Details

#build_request_idObject

Builds the client info string to be sent with each request.



94
95
96
# File 'lib/oraclebmc/api_client.rb', line 94

def build_request_id
  return SecureRandom.uuid.gsub!('-', '').upcase
end

#build_user_agentObject

Build the user agent string to be send with each request.



104
105
106
107
108
109
110
111
112
# File 'lib/oraclebmc/api_client.rb', line 104

def build_user_agent
  agent = "#{build_user_info} (ruby #{RUBY_VERSION}; #{RUBY_PLATFORM})"

  if config.additional_user_agent
    agent = "#{agent} #{config.additional_user_agent}"
  end

  agent
end

#build_user_infoObject

Builds the client info string to be sent with each request.



99
100
101
# File 'lib/oraclebmc/api_client.rb', line 99

def 
  return "Oracle-RubySDK/#{VERSION}"
end

#call_api(http_method, path, endpoint, opts) ⇒ Array<(Object, Fixnum, Hash)>

Call an API with given options.

Parameters:

  • http_method (Symbol)

    HTTP method/verb (e.g. :post, :get)

  • path (String)

    URL path (e.g. /volumeAttachments/)

  • endpoint (String)

    URL of the endpoint (e.g iaas.us-phoenix-1.oraclecloud.com/20160918)

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :header_params (Hash)

    Header parameters

  • :query_params (Hash)

    Query parameters

  • :form_params (Hash)

    Form parameters

  • :body (Object)

    HTTP body in JSON

Returns:

  • (Array<(Object, Fixnum, Hash)>)

    an array of 3 elements: the data deserialized from response body (could be nil), response status code, and response headers.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/oraclebmc/api_client.rb', line 54

def call_api(http_method, path, endpoint, opts)
  http_method = http_method.to_sym.downcase

  if http_method == :get
    # Wrap get calls in a lambda that can be called later for paging
    # and wait_until.
    proc = lambda { |page|
      if !page.nil?
        opts[:query_params] ||= {}
        opts[:query_params][:page] = page
      end

      return call_api_inner(http_method, path, endpoint, opts)
    }

    response = proc.call(nil)
    response.api_call = proc
    return response
  else
    # No need to wrap methods other than GET, just call them directly.
    return call_api_inner(http_method, path, endpoint, opts)
  end

end

#object_to_http_body(model) ⇒ String

Convert object (array, hash, object, etc) to JSON string.

Parameters:

  • model (Object)

    object to be converted into JSON string

Returns:

  • (String)

    JSON string representation of the object



82
83
84
85
86
87
88
89
90
91
# File 'lib/oraclebmc/api_client.rb', line 82

def object_to_http_body(model)
  return model if model.nil? || model.is_a?(String)
  local_body = nil
  if model.is_a?(Array)
    local_body = model.map{|m| object_to_hash(m) }
  else
    local_body = object_to_hash(model)
  end
  local_body.to_json
end