Class: VSphereAutomation::ApiClient
- Inherits:
-
Object
- Object
- VSphereAutomation::ApiClient
- Defined in:
- lib/vsphere-automation-appliance/api_client.rb
Overview
The client responsible for communicating with the API.
Instance Attribute Summary collapse
-
#config ⇒ Object
The Configuration object holding settings to be used in the API client.
-
#default_headers ⇒ Hash
readonly
Defines the headers to be used in HTTP requests of all API calls by default.
Class Method Summary collapse
-
.default ⇒ ApiClient
Retrieves an instance of the object in it’s default state.
Instance Method Summary collapse
-
#build_collection_param(params, format) ⇒ Object
Build the collection of parameters.
-
#call_api(http_method, path, opts = {}) ⇒ Array<(Object, Fixnum, Hash)>
Make a request to an API endpoint with the given options.
-
#initialize(config = Configuration.default) ⇒ ApiClient
constructor
Creates a new instance.
-
#object_to_http_body(object) ⇒ String
Takes an object and returns the object as an HTTP body.
-
#select_header_accept(types) ⇒ String
Select an Accept header to use.
-
#select_header_content_type(types) ⇒ String
Select an Content-Type header to use.
Constructor Details
#initialize(config = Configuration.default) ⇒ ApiClient
Creates a new instance
25 26 27 28 29 30 31 |
# File 'lib/vsphere-automation-appliance/api_client.rb', line 25 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
#config ⇒ Object
The Configuration object holding settings to be used in the API client.
14 15 16 |
# File 'lib/vsphere-automation-appliance/api_client.rb', line 14 def config @config end |
#default_headers ⇒ Hash (readonly)
Defines the headers to be used in HTTP requests of all API calls by default.
20 21 22 |
# File 'lib/vsphere-automation-appliance/api_client.rb', line 20 def default_headers @default_headers end |
Class Method Details
.default ⇒ ApiClient
Retrieves an instance of the object in it’s default state
36 37 38 |
# File 'lib/vsphere-automation-appliance/api_client.rb', line 36 def self.default DEFAULT end |
Instance Method Details
#build_collection_param(params, format) ⇒ Object
Build the collection of parameters
41 42 43 |
# File 'lib/vsphere-automation-appliance/api_client.rb', line 41 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
52 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 |
# File 'lib/vsphere-automation-appliance/api_client.rb', line 52 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 = (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
83 84 85 86 87 88 89 |
# File 'lib/vsphere-automation-appliance/api_client.rb', line 83 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
95 96 97 98 99 |
# File 'lib/vsphere-automation-appliance/api_client.rb', line 95 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
105 106 107 108 109 |
# File 'lib/vsphere-automation-appliance/api_client.rb', line 105 def select_header_content_type(types) return DEFAULT_MIME_TYPE unless types.is_a?(Array) types.find { |t| t.include?('json') } || types.first end |