Class: HTTP::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/http/feature.rb

Overview

Base class for HTTP client features (middleware)

Instance Method Summary collapse

Instance Method Details

#around_request(request) {|HTTP::Request| ... } ⇒ HTTP::Response

Wraps the HTTP exchange for a single request attempt

Called once per attempt (including retries), wrapping the send and receive cycle. The block performs the I/O and returns the response. Override this to add behavior that must span the entire exchange, such as instrumentation spans or circuit breakers.

Examples:

Timing a request

def around_request(request)
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  yield(request).tap { log_duration(Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) }
end

Parameters:

Yields:

Yield Returns:

Returns:



62
63
64
# File 'lib/http/feature.rb', line 62

def around_request(request)
  yield request
end

#on_error(_request, _error) ⇒ nil

Callback for request errors

Examples:

feature.on_error(request, error)

Parameters:

Returns:

  • (nil)


75
# File 'lib/http/feature.rb', line 75

def on_error(_request, _error); end

#on_request(_request) ⇒ nil

Callback invoked before each request attempt

Unlike #wrap_request, which is called once when the request is built, this hook is called before every attempt, including retries. Use it for per-attempt side effects like starting instrumentation spans.

Examples:

feature.on_request(request)

Parameters:

Returns:

  • (nil)


42
# File 'lib/http/feature.rb', line 42

def on_request(_request); end

#wrap_request(request) ⇒ HTTP::Request

Wraps an HTTP request

Examples:

feature.wrap_request(request)

Parameters:

Returns:



14
15
16
# File 'lib/http/feature.rb', line 14

def wrap_request(request)
  request
end

#wrap_response(response) ⇒ HTTP::Response

Wraps an HTTP response

Examples:

feature.wrap_response(response)

Parameters:

Returns:



26
27
28
# File 'lib/http/feature.rb', line 26

def wrap_response(response)
  response
end