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

Inherits:
Object
  • Object
show all
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.



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

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.



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

def authentication
  @authentication
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#connection_optionsObject (readonly)

Returns the value of attribute connection_options.



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

def connection_options
  @connection_options
end

#errorObject (readonly)

Returns the value of attribute error.



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

def error
  @error
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/manageiq/api/client/connection.rb', line 5

def url
  @url
end

Instance Method Details

#api_path(path) ⇒ Object



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

def api_path(path)
  if path.to_s.starts_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.starts_with?(API_PREFIX) ? path.to_s : "#{API_PREFIX}/#{path}").to_s
  end
end

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



43
44
45
46
# File 'lib/manageiq/api/client/connection.rb', line 43

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

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



23
24
25
26
# File 'lib/manageiq/api/client/connection.rb', line 23

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

#handleObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/manageiq/api/client/connection.rb', line 70

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.use FaradayMiddleware::FollowRedirects, :limit => 3, :standards_compliant => true
    faraday.adapter(Faraday.default_adapter) # make requests with Net::HTTP
    if authentication.token.blank? && authentication.miqtoken.blank?
      faraday.basic_auth(authentication.user, authentication.password)
    end
  end
end

#json_responseObject



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

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



48
49
50
51
# File 'lib/manageiq/api/client/connection.rb', line 48

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

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



38
39
40
41
# File 'lib/manageiq/api/client/connection.rb', line 38

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

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



33
34
35
36
# File 'lib/manageiq/api/client/connection.rb', line 33

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

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



28
29
30
31
# File 'lib/manageiq/api/client/connection.rb', line 28

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