Class: OCI::ApiClient

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

Constant Summary collapse

VALID_COLLECTION_FORMATS =

Maps collection format types (as per the collectionFormat types described here: swagger.io/docs/specification/2-0/describing-parameters/) to the delimiters we should use to separate values.

The :multi type contains no delimiter because this means we should preserve values as a collection rather than transforming them into a single string

{
  multi: nil,
  csv: ',',
  ssv: ' ',
  tsv: "\t",  # this is double quoted so it is interpreted as a tab rather than as a literal slash ('\') followed by a 't'
  pipes: '|'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, signer, proxy_settings: nil) ⇒ ApiClient

Returns a new instance of ApiClient.



53
54
55
56
57
58
59
60
61
62
# File 'lib/oci/api_client.rb', line 53

def initialize(config, signer, proxy_settings: nil)
  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 = {}
  @proxy_settings = proxy_settings
end

Instance Attribute Details

#configObject

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



33
34
35
# File 'lib/oci/api_client.rb', line 33

def config
  @config
end

#default_headersHash

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

Returns:

  • (Hash)


38
39
40
# File 'lib/oci/api_client.rb', line 38

def default_headers
  @default_headers
end

#proxy_settingsOCI::ApiClientProxySettings

The proxy settings which this ApiClient will use



51
52
53
# File 'lib/oci/api_client.rb', line 51

def proxy_settings
  @proxy_settings
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)


46
47
48
# File 'lib/oci/api_client.rb', line 46

def request_option_overrides
  @request_option_overrides
end

Class Method Details

.build_collection_params(collection, collection_format) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/oci/api_client.rb', line 148

def self.build_collection_params(collection, collection_format)
  if collection_format.nil? || !VALID_COLLECTION_FORMATS.has_key?(collection_format.to_sym)
    raise "Invalid collection_format: #{collection_format}. Must be one of: #{VALID_COLLECTION_FORMATS.keys}"
  end

  if collection_format == :multi
    return collection
  else
    return collection.join(VALID_COLLECTION_FORMATS[collection_format.to_sym])
  end
end

Instance Method Details

#build_request_idObject

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



128
129
130
# File 'lib/oci/api_client.rb', line 128

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

#build_user_agentObject

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



138
139
140
141
142
143
144
145
146
# File 'lib/oci/api_client.rb', line 138

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.



133
134
135
# File 'lib/oci/api_client.rb', line 133

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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/oci/api_client.rb', line 78

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) if !using_instance_principals? && http_method != :get
  return instance_principals_signer_wrapped_call { call_api_inner(http_method, path, endpoint, opts, &block) } if using_instance_principals? && 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
      opts[:query_params][:start] = page if opts[:return_type] == 'OCI::ObjectStorage::Models::ListObjects'
    end

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

  response = proc.call(nil) if !using_instance_principals?
  response = instance_principals_signer_wrapped_call { proc.call(nil) } if using_instance_principals?

  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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/oci/api_client.rb', line 106

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))

  # Supports IO-wrapping objects we can convert to an IO. An example is Rails'
  # ActionDispatch::Http::UploadedFile, which wraps an IO (a Tempfile) but
  # doesn't expose all the IO operations directly (e.g. you can't write to it, it's not seekable)
  #
  # This should be safe to use with IO and its subclasses as well as to_io is a method on IO:
  # http://ruby-doc.org/core-2.3.1/IO.html#method-i-to_io and returns itself if called on
  # an IO
  return model.to_io if (model.respond_to?(:read) || model.respond_to?(:write)) && model.respond_to?(:to_io)

  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