Class: FractalApi::Client
- Inherits:
-
Object
- Object
- FractalApi::Client
- Defined in:
- lib/fractal_api/client.rb
Instance Method Summary collapse
- #api_url ⇒ Object
- #connection ⇒ Object
- #get(path, params: {}, headers: {}) ⇒ Object
- #handle_response(response) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #post(path, params: {}, headers: {}) ⇒ Object
- #put(path, params: {}, headers: {}) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
10 11 12 |
# File 'lib/fractal_api/client.rb', line 10 def initialize Faraday::Request.register_middleware fractal_api_auth: -> { FractalApi::FaradayAuth } end |
Instance Method Details
#api_url ⇒ Object
43 44 45 |
# File 'lib/fractal_api/client.rb', line 43 def api_url FractalApi.configuration.base_url end |
#connection ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/fractal_api/client.rb', line 32 def connection @connection ||= Faraday.new(url: api_url) do |conn| conn.request :fractal_api_auth, FractalApi.configuration.api_key, FractalApi.configuration.partner_id conn.response :multi_json, symbolize_keys: true conn.response :logger if FractalApi.configuration.debug conn.adapter Faraday.default_adapter end end |
#get(path, params: {}, headers: {}) ⇒ Object
14 15 16 17 18 |
# File 'lib/fractal_api/client.rb', line 14 def get(path, params: {}, headers: {}) connection.get(path, params, headers).tap do |response| handle_response(response) end end |
#handle_response(response) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/fractal_api/client.rb', line 47 def handle_response(response) return if response.success? case response.status when 400 raise InvalidRequestError, response.body.inspect when 401 raise APIKeyError, response.body.inspect when 403 raise , response.body.inspect when 404 raise NotFoundError, response.body.inspect else raise GenericError, "#{response.status}: #{response.body.inspect}" end end |
#post(path, params: {}, headers: {}) ⇒ Object
20 21 22 23 24 |
# File 'lib/fractal_api/client.rb', line 20 def post(path, params: {}, headers: {}) connection.post(path, MultiJson.dump(params), headers).tap do |response| handle_response(response) end end |
#put(path, params: {}, headers: {}) ⇒ Object
26 27 28 29 30 |
# File 'lib/fractal_api/client.rb', line 26 def put(path, params: {}, headers: {}) connection.put(path, MultiJson.dump(params), headers).tap do |response| handle_response(response) end end |