Class: AbiquoAPIClient::HTTPClient
- Inherits:
-
Object
- Object
- AbiquoAPIClient::HTTPClient
- Defined in:
- lib/abiquo-api/httpclient.rb
Overview
HTTPClient class.
Does the actual HTTP requests to the server.
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Faraday connection object.
-
#cookies ⇒ Object
readonly
Cookies returned by the API.
Instance Method Summary collapse
-
#initialize(api_url, creds, connection_options) ⇒ HTTPClient
constructor
Constructor.
-
#request(params) ⇒ Object
The public method called by the client.
Constructor Details
#initialize(api_url, creds, connection_options) ⇒ HTTPClient
Constructor. Recieves the parameters to establish a connection to the Abiquo API.
Parameters:
:abiquo_api_url:: The URL of the Abiquo API. ie. https://yourserver/api
:creds:: The credentials used to connect to the Abiquo API (basic auth or oauth).
:connection_options:: { :connect_timeout => <time_in_secs>, :read_timeout => <time_in_secs>, :write_timeout => <time_in_secs>,
:ssl_verify_peer => <true_or_false>, :ssl_ca_path => <path_to_ca_file> }
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/abiquo-api/httpclient.rb', line 35 def initialize(api_url, creds, ) if creds.has_key? :access_token @connection = Faraday.new(api_url, ) do |c| c. :Bearer, creds[:access_token] c.adapter :excon end elsif creds.has_key? :consumer_key @connection = Faraday.new(api_url, ) do |c| c.request :oauth, creds c.adapter :excon end else @connection = Faraday.new(api_url, ) do |c| c.basic_auth(creds[:api_username], creds[:api_password]) c.adapter :excon end end self end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Faraday connection object.
17 18 19 |
# File 'lib/abiquo-api/httpclient.rb', line 17 def connection @connection end |
#cookies ⇒ Object (readonly)
Cookies returned by the API. Contains the auth cookie that will be used after the first call.
23 24 25 |
# File 'lib/abiquo-api/httpclient.rb', line 23 def @cookies end |
Instance Method Details
#request(params) ⇒ Object
The public method called by the client.
Parameters:
- params
-
A hash of options to be passed to the Faraday connection.
Returns a hash resulting of parsing the response body as JSON, or nil if the response is “No content” (HTTP code 204).
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/abiquo-api/httpclient.rb', line 67 def request(params) # Remove nil params params.reject!{|k,v| v.nil?} # Setup Accept and Content-Type headers headers = {} headers.merge!('Accept' => params.delete(:accept)) if params.has_key?(:accept) headers.merge!('Content-Type' => params.delete(:content)) if params.has_key?(:content) # Set Auth cookie and delete user and password if present unless @cookies.nil? # @connection.data.delete(:user) unless @connection.data[:user].nil? # @connection.data.delete(:password) unless @connection.data[:password].nil? headers.merge!(@cookies) end if params.has_key? :headers params[:headers].merge!(headers) else params[:headers] = headers end response = issue_request(params) return nil if response.nil? begin response = JSON.parse(response.body) unless response.body.empty? rescue response = response.body end end |