Class: StrawberryAPI::HttpClient
- Inherits:
-
Object
- Object
- StrawberryAPI::HttpClient
- Includes:
- HTTParty
- Defined in:
- lib/strawberry_api/http_client.rb
Overview
Class HttpClient provides a way to query a remote HTTP server It shamelessly uses the HTTParty gem for all the dirty job
Direct Known Subclasses
Instance Method Summary collapse
-
#delete(path, mute_failure: false, **options) ⇒ HTTParty::Response
Overrides HTTPary reponse handling.
-
#get(path, mute_failure: false, **options) ⇒ HTTParty::Response
Overrides HTTPary reponse handling.
-
#handle_response(response) ⇒ HTTParty::Response
Handles HTTParty reponse.
-
#initialize(**options) ⇒ StrawberryAPI::API:User
constructor
Logs in and stores authentication session.
-
#post(path, mute_failure: false, **options) ⇒ HTTParty::Response
Overrides HTTPary reponse handling.
-
#put(path, mute_failure: false, **options) ⇒ HTTParty::Response
Overrides HTTPary reponse handling.
- #request(method, path, mute_failure: false, **options) ⇒ Object
Constructor Details
#initialize(**options) ⇒ StrawberryAPI::API:User
Logs in and stores authentication session
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/strawberry_api/http_client.rb', line 32 def initialize(**) if .empty? username = StrawberryAPI.config.username password = StrawberryAPI.config.password api_key = StrawberryAPI.config.api_key else username = [:username] password = [:password] api_key = [: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..dup @cookies[:_auth_token] = authentication_token else raise 'Invalid credentials provided' end end |
Instance Method Details
#delete(path, mute_failure: false, **options) ⇒ HTTParty::Response
Overrides HTTPary reponse handling
130 131 132 |
# File 'lib/strawberry_api/http_client.rb', line 130 def delete(path, mute_failure: false, **) request(:delete, path, mute_failure: false, **) end |
#get(path, mute_failure: false, **options) ⇒ HTTParty::Response
Overrides HTTPary reponse handling
97 98 99 |
# File 'lib/strawberry_api/http_client.rb', line 97 def get(path, mute_failure: false, **) request(:get, path, mute_failure: false, **) end |
#handle_response(response) ⇒ HTTParty::Response
Handles HTTParty reponse
82 83 84 85 86 87 88 |
# 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 |
#post(path, mute_failure: false, **options) ⇒ HTTParty::Response
Overrides HTTPary reponse handling
108 109 110 |
# File 'lib/strawberry_api/http_client.rb', line 108 def post(path, mute_failure: false, **) request(:post, path, mute_failure: false, **) end |
#put(path, mute_failure: false, **options) ⇒ HTTParty::Response
Overrides HTTPary reponse handling
119 120 121 |
# File 'lib/strawberry_api/http_client.rb', line 119 def put(path, mute_failure: false, **) request(:put, path, mute_failure: false, **) end |
#request(method, path, mute_failure: false, **options) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/strawberry_api/http_client.rb', line 61 def request(method, path, mute_failure: false, **) [:headers] = @headers [:cookies] = @cookies response = self.class.send(method, path, ) if mute_failure response else handle_response(response) end end |