module Agave::ApiClient

Public Class Methods

included(base) click to toggle source
# File lib/agave/api_client.rb, line 17
def self.included(base)
  base.extend ClassMethods

  base.class_eval do
    attr_reader :token, :base_url, :schema, :extra_headers
  end
end

Public Instance Methods

request(*args) click to toggle source
# File lib/agave/api_client.rb, line 55
def request(*args)
  connection.send(*args).body.with_indifferent_access
rescue Faraday::SSLError => e
  raise e if ENV['SSL_CERT_FILE'] == Cacert.pem

  Cacert.set_in_env
  request(*args)
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
  puts e.message
  raise e
rescue Faraday::ClientError => e
  error = ApiError.new(e)
  puts '===='
  puts error.message
  puts '===='
  raise error
end

Private Instance Methods

connection() click to toggle source
# File lib/agave/api_client.rb, line 75
def connection
  default_headers = {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{@token}",
    'User-Agent' => "ruby-client v#{Agave::VERSION}",
    'X-Api-Version' => "2"
  }

  options = {
    url: base_url + "/api",
    headers: default_headers.merge(extra_headers)
  }
  @connection ||= Faraday.new(options) do |c|
    c.request :json
    c.response :json, content_type: /\bjson$/
    c.response :raise_error
    c.use FaradayMiddleware::FollowRedirects
    c.adapter :net_http
  end
end