httpx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package httpx provides HTTP plumbing shared by provider API clients: a redacting debug-logging transport, a conservative retry transport that understands HTTP 429 / Retry-After, Link header parsing and small helpers.

It deliberately contains no provider-specific behavior. Each driver owns its API client (base URL, auth headers, pagination, error mapping) and only borrows these building blocks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON(resp *http.Response, v any) error

DecodeJSON decodes a JSON response body into v.

func NewClient

func NewClient(o Options) *http.Client

NewClient builds an *http.Client with logging and retry transports.

func ParseLinkHeader

func ParseLinkHeader(h string) map[string]string

ParseLinkHeader parses an RFC 8288 Link header into rel -> URL.

func ReadBody

func ReadBody(resp *http.Response, limit int64) []byte

ReadBody reads up to limit bytes of a response body.

func RetryAfter

func RetryAfter(h http.Header) time.Duration

RetryAfter parses a Retry-After header (delta-seconds or HTTP date).

func Sleep

func Sleep(ctx context.Context, d time.Duration) error

Sleep waits for d or until ctx is done.

func UserAgent

func UserAgent() string

UserAgent is sent with every API request.

func WithNoRetry

func WithNoRetry(ctx context.Context) context.Context

WithNoRetry disables retries for a request.

func WithRetrySafe

func WithRetrySafe(ctx context.Context) context.Context

WithRetrySafe marks a non-idempotent request as safe to retry.

Types

type LoggingTransport

type LoggingTransport struct {
	Base   http.RoundTripper
	Logger *slog.Logger
}

LoggingTransport logs requests at debug level with credentials redacted.

func (*LoggingTransport) RoundTrip

func (t *LoggingTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

type Options

type Options struct {
	Timeout time.Duration
	Logger  *slog.Logger
	Retry   RetryPolicy
	// Base is the underlying transport (defaults to http.DefaultTransport).
	Base http.RoundTripper
}

Options configure NewClient.

type RetryPolicy

type RetryPolicy struct {
	MaxRetries int
	BaseDelay  time.Duration
	MaxDelay   time.Duration
	// MaxWait is the longest Retry-After we are willing to sleep for. Longer
	// waits surface as rate-limit errors to the caller instead.
	MaxWait time.Duration
	// RateLimited lets a driver recognize provider-specific rate limit
	// responses (e.g. GitHub's 403 + x-ratelimit-remaining: 0) and return the
	// wait duration if known.
	RateLimited func(resp *http.Response) (limited bool, wait time.Duration)
	// Disabled turns off retries entirely.
	Disabled bool
}

RetryPolicy controls RetryTransport.

type RetryTransport

type RetryTransport struct {
	Base   http.RoundTripper
	Policy RetryPolicy
	Logger *slog.Logger
}

RetryTransport retries idempotent requests on 429, transient 5xx and network errors with exponential backoff and jitter, honoring Retry-After and context cancellation. Non-idempotent requests (POST, PATCH, DELETE...) are never retried unless marked with WithRetrySafe.

func (*RetryTransport) RoundTrip

func (t *RetryTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

Jump to

Keyboard shortcuts

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