Class: ManageIQ::API::Client::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/manageiq/api/client/connection.rb

Constant Summary collapse

API_PREFIX =
"/api".freeze
CONTENT_TYPE =
"application/json".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, connection_options = {}) ⇒ Connection

Returns a new instance of Connection.



19
20
21
22
23
# File 'lib/manageiq/api/client/connection.rb', line 19

def initialize(client, connection_options = {})
  @client = client
  @connection_options = connection_options
  @error = nil
end

Instance Attribute Details

#authenticationObject (readonly)

Returns the value of attribute authentication.



8
9
10
# File 'lib/manageiq/api/client/connection.rb', line 8

def authentication
  @authentication
end

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/manageiq/api/client/connection.rb', line 9

def client
  @client
end

#connection_optionsObject (readonly)

Returns the value of attribute connection_options.



10
11
12
# File 'lib/manageiq/api/client/connection.rb', line 10

def connection_options
  @connection_options
end

#errorObject (readonly)

Returns the value of attribute error.



12
13
14
# File 'lib/manageiq/api/client/connection.rb', line 12

def error
  @error
end

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'lib/manageiq/api/client/connection.rb', line 11

def response
  @response
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/manageiq/api/client/connection.rb', line 7

def url
  @url
end

Instance Method Details

#api_path(path) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/manageiq/api/client/connection.rb', line 62

def api_path(path)
  if path.to_s.start_with?(url.to_s)
    path.to_s
  elsif path.to_s.blank?
    URI.join(url, API_PREFIX).to_s
  else
    URI.join(url, path.to_s.start_with?(API_PREFIX) ? path.to_s : "#{API_PREFIX}/#{path}").to_s
  end
end

#delete(path, params = {}) ⇒ Object



45
46
47
48
# File 'lib/manageiq/api/client/connection.rb', line 45

def delete(path, params = {})
  send_request(:delete, path, params)
  json_response
end

#get(path = "", params = {}) ⇒ Object



25
26
27
28
# File 'lib/manageiq/api/client/connection.rb', line 25

def get(path = "", params = {})
  send_request(:get, path, params)
  json_response
end

#handleObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/manageiq/api/client/connection.rb', line 72

def handle
  ssl_options = @connection_options[:ssl]
  Faraday.new(:url => url, :ssl => ssl_options) do |faraday|
    faraday.request(:url_encoded) # form-encode POST params
    faraday.options.open_timeout = @connection_options[:open_timeout] if @connection_options[:open_timeout]
    faraday.options.timeout      = @connection_options[:timeout]      if @connection_options[:timeout]
    faraday.response(:logger, client.logger)
    faraday.response(:follow_redirects, :limit => 3, :standards_compliant => true)
    # make requests with Net::HTTP
    faraday.adapter(Faraday.default_adapter)
    if authentication.token.blank? && authentication.miqtoken.blank? && authentication.bearer_token.blank?
      if faraday.respond_to?(:basic_auth)
        # faraday 1.0
        faraday.basic_auth(authentication.user, authentication.password)
      else
        # faraday 2.0
        faraday.request(:authorization, :basic, authentication.user, authentication.password)
      end
    end
  end
end

#json_responseObject



55
56
57
58
59
60
# File 'lib/manageiq/api/client/connection.rb', line 55

def json_response
  resp = response.body.strip
  resp.blank? ? {} : JSON.parse(resp)
rescue
  raise JSON::ParserError, "Response received from #{url} is not of type #{CONTENT_TYPE}"
end

#options(path = "", params = {}) ⇒ Object



50
51
52
53
# File 'lib/manageiq/api/client/connection.rb', line 50

def options(path = "", params = {})
  send_request(:options, path, params)
  json_response
end

#patch(path, params = {}, &block) ⇒ Object



40
41
42
43
# File 'lib/manageiq/api/client/connection.rb', line 40

def patch(path, params = {}, &block)
  send_request(:patch, path, params, &block)
  json_response
end

#post(path, params = {}, &block) ⇒ Object



35
36
37
38
# File 'lib/manageiq/api/client/connection.rb', line 35

def post(path, params = {}, &block)
  send_request(:post, path, params, &block)
  json_response
end

#put(path, params = {}, &block) ⇒ Object



30
31
32
33
# File 'lib/manageiq/api/client/connection.rb', line 30

def put(path, params = {}, &block)
  send_request(:put, path, params, &block)
  json_response
end