class APIClientBuilder::Request

Attributes

body[R]
error_handlers_collection[R]
response_handler[R]
type[R]

Public Class Methods

new(type, response_handler, body = {}) click to toggle source

@param type [Symbol] defines the object type to be processed @option response_handler [ResponseHandler] the response handler. Usually a pagination strategy @option body [Hash] the body of the response from the source

# File lib/api_client_builder/request.rb, line 11
def initialize(type, response_handler, body = {})
  @type = type
  @response_handler = response_handler
  @body = body
  @error_handlers_collection = []
end

Public Instance Methods

error_handlers() click to toggle source

Yields the collection of error handlers that have been populated via the on_error interface. If none are defined, this will provide a default error handler that will provide context about the error and also how to define a new error handler.

@return [Array<Block>] the error handlers collection

# File lib/api_client_builder/request.rb, line 24
    def error_handlers
      if error_handlers_collection.empty?
        on_error do |page, _handler|
          raise DefaultPageError, <<~MESSAGE
            Default error for bad response. If you want to handle this error use #on_error
            on the response in your api consumer. Error Code: #{page.status_code}.
          MESSAGE
        end
      end
      error_handlers_collection
    end
on_error(&block) click to toggle source

Used to define custom error handling on this response. The error handlers will be called if there is not a success

@param block [Lambda] the error handling block to be stored in

the error_handlers list
# File lib/api_client_builder/request.rb, line 41
def on_error(&block)
  @error_handlers_collection << block
end

Private Instance Methods

attempt_retry() click to toggle source
# File lib/api_client_builder/request.rb, line 47
def attempt_retry
  page = response_handler.retry_request

  if page.success?
    response_handler.reset_retries
    return page
  elsif response_handler.retryable?(page.status_code)
    attempt_retry
  else
    notify_error_handlers(page)
  end
end
notify_error_handlers(page) click to toggle source
# File lib/api_client_builder/request.rb, line 60
def notify_error_handlers(page)
  error_handlers.each do |handler|
    handler.call(page, self)
  end
end