class StrawberryAPI::HttpClient
Class HttpClient
provides a way to query a remote HTTP server It shamelessly uses the HTTParty
gem for all the dirty job
@author Pierre Lebrun <pierreyves.lebrun@gmail.com>
Public Class Methods
Logs in and stores authentication session
@option [String] host Hostname or IP address of Strawberry server @option [String] username Username of Strawberry's user @option [String] password Password of Strawberry's user @option [String] api_key API key of Strawberry's user
@return [StrawberryAPI::API:User] logged in user
# File lib/strawberry_api/http_client.rb, line 32 def initialize(**options) if options.empty? username = StrawberryAPI.config.username password = StrawberryAPI.config.password api_key = StrawberryAPI.config.api_key else username = options[:username] password = options[:password] api_key = options[:api_key] end self.class.base_uri("http://#{StrawberryAPI.config.host}/api/v1") if api_key self.class.get("/session", headers: {Authorization: "Bearer #{api_key}"}).success? @headers = self.class.headers.dup @headers[:Authorization] = "Bearer #{api_key}" elsif username && password session = self.class.post('/session', query: {login: username, password: password}) authentication_token = session.parse['authentication_token'] @cookies = self.class.cookies.dup @cookies[:_auth_token] = authentication_token else raise 'Invalid credentials provided' end end
Public Instance Methods
Overrides HTTPary reponse handling
@param [String] path @param [String] mute_failure Prevents error raise if request fails
@return [HTTParty::Response] API response
# File lib/strawberry_api/http_client.rb, line 130 def delete(path, mute_failure: false, **options) request(:delete, path, mute_failure: false, **options) end
Overrides HTTPary reponse handling
@param [String] path @param [String] mute_failure Prevents error raise if request fails
@return [HTTParty::Response] API response
# File lib/strawberry_api/http_client.rb, line 97 def get(path, mute_failure: false, **options) request(:get, path, mute_failure: false, **options) end
Handles HTTParty
reponse
@param [HTTParty::Response] response
@return [HTTParty::Response] HTTP reponse
@raise [String] Indicates the reponse error code and information
# File lib/strawberry_api/http_client.rb, line 82 def handle_response(response) if response.success? response else raise "#{response.code} #{response['error']}" end end
Overrides HTTPary reponse handling
@param [String] path @param [String] mute_failure Prevents error raise if request fails
@return [HTTParty::Response] API response
# File lib/strawberry_api/http_client.rb, line 108 def post(path, mute_failure: false, **options) request(:post, path, mute_failure: false, **options) end
Overrides HTTPary reponse handling
@param [String] path @param [String] mute_failure Prevents error raise if request fails
@return [HTTParty::Response] API response
# File lib/strawberry_api/http_client.rb, line 119 def put(path, mute_failure: false, **options) request(:put, path, mute_failure: false, **options) end
# File lib/strawberry_api/http_client.rb, line 61 def request(method, path, mute_failure: false, **options) options[:headers] = @headers options[:cookies] = @cookies response = self.class.send(method, path, options) if mute_failure response else handle_response(response) end end