class ManageIQ::API::Client::Connection
Constants
- API_PREFIX
- CONTENT_TYPE
Attributes
authentication[R]
client[R]
connection_options[R]
error[R]
response[R]
url[R]
Public Class Methods
new(client, connection_options = {})
click to toggle source
# File lib/manageiq/api/client/connection.rb, line 17 def initialize(client, connection_options = {}) @client = client @connection_options = connection_options @error = nil end
Public Instance Methods
api_path(path)
click to toggle source
# 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 = {})
click to toggle source
# File lib/manageiq/api/client/connection.rb, line 43 def delete(path, params = {}) send_request(:delete, path, params) json_response end
get(path = "", params = {})
click to toggle source
# File lib/manageiq/api/client/connection.rb, line 23 def get(path = "", params = {}) send_request(:get, path, params) json_response end
handle()
click to toggle source
# 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_response()
click to toggle source
# 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 = {})
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/manageiq/api/client/connection.rb, line 28 def put(path, params = {}, &block) send_request(:put, path, params, &block) json_response end
Private Instance Methods
check_response()
click to toggle source
# File lib/manageiq/api/client/connection.rb, line 105 def check_response if response.status == 404 message = json_response.fetch_path("error", "message") || json_response["error"] raise ManageIQ::API::Client::ResourceNotFound, message elsif response.status >= 400 @error = ManageIQ::API::Client::Error.new(response.status, json_response) raise @error.message end end
send_request(method, path, params) { |block| ... }
click to toggle source
# File lib/manageiq/api/client/connection.rb, line 87 def send_request(method, path, params, &block) begin @error = nil @response = handle.run_request(method.to_sym, api_path(path), nil, nil) do |request| request.headers[:content_type] = CONTENT_TYPE request.headers[:accept] = CONTENT_TYPE request.headers['X-MIQ-Group'] = authentication.group unless authentication.group.blank? request.headers['X-Auth-Token'] = authentication.token unless authentication.token.blank? request.headers['X-MIQ-Token'] = authentication.miqtoken unless authentication.miqtoken.blank? request.params.merge!(params) request.body = yield(block).to_json if block end rescue => err raise "Failed to send request to #{url} - #{err}" end check_response end