Class: ManageIQ::API::Client::Connection
- Inherits:
-
Object
- Object
- ManageIQ::API::Client::Connection
- 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.
17 18 19 20 21 |
# File 'lib/manageiq/api/client/connection.rb', line 17 def initialize(client, = {}) @client = client @connection_options = @error = nil end |
Instance Attribute Details
#authentication ⇒ Object (readonly)
Returns the value of attribute authentication.
6 7 8 |
# File 'lib/manageiq/api/client/connection.rb', line 6 def authentication @authentication end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
7 8 9 |
# File 'lib/manageiq/api/client/connection.rb', line 7 def client @client end |
#connection_options ⇒ Object (readonly)
Returns the value of attribute connection_options.
8 9 10 |
# File 'lib/manageiq/api/client/connection.rb', line 8 def @connection_options end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
10 11 12 |
# File 'lib/manageiq/api/client/connection.rb', line 10 def error @error end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
9 10 11 |
# File 'lib/manageiq/api/client/connection.rb', line 9 def response @response end |
#url ⇒ Object (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 |
#handle ⇒ Object
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 = @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.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_response ⇒ Object
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 (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 |