package middleware import "net/http" // Middleware wraps an http.RoundTripper to add behavior. // This is the fundamental building block of the httpx library. type Middleware func(http.RoundTripper) http.RoundTripper // 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 Chain(mws ...Middleware) Middleware { return func(rt http.RoundTripper) http.RoundTripper { for i := len(mws) - 1; i >= 0; i-- { rt = mws[i](rt) } return rt } } // RoundTripperFunc is an adapter to allow the use of ordinary functions as // http.RoundTripper. It works exactly like http.HandlerFunc for handlers. type RoundTripperFunc func(*http.Request) (*http.Response, error) // RoundTrip implements http.RoundTripper. func (f RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) }