Class: FlowcommerceSpree::LoggingHttpClient

Inherits:
Io::Flow::V0::HttpClient::DefaultHttpHandlerInstance
  • Object
show all
Defined in:
lib/flowcommerce_spree/logging_http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, logger: FlowcommerceSpree.logger) ⇒ LoggingHttpClient

Returns a new instance of LoggingHttpClient.



7
8
9
10
# File 'lib/flowcommerce_spree/logging_http_client.rb', line 7

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

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



5
6
7
# File 'lib/flowcommerce_spree/logging_http_client.rb', line 5

def error
  @error
end

Instance Method Details

#execute(request) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/flowcommerce_spree/logging_http_client.rb', line 12

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