Sha256: d2b5e2077229304f06624dfb8f06e3a42630bc77172730a30d7f71a8281bc5da

Contents?: true

Size: 1.9 KB

Versions: 19

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module FlowcommerceSpree
  class LoggingHttpClient < ::Io::Flow::V0::HttpClient::DefaultHttpHandlerInstance
    attr_reader :error

    def initialize(base_uri, logger: FlowcommerceSpree.logger)
      super(base_uri)
      @logger = logger
    end

    def execute(request)
      # original_open = client.open_timeout
      # original_read = client.read_timeout

      start_time = Time.now.utc.round(10)

      # Contrived example to show how client settings can be adjusted
      # if request.path.start_with?('/organizations')
      #   client.open_timeout = 60
      #   client.read_timeout = 60
      # end

      begin
        response = super
      rescue Io::Flow::V0::HttpClient::ServerError => e
        @error = { error: Oj.load(e.body), code: e.code, status: e.details }
        raise exception_to_raise(e), @error.dig(:error, 'messages')
      ensure
        # client.open_timeout = original_open
        # client.read_timeout = original_read

        log_request(request, response, start_time)
      end
    end

    private

    def exception_to_raise(flow_io_exception)
      resource = flow_io_exception.uri.split('/').last
      exception = case resource
                    when 'reversals', 'refunds'
                      'Spree::Core::GatewayError'
                    else
                      'StandardError'
                  end
      exception.constantize
    end

    def log_request(request, response, start_time)
      duration = ((Time.now.utc.round(10) - start_time) * 1000).round(0)

      @logger.info("Started #{request.method} #{request.path}\n"\
                     "headers: #{request.instance_variable_get(:@header)}\nbody: #{request.body}\n"\
                     "response: #{response&.force_encoding('utf-8')}\n"\
                     "Completed #{request.method} #{request.path} #{duration} ms\n")

      @logger.info "Error: #{@error.inspect}" if @error
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
flowcommerce_spree-0.0.22 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.21 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.20 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.19 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.18 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.17 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.16 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.15 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.14 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.13 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.12 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.11 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.10 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.9 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.8 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.7 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.6 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.5 lib/flowcommerce_spree/logging_http_client.rb
flowcommerce_spree-0.0.4 lib/flowcommerce_spree/logging_http_client.rb