Class: DuffelAPI::APIService

Inherits:
Object
  • Object
show all
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

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

Parameters:

  • base_url (String)

    A test or live mode access token

  • access_token (String)

    The URL of the Duffel API

  • default_headers (Hash)

    The headers to include by default in HTTP requests



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

Parameters:

  • method (Symbol)

    the HTTP method to make the request with

  • path (String)

    the path to make the request to

  • options (Hash) (defaults to: {})

    options to be passed with ‘Request#new`

Returns:

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
# File 'lib/duffel_api/api_service.rb', line 37

def make_request(method, path, options = {})
  raise ArgumentError, "options must be a hash" unless options.is_a?(Hash)

  options[:headers] ||= {}
  options[:headers] = @headers.merge(options[:headers])
  Request.new(@connection, method, @path_prefix + path, **options).call
end