lib/finapps/rest/base_client.rb in finapps-2.0.6 vs lib/finapps/rest/base_client.rb in finapps-2.0.10

- old
+ new

@@ -1,22 +1,27 @@ +# frozen_string_literal: true module FinApps module REST - class BaseClient # :nodoc: + # base client functionality + class BaseClient include ::FinApps::Utils::Loggeable + include ::FinApps::REST::Connection + using ObjectExtensions + using StringExtensions attr_reader :config - def initialize(options={}, logger=nil) + def initialize(options, logger=nil) @config = FinApps::REST::Configuration.new options @logger = logger end # Returns an initialized Faraday connection object. # # @return Faraday::Connection. def connection - @connection ||= FinApps::REST::Connection.faraday(config, logger) + @connection ||= faraday(config, logger) end # Performs HTTP GET, POST, UPDATE and DELETE requests. # You shouldn't need to use this method directly, but it can be useful for debugging. # Returns a hash obtained from parsing the JSON object in the response body. @@ -28,40 +33,51 @@ def send_request(path, method, params={}) raise FinApps::MissingArgumentsError.new 'Missing argument: path.' if path.blank? raise FinApps::MissingArgumentsError.new 'Missing argument: method.' if method.blank? response, error_messages = execute_request(method, params, path) - result = if response.present? - block_given? ? yield(response) : response.body - else + result = if response.blank? logger.error "##{__method__} => Null response found. Unable to process it." nil + else + block_given? ? yield(response) : response.body end [result, error_messages] end private def execute_request(method, params, path) error_messages = [] - begin response = execute_method method, params, path rescue FinApps::InvalidArgumentsError, FinApps::MissingArgumentsError, Faraday::Error::ConnectionFailed => error - logger.fatal "##{__method__} => #{error}" - raise error + handle_error error rescue Faraday::Error::ClientError => error - error_messages = error.response[:error_messages].blank? ? [error.message] : error.response[:error_messages] - logger.error "##{__method__} => Faraday::Error::ClientError, #{error}" + error_messages = handle_client_error error rescue StandardError => error - error_messages << 'Unexpected error.' - logger.fatal "##{__method__} => StandardError, #{error}" + error_messages = handle_standard_error error end [response, error_messages] + end + + def handle_error(error) + logger.fatal "#{self.class}##{__method__} => #{error}" + raise error + end + + def handle_client_error(error) + logger.error "#{self.class}##{__method__} => Faraday::Error::ClientError, #{error}" + error.response[:error_messages] || [error.message] + end + + def handle_standard_error(error) + logger.error "#{self.class}##{__method__} => StandardError, #{error}" + ['Unexpected error.'] end def execute_method(method, params, path) case method when :get