Class: DuffelAPI::APIService
- Inherits:
-
Object
- Object
- DuffelAPI::APIService
- Defined in:
- lib/duffel_api/api_service.rb
Overview
An internal class used within the library that is able to make requests to the Duffel API and handle errors
Instance Method Summary collapse
-
#initialize(base_url, access_token, default_headers:) ⇒ APIService
constructor
Sets up an API service based on a base URL, access token and set of default headers.
-
#make_request(method, path, options = {}) ⇒ Request
Makes a request to the API, including any defauot headers.
Constructor Details
#initialize(base_url, access_token, default_headers:) ⇒ APIService
Sets up an API service based on a base URL, access token and set of default headers
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/duffel_api/api_service.rb', line 17 def initialize(base_url, access_token, default_headers:) @base_url = base_url root_url, @path_prefix = unpack_url(base_url) @connection = Faraday.new(root_url) do |faraday| faraday.request :rate_limiter faraday.response :raise_duffel_errors faraday.adapter(:net_http) end @headers = default_headers.merge("Authorization" => "Bearer #{access_token}") end |
Instance Method Details
#make_request(method, path, options = {}) ⇒ Request
Makes a request to the API, including any defauot headers
37 38 39 40 41 42 43 |
# File 'lib/duffel_api/api_service.rb', line 37 def make_request(method, path, = {}) raise ArgumentError, "options must be a hash" unless .is_a?(Hash) [:headers] ||= {} [:headers] = @headers.merge([:headers]) Request.new(@connection, method, @path_prefix + path, **).call end |