class EasybillRestClient::Request

Constants

BASE_URL
OPEN_TIMEOUT
SUPPORTED_METHODS
USERNAME

Attributes

api_key[R]
endpoint[R]
logger[R]
method[R]
params[R]
retry_cool_off_time[R]
tries[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/easybill_rest_client/request.rb, line 13
def initialize(opts = {})
  unless SUPPORTED_METHODS.include?(opts.fetch(:method).to_sym)
    raise ArgumentError, "Unsupported HTTP method '#{method}'"
  end

  @api_key = opts.fetch(:api_key)
  @method = opts.fetch(:method)
  @endpoint = opts.fetch(:endpoint)
  @params = opts.fetch(:params)
  @logger = opts.fetch(:logger)
  @tries = opts.fetch(:tries)
  @retry_cool_off_time = opts.fetch(:retry_cool_off_time)
end

Public Instance Methods

request_details() click to toggle source
# File lib/easybill_rest_client/request.rb, line 38
def request_details
  body_allowed? ? request.body : uri.query
end
run() click to toggle source
# File lib/easybill_rest_client/request.rb, line 27
def run
  request_logger.info("#{method.to_s.upcase} #{endpoint} #{request_details}")
  retry_on(TooManyRequests) do
    retry_on(Net::OpenTimeout) do
      response = perform_request
      raise TooManyRequests if response.is_a?(Net::HTTPTooManyRequests)
      response
    end
  end
end

Private Instance Methods

body_allowed?() click to toggle source
# File lib/easybill_rest_client/request.rb, line 88
def body_allowed?
  request_class::REQUEST_HAS_BODY
end
perform_request() click to toggle source
# File lib/easybill_rest_client/request.rb, line 46
def perform_request
  http_opts = { use_ssl: uri.scheme == 'https', open_timeout: OPEN_TIMEOUT }
  Net::HTTP.start(uri.host, uri.port, http_opts) do |http|
    http.request(request)
  end
end
query_params() click to toggle source
# File lib/easybill_rest_client/request.rb, line 61
def query_params
  params.map { |key, value| [key, translate_query_value(value)] }.to_h
end
request() click to toggle source
# File lib/easybill_rest_client/request.rb, line 76
def request
  return @request if @request
  @request = request_class.new(uri)
  @request.body = params.reject { |_k, v| v.nil? }.to_json if body_allowed?
  @request.basic_auth(USERNAME, api_key)
  @request
end
request_class() click to toggle source
# File lib/easybill_rest_client/request.rb, line 84
def request_class
  Net::HTTP.const_get(method.to_s.capitalize)
end
request_id() click to toggle source
# File lib/easybill_rest_client/request.rb, line 101
def request_id
  @request_id ||= SecureRandom.hex(3)
end
request_logger() click to toggle source
# File lib/easybill_rest_client/request.rb, line 97
def request_logger
  @request_logger ||= RequestLogger.new(logger: logger, request_id: request_id)
end
retry_on(exception, &block) click to toggle source
# File lib/easybill_rest_client/request.rb, line 92
def retry_on(exception, &block)
  @retry_on ||= RetryOn.new(request_logger, tries, retry_cool_off_time)
  @retry_on.retry_on(exception, &block)
end
translate_query_value(value) click to toggle source
# File lib/easybill_rest_client/request.rb, line 65
def translate_query_value(value)
  case value
  when Array
    value.join(',')
  when nil
    'null'
  else
    value
  end
end
uri() click to toggle source
# File lib/easybill_rest_client/request.rb, line 53
def uri
  return @uri if @uri
  @uri = URI.parse(BASE_URL)
  @uri.path << endpoint
  @uri.query = URI.encode_www_form(query_params) unless body_allowed?
  @uri
end