class FidorApi::Client::Connection

Constants

DEFAULT_HEADERS

Attributes

client[RW]
host[RW]

Public Class Methods

new(client:, host:) click to toggle source
# File lib/fidor_api/client/connection.rb, line 9
def initialize(client:, host:)
  @client = client
  @host   = host
end

Private Instance Methods

faraday(auth:) click to toggle source
# File lib/fidor_api/client/connection.rb, line 44
def faraday(auth:) # rubocop:disable Metrics/AbcSize
  Faraday.new(url: host, ssl: { verify: client.config.verify_ssl }) do |config|
    config.use Faraday::Request::BasicAuthentication, *auth if auth.is_a? Array
    config.request :oauth2, client.token.access_token, token_type: :bearer if client.token
    config.response :logger, client.config.logger, bodies: client.config.log_bodies if client.config.logger
    config.response :raise_error
    config.response :json, content_type: /json/
    client.config.faraday.call(config)
    config.adapter Faraday.default_adapter
  end
end
request(path:, method: :get, query: {}, body: {}, headers: {}, auth: nil) click to toggle source
# File lib/fidor_api/client/connection.rb, line 31
def request(path:, method: :get, query: {}, body: {}, headers: {}, auth: nil) # rubocop:disable Metrics/ParameterLists
  payload = method == :get ? query : body&.to_json

  response = faraday(auth: auth).public_send(method, path, payload) do |request|
    request.headers = headers
  end

  response
rescue Faraday::ClientError => e
  client.config.logger.error e.inspect if client.config.logger # rubocop:disable Style/SafeNavigation
  raise
end