Class: ManageIQ::API::Client::Connection
- Inherits:
-
Object
- Object
- ManageIQ::API::Client::Connection
- 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
-
#authentication ⇒ Object
readonly
Returns the value of attribute authentication.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#connection_options ⇒ Object
readonly
Returns the value of attribute connection_options.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #api_path(path) ⇒ Object
- #delete(path, params = {}) ⇒ Object
- #get(path = "", params = {}) ⇒ Object
- #handle ⇒ Object
-
#initialize(client, connection_options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #json_response ⇒ Object
- #options(path = "", params = {}) ⇒ Object
- #patch(path, params = {}, &block) ⇒ Object
- #post(path, params = {}, &block) ⇒ Object
- #put(path, params = {}, &block) ⇒ Object
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, = {}) @client = client @connection_options = @error = nil end |
Instance Attribute Details
#authentication ⇒ Object (readonly)
Returns the value of attribute authentication.
8 9 10 |
# File 'lib/manageiq/api/client/connection.rb', line 8 def authentication @authentication end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
9 10 11 |
# File 'lib/manageiq/api/client/connection.rb', line 9 def client @client end |
#connection_options ⇒ Object (readonly)
Returns the value of attribute connection_options.
10 11 12 |
# File 'lib/manageiq/api/client/connection.rb', line 10 def @connection_options end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
12 13 14 |
# File 'lib/manageiq/api/client/connection.rb', line 12 def error @error end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
11 12 13 |
# File 'lib/manageiq/api/client/connection.rb', line 11 def response @response end |
#url ⇒ Object (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 |
#handle ⇒ Object
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 = @connection_options[:ssl] Faraday.new(:url => url, :ssl => ) do |faraday| faraday.request(:url_encoded) # form-encode POST params faraday..open_timeout = @connection_options[:open_timeout] if @connection_options[:open_timeout] faraday..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_response ⇒ Object
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 (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 |