Class: VSphereAutomation::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/vsphere-automation-appliance/api_client.rb

Overview

The client responsible for communicating with the API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Configuration.default) ⇒ ApiClient

Creates a new instance

Parameters:

  • config (Configuration) (defaults to: Configuration.default)

    configuration object with values to use



26
27
28
29
30
31
32
# File 'lib/vsphere-automation-appliance/api_client.rb', line 26

def initialize(config = Configuration.default)
  @config = config
  @http = create_http
  @user_agent = default_user_agent
  @default_headers = { 'Content-Type' => 'application/json',
                       'User-Agent' => @user_agent }
end

Instance Attribute Details

#configObject

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



15
16
17
# File 'lib/vsphere-automation-appliance/api_client.rb', line 15

def config
  @config
end

#default_headersHash (readonly)

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

Returns:

  • (Hash)


21
22
23
# File 'lib/vsphere-automation-appliance/api_client.rb', line 21

def default_headers
  @default_headers
end

Class Method Details

.defaultApiClient

Retrieves an instance of the object in it’s default state

Returns:

  • (ApiClient)

    an instance of the object in it’s default state



37
38
39
# File 'lib/vsphere-automation-appliance/api_client.rb', line 37

def self.default
  DEFAULT
end

Instance Method Details

#build_collection_param(params, format) ⇒ Object

Build the collection of parameters



42
43
44
# File 'lib/vsphere-automation-appliance/api_client.rb', line 42

def build_collection_param(params, format)
  params
end

#call_api(http_method, path, opts = {}) ⇒ Array<(Object, Fixnum, Hash)>

Make a request to an API endpoint with the given options

Parameters:

  • http_method (Symbol)

    the HTTP method to be used

  • path (String)

    the path request will be made to

  • opts (Hash) (defaults to: {})

    any additional options needed

Returns:

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

    the deserialized body, status code, and headers.



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

def call_api(http_method, path, opts = {})
  query_params = opts.fetch(:query_params, {})
  header_params = opts.fetch(:header_params, {})
  form_params = opts.fetch(:form_params, {})
  add_auth(query_params, header_params, opts.fetch(:auth_names, []))

  uri = build_request_uri(path, query_params)
  request = Net::HTTP.const_get(http_method.capitalize).new(uri)

  add_form_params(request, form_params)
  add_header_params(request, header_params)
  request.body = opts[:body] if opts[:body]
  request.content_type = request['Content-Type'] if request['Content-Type']

  if @config.debugging
    @config.logger.debug("Request Body:\n#{request.body}\n")
  end

  response = @http.request(request)
  @cookie = cookie_from_response(response)
  api_key_from_response(response)

  return_type = opts.fetch(:return_type, {}).fetch(response.code, nil)
  data = deserialize(response, return_type)
  [data, Integer(response.code), response.each_header.to_h]
end

#object_to_http_body(object) ⇒ String

Takes an object and returns the object as an HTTP body

Parameters:

  • object (Object)

    object to transform

Returns:

  • (String)

    object as JSON string



84
85
86
87
88
89
90
# File 'lib/vsphere-automation-appliance/api_client.rb', line 84

def object_to_http_body(object)
  return object.map { |o| object_to_http_body(o) } if object.is_a?(Array)

  return object unless object.respond_to?(:to_hash)

  object.to_hash.to_json
end

#select_header_accept(types) ⇒ String

Select an Accept header to use

Parameters:

  • types (Array)

    a list of suggested types

Returns:

  • (String)

    the Accept header value



96
97
98
99
100
# File 'lib/vsphere-automation-appliance/api_client.rb', line 96

def select_header_accept(types)
  return DEFAULT_MIME_TYPE unless types.is_a?(Array)

  types.find { |t| t.include?('json') } || types.join(', ')
end

#select_header_content_type(types) ⇒ String

Select an Content-Type header to use

Parameters:

  • types (Array)

    a list of suggested types

Returns:

  • (String)

    the Content-Type header value



106
107
108
109
110
# File 'lib/vsphere-automation-appliance/api_client.rb', line 106

def select_header_content_type(types)
  return DEFAULT_MIME_TYPE unless types.is_a?(Array)

  types.find { |t| t.include?('json') } || types.first
end