Documentation
¶
Overview ¶
Package middleware provides client-side HTTP middleware for use with httpx.Client or any http.RoundTripper-based transport chain.
Each middleware is a function of type func(http.RoundTripper) http.RoundTripper. Compose them with Chain:
chain := middleware.Chain(
middleware.Logging(logger),
middleware.Recovery(),
middleware.UserAgent("my-service/1.0"),
)
transport := chain(http.DefaultTransport)
Available middleware ¶
- Logging — structured request/response logging via slog
- Recovery — panic recovery, converts panics to errors
- DefaultHeaders — adds default headers to outgoing requests
- UserAgent — sets User-Agent header
- BearerAuth — dynamic Bearer token authentication
- BasicAuth — HTTP Basic authentication
- RequestID — propagates request ID from context to X-Request-Id header
RoundTripperFunc ¶
RoundTripperFunc adapts plain functions to http.RoundTripper, similar to http.HandlerFunc. Useful for testing and inline middleware.
Index ¶
- type Middleware
- func BasicAuth(username, password string) Middleware
- func BearerAuth(tokenFunc func(ctx context.Context) (string, error)) Middleware
- func Chain(mws ...Middleware) Middleware
- func DefaultHeaders(headers http.Header) Middleware
- func Logging(logger *slog.Logger) Middleware
- func Recovery() Middleware
- func RequestID() Middleware
- func UserAgent(ua string) Middleware
- type RoundTripperFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Middleware ¶
type Middleware func(http.RoundTripper) http.RoundTripper
Middleware wraps an http.RoundTripper to add behavior. This is the fundamental building block of the httpx library.
func BasicAuth ¶
func BasicAuth(username, password string) Middleware
BasicAuth returns a middleware that sets HTTP Basic Authentication credentials on every outgoing request.
func BearerAuth ¶
func BearerAuth(tokenFunc func(ctx context.Context) (string, error)) Middleware
BearerAuth returns a middleware that sets the Authorization header to a Bearer token obtained by calling tokenFunc on each request. If tokenFunc returns an error, the request is not sent and the error is returned.
func Chain ¶
func Chain(mws ...Middleware) Middleware
Chain composes middlewares so that Chain(A, B, C)(base) == A(B(C(base))). Middlewares are applied from right to left: C wraps base first, then B wraps the result, then A wraps last. This means A is the outermost layer and sees every request first.
func DefaultHeaders ¶
func DefaultHeaders(headers http.Header) Middleware
DefaultHeaders returns a middleware that adds the given headers to every outgoing request. Existing headers on the request are not overwritten.
func Logging ¶
func Logging(logger *slog.Logger) Middleware
Logging returns a middleware that logs each request's method, URL, status code, duration, and error (if any) using the provided structured logger. Successful responses are logged at Info level; errors at Error level.
func Recovery ¶
func Recovery() Middleware
Recovery returns a middleware that recovers from panics in the inner RoundTripper chain. A recovered panic is converted to an error wrapping the panic value.
func RequestID ¶
func RequestID() Middleware
RequestID returns a middleware that propagates the request ID from the request context to the outgoing X-Request-Id header. This pairs with the server.RequestID middleware: the server stores the ID in the context, and the client middleware forwards it to downstream services.
func UserAgent ¶
func UserAgent(ua string) Middleware
UserAgent returns a middleware that sets the User-Agent header on every outgoing request, unless one is already present.
type RoundTripperFunc ¶
RoundTripperFunc is an adapter to allow the use of ordinary functions as http.RoundTripper. It works exactly like http.HandlerFunc for handlers.