Class: OCI::ApiClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, signer) ⇒ ApiClient

Returns a new instance of ApiClient.



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

def initialize(config, signer)
  raise "Missing the required parameter 'config' when initializing ApiClient." if config.nil?
  raise "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.



18
19
20
# File 'lib/oci/api_client.rb', line 18

def config
  @config
end

#default_headersHash

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

Returns:

  • (Hash)


23
24
25
# File 'lib/oci/api_client.rb', line 23

def default_headers
  @default_headers
end

#request_option_overridesHash

Request options to be sent with Net::HTTP. These options will override any defaults normally set by ApiClient. See http://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html#method-c-start for some of the available options.

Returns:

  • (Hash)


31
32
33
# File 'lib/oci/api_client.rb', line 31

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.



93
94
95
# File 'lib/oci/api_client.rb', line 93

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

#build_user_agentObject

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



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

def build_user_agent
  agent = "#{}#{OCI.sdk_name} (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.



98
99
100
# File 'lib/oci/api_client.rb', line 98

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

#call_api(http_method, path, endpoint, opts, &block) ⇒ 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

  • [Block] (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.



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

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

  return call_api_inner(http_method, path, endpoint, opts, &block) unless http_method == :get

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

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

  response = proc.call(nil)
  response.api_call = proc
  response
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



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

def object_to_http_body(model)
  # to support IO-like object as well like StringIO
  return model if model.nil? || model.is_a?(String) || (model.respond_to?(:read) && model.respond_to?(:write))
  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