class TRMNL::API::Requester

Provides a low level configurable and monadic API client.

def call method, path, headers, **options

def call method, path, headers, **options
  http.headers(headers)
      .public_send(method, uri(path), **options)
      .then { |response| response.status.success? ? Success(response) : Failure(response) }
rescue HTTP::RequestError => error then handle_bad_request path, error
rescue HTTP::ConnectionError => error then handle_bad_connection path, error
rescue HTTP::TimeoutError => error then handle_timeout path, error
rescue OpenSSL::SSL::SSLError => error then handle_bad_ssl path, error
end

def get(path, headers: Core::EMPTY_HASH, **params) = call(__method__, path, headers, params:)

def get(path, headers: Core::EMPTY_HASH, **params) = call(__method__, path, headers, params:)

def handle_bad_connection path, error

def handle_bad_connection path, error
  logger.debug { error.message }
  Failure "Unable to connect: #{uri(path).inspect}. Is the network intermittent or down?"
end

def handle_bad_request path, error

def handle_bad_request path, error
  logger.debug { error.message }
  Failure "Unable to make request: #{uri(path).inspect}. Is the URI valid?"
end

def handle_bad_ssl path, error

def handle_bad_ssl path, error
  logger.debug { error.message }
  Failure "Unable to secure connection: #{uri(path).inspect}. " \
          "Is your certificate or SSL valid?"
end

def handle_timeout path, error

def handle_timeout path, error
  logger.debug { error.message }
  Failure "Connection timed out: #{uri(path).inspect}."
end

def initialize(**)

def initialize(**)
  super
  yield settings if block_given?
end

def post(path, headers: Core::EMPTY_HASH, **json) = call(__method__, path, headers, json:)

def post(path, headers: Core::EMPTY_HASH, **json) = call(__method__, path, headers, json:)

def uri(path) = "#{settings.uri}/#{path}"

def uri(path) = "#{settings.uri}/#{path}"