httpx

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpx provides the HTTP transport shared by every client in the CLI. It sends the CLI user agent, maps HTTP statuses onto typed application errors, retries safe reads with backoff, and writes one redacted debug record per attempt.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RedactBody

func RedactBody(b []byte) string

RedactBody returns a log-safe rendering of a request or response body. JSON bodies keep their structure with sensitive values replaced; form bodies are re-encoded the same way. Anything else is reduced to "<N bytes>".

func RedactHeader

func RedactHeader(h http.Header) map[string]string

RedactHeader returns the header values as a map with every sensitive header replaced by "REDACTED". Values of non-sensitive headers with several entries are joined with ", ".

func RedactURL

func RedactURL(u *url.URL) string

RedactURL returns the URL with every sensitive query value and any user information replaced by "REDACTED". It is safe to put in a log line.

Types

type Client

type Client struct {
	// HTTP is the underlying client. Do falls back to http.DefaultClient when
	// it is nil.
	HTTP *http.Client
	// UserAgent is sent on every request.
	UserAgent string
	// Logger receives one debug record per attempt. A nil Logger disables
	// debug logging.
	Logger *slog.Logger
	// Attempts is the maximum number of attempts one Do call makes.
	Attempts int
	// Backoff returns how long to wait before retry attempt n (1 for the
	// first wait). A nil Backoff means no wait.
	Backoff func(attempt int) time.Duration
}

Client is the shared HTTP transport. It is safe for concurrent use.

func New

func New(userAgent string, logger *slog.Logger) *Client

New returns a client with a 30 second timeout, three attempts and an exponential backoff of 250 ms, 1 s and 4 s. A nil logger disables debug logging.

func (*Client) Do

func (c *Client) Do(ctx context.Context, r Request) (*Response, error)

Do sends the request, retrying retryable failures when r.Retry is set. It returns an *apperr.Error for any non-2xx status or transport failure, and ctx.Err() unwrapped when the context is done.

type Request

type Request struct {
	// Method is the HTTP method. An empty Method means GET.
	Method string
	// URL is the full request URL, query string included.
	URL string
	// Header holds extra request headers. It is cloned, never mutated.
	Header http.Header
	// Body is the request body, sent as-is.
	Body []byte
	// Retry allows retries on retryable failures. Only safe reads should set
	// it.
	Retry bool
	// Endpoint is the API path reported in errors and logs. It defaults to
	// the path of URL.
	Endpoint string
}

Request is one HTTP call. Endpoint defaults to the URL path and is the only location that appears in errors.

type Response

type Response struct {
	// Status is the HTTP status code.
	Status int
	// Header holds the response headers.
	Header http.Header
	// Body is the response body, capped at 8 MiB.
	Body []byte
}

Response is one HTTP response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL