Documentation
¶
Overview ¶
Package httpclient wraps net/http.Client with a small, context-first API and composable middleware. It is intentionally thin: the goal is to reduce per-call boilerplate (base URL joining, auth headers, JSON body handling) without hiding net/http semantics.
Index ¶
- func DecodeJSON[T any](r *http.Response) (T, error)
- func JSONBody(v any) *bytes.Reader
- type Client
- func (c *Client) Delete(ctx context.Context, path string, mws ...Middleware) (*http.Response, error)
- func (c *Client) Do(req *http.Request, mws ...Middleware) (*http.Response, error)
- func (c *Client) Get(ctx context.Context, path string, mws ...Middleware) (*http.Response, error)
- func (c *Client) Patch(ctx context.Context, path string, body io.Reader, mws ...Middleware) (*http.Response, error)
- func (c *Client) Post(ctx context.Context, path string, body io.Reader, mws ...Middleware) (*http.Response, error)
- func (c *Client) Put(ctx context.Context, path string, body io.Reader, mws ...Middleware) (*http.Response, error)
- type Doer
- type DoerFunc
- type Middleware
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeJSON ¶
DecodeJSON decodes r.Body into a fresh T and closes the body. Use as:
resp, err := client.Get(ctx, "/users/1")
if err != nil { return err }
u, err := httpclient.DecodeJSON[User](resp)
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP client with a base URL, a middleware chain, and shortcut methods for common verbs. It is safe for concurrent use once configured.
func New ¶
func New(httpClient *http.Client, baseURL string, mws ...Middleware) *Client
New constructs a Client dispatching via the given http.Client (nil means http.DefaultClient) with baseURL prepended to relative request paths. Middlewares are applied in the order given; the first runs closest to the caller (outermost wrapper).
func (*Client) Delete ¶
func (c *Client) Delete(ctx context.Context, path string, mws ...Middleware) (*http.Response, error)
Delete issues a DELETE request.
func (*Client) Do ¶
Do sends req through the client's middleware chain, wrapped on the outside by any per-call middleware. Per-call middleware registered first runs closest to the caller, and all per-call middleware run before the chain configured on the client.
func (*Client) Get ¶
Get issues a GET request to path (joined with the base URL if relative). Per-call middleware follows the same ordering rules as Client.Do.
func (*Client) Patch ¶
func (c *Client) Patch(ctx context.Context, path string, body io.Reader, mws ...Middleware) (*http.Response, error)
Patch issues a PATCH request with body.
type Doer ¶
Doer is the minimal interface needed to dispatch an HTTP request. Both *http.Client and middleware wrappers satisfy it.
type Middleware ¶
Middleware wraps a Doer to inject behavior (auth, logging, retries, ...) on every request passing through it. Middlewares compose by wrapping: the first registered runs closest to the outer caller.
func BearerAuth ¶
func BearerAuth(token func() string) Middleware
BearerAuth returns a Middleware that sets `Authorization: Bearer <token>` on every outgoing request. Token is resolved per-request via the supplied function, so rotation is handled by returning a fresh value; empty strings are skipped so anonymous requests pass through.
func Header ¶
func Header(key, value string) Middleware
Header returns a Middleware that sets a static header on every request. Existing values for the same key are replaced.
func JSONContent ¶
func JSONContent() Middleware
JSONContent returns a Middleware that sets `Content-Type: application/json` on requests that have a body and do not already specify a content type.