class ActiveCampaignRb::HttpClient

HTTP Client wrapper

Attributes

connection[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/active_campaign_rb/http_client.rb, line 10
def initialize(opts = {})
  @connection = ::Faraday.new(url: opts.api_endpoint) do |f|
    ActiveCampaignRb::Faraday::Middleware.add_request_middleware(f, opts.request_middleware)
    ActiveCampaignRb::Faraday::Middleware.add_response_middleware(f, opts.response_middleware)
    f.adapter opts.adapter

    f.headers = {
      "Accept" => "application/json",
      "Content-Type" => "application/json",
      "Api-Token" => opts.api_key,
      "User-Agent" => "ActiveCampaignRb Client (v#{ActiveCampaignRb::VERSION})"
    }
  end
end

Public Instance Methods

delete(*args) click to toggle source
# File lib/active_campaign_rb/http_client.rb, line 43
def delete(*args)
  safe_http_call do
    connection.delete(*args)
  end
end
get(*args) click to toggle source
# File lib/active_campaign_rb/http_client.rb, line 49
def get(*args)
  safe_http_call do
    connection.get(*args)
  end
end
patch(*args) click to toggle source
# File lib/active_campaign_rb/http_client.rb, line 31
def patch(*args)
  safe_http_call do
    connection.patch(*args)
  end
end
post(*args) click to toggle source
# File lib/active_campaign_rb/http_client.rb, line 25
def post(*args)
  safe_http_call do
    connection.post(*args)
  end
end
put(*args) click to toggle source
# File lib/active_campaign_rb/http_client.rb, line 37
def put(*args)
  safe_http_call do
    connection.put(*args)
  end
end

Private Instance Methods

safe_http_call() { || ... } click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/active_campaign_rb/http_client.rb, line 59
def safe_http_call
  response = yield
  response.body
rescue ::Oj::ParseError => e
  raise ParsingError, e
rescue ::Faraday::BadRequestError => e
  raise BadRequestError, e
rescue ::Faraday::UnauthorizedError => e
  raise UnauthorizedError, e
rescue ::Faraday::ForbiddenError => e
  raise ForbiddenError, e
rescue ::Faraday::ResourceNotFound => e
  raise ResourceNotFound, e
rescue ::Faraday::ProxyAuthError => e
  raise ProxyAuthError, e
rescue ::Faraday::ConflictError => e
  raise ConflictError, e
rescue ::Faraday::UnprocessableEntityError => e
  raise UnprocessableEntityError, e
rescue ::Faraday::ServerError => e
  raise ServerError, e
rescue ::Faraday::TimeoutError => e
  raise TimeoutError, e
rescue ::Faraday::NilStatusError => e
  raise NilStatusError, e
rescue ::Faraday::ConnectionFailed => e
  raise ConnectionFailed, e
rescue ::Faraday::SSLError => e
  raise SSLError, e
end