Documentation
¶
Overview ¶
Package httpx is an opinionated outbound HTTP client for hex apps.
It wraps *http.Client with:
- retries via hex/retry (exponential backoff, respects Retry-After),
- default timeout,
- structured request/response logging through hex/log,
- a request-ID header for cross-service correlation.
Consumers who need something the wrapper does not expose can reach the underlying *http.Client via Client.Underlying.
Example:
c := httpx.New(httpx.Options{
Timeout: 10 * time.Second,
MaxAttempts: 3,
BaseBackoff: 500 * time.Millisecond,
})
resp, err := c.Do(ctx, req)
if err != nil { return err }
defer resp.Body.Close()
Index ¶
- type Client
- func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, error)
- func (c *Client) Get(ctx context.Context, url string) (*http.Response, error)
- func (c *Client) Post(ctx context.Context, url, contentType string, body io.Reader) (*http.Response, error)
- func (c *Client) Underlying() *http.Client
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a hex outbound HTTP client. Safe for concurrent use.
func (*Client) Do ¶
Do executes req with retry, backoff, and logging. The request body must be re-readable across attempts; if it is not, Do buffers it once at the start.
func (*Client) Post ¶
func (c *Client) Post(ctx context.Context, url, contentType string, body io.Reader) (*http.Response, error)
Post is a convenience wrapper for POST with a request body.
func (*Client) Underlying ¶
Underlying returns the wrapped *http.Client. Use only when Client's higher-level surface is insufficient — bypassing it skips retry, logging, and default headers.
type Options ¶
type Options struct {
// Timeout is the per-request timeout applied to every attempt (not
// the total call). Zero means 30 seconds.
Timeout time.Duration
// MaxAttempts caps total attempts (initial + retries). Zero means 3.
// Set to 1 to disable retries.
MaxAttempts int
// BaseBackoff is the base delay between retries. Zero means 200ms.
BaseBackoff time.Duration
// MaxBackoff caps individual backoffs. Zero means 30s.
MaxBackoff time.Duration
// RetryOn is the set of HTTP status codes that trigger a retry
// (idempotent methods only). Zero-value defaults to
// {429, 500, 502, 503, 504}.
RetryOn []int
// UserAgent, when non-empty, is set on every outbound request unless
// the caller already set one.
UserAgent string
// Transport is the underlying http.RoundTripper. Zero uses
// http.DefaultTransport.
Transport http.RoundTripper
}
Options configures a Client.