Class: Starling::ApiService

Inherits:
Object
  • Object
show all
Defined in:
lib/starling/api_service.rb

Overview

The API client under the hood, used by services publicly exposed by Client to dispatch requests to the Starling Bank API

Constant Summary collapse

DEFAULT_ADAPTER =

The default adapter which will be used by Faraday to make requests. This can be customised by passing a :http_adapter when instantiating the ApiService.

:net_http
BASE_PATH =

The path of the API version which will be prepended to request paths

'/api/v1'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(base_url, access_token:, http_adapter: [DEFAULT_ADAPTER], connection_options: {}, default_headers: {}) ⇒ Starling::ApiService

Instantiates a Starling::ApiService for dispatching requests to the Starling Bank API

Parameters:

  • base_url (String)

    The base URL for the Starling API, including the protocol and hostname, to which the BASE_PATH and request paths will be added

  • access_token (String)

    A personal access token for the Starling Bank API

  • http_adapter (Array) (defaults to: [DEFAULT_ADAPTER])

    The HTTP adapter to be used, defaulting to DEFAULT_ADAPTER

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

    A hash of options to be passed in when instantiating Faraday (for example for setting the request timeout)

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

    A set of user-provided HTTP headers to add to each request, alongside the library’s defaults defined in #library_default_headers



29
30
31
32
33
34
35
36
37
38
# File 'lib/starling/api_service.rb', line 29

def initialize(base_url, access_token:, http_adapter: [DEFAULT_ADAPTER],
               connection_options: {}, default_headers: {})
  @connection = Faraday.new(base_url, connection_options) do |faraday|
    faraday.response(:raise_starling_errors)
    faraday.adapter(*http_adapter)
  end

  @headers = library_default_headers(access_token: access_token)
             .merge(default_headers)
end

Instance Method Details

#make_request(method, path, params: {}, headers: {}) ⇒ Faraday::Response

Makes an HTTP request to the Starling Bank API with the specified method and path

Parameters:

  • method (Symbol)

    The HTTP method which will be used for the request

  • path (String)

    The path of the API endpoint, which will be appended to the BASE_PATH

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

    The parameters which will be included in the request, either in the URL or the body, depending on the method

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

    Additional headers to be included in your request, which will be merged on top of the service’s default headers

Returns:

  • (Faraday::Response)

    The response from the server to the dispatched request



50
51
52
53
54
55
56
# File 'lib/starling/api_service.rb', line 50

def make_request(method, path, params: {}, headers: {})
  headers = @headers.merge(headers)

  Request.new(@connection, method, build_path(path), params: params,
                                                     headers: headers)
         .make_request
end