httpx

package
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 13 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 wraps net/http with retry, structured logging, and the User-Agent string the project uses for every outbound request. It is the foundation for github-metrics' GitHub REST and GraphQL clients (internal/githubapi) and for any other external fetcher (twemoji, octicons, gemoji) that lands in later phases.

Index

Constants

View Source
const DefaultUserAgent = "github-metrics/0 (https://github.com/mjun0812/github-metrics)"

DefaultUserAgent is used when Options.UserAgent is empty. Callers (notably the github-metrics action binary) typically override it with a more specific string at start-up.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the thin wrapper around retryablehttp that the rest of the project consumes. Construct one per process (or per binary mode); safe for concurrent use.

func New

func New(opts Options) *Client

New constructs a Client using opts; zero-value Options is a valid production default for a tool that talks to small handful of HTTP endpoints (mainly api.github.com plus a few asset CDNs).

func (*Client) Binary

func (c *Client) Binary(ctx context.Context, target string) ([]byte, string, error)

Binary fetches an arbitrary blob, returning the body bytes and the reported MIME (Content-Type with parameters stripped).

func (*Client) Get

func (c *Client) Get(ctx context.Context, target string, header http.Header) ([]byte, *http.Response, error)

Get issues a GET request. Header is merged onto User-Agent (caller wins for duplicate keys).

func (*Client) HTTPClient

func (c *Client) HTTPClient() *http.Client

HTTPClient returns the underlying *http.Client. Exposed so callers that need a stdlib client (e.g. genqlient.NewClient) can hand it the retry-enabled instance without re-implementing transport plumbing.

func (*Client) ImgB64

func (c *Client) ImgB64(ctx context.Context, target string) (string, error)

ImgB64 fetches an image and returns a `data:<mime>;base64,<payload>` string suitable for embedding directly in SVG/HTML.

func (*Client) PostForm

func (c *Client) PostForm(ctx context.Context, target string, values url.Values) ([]byte, *http.Response, error)

PostForm POSTs application/x-www-form-urlencoded data.

func (*Client) PostJSON

func (c *Client) PostJSON(ctx context.Context, target string, body any, header http.Header) ([]byte, *http.Response, error)

PostJSON marshals body as application/json and POSTs it.

type Options

type Options struct {
	// UserAgent overrides DefaultUserAgent.
	UserAgent string
	// Logger receives retry/backoff messages. nil falls back to a
	// no-op slog handler so production binaries can route logs
	// explicitly.
	Logger *slog.Logger
	// Transport is the underlying RoundTripper. nil uses
	// retryablehttp's default (which wraps http.DefaultTransport).
	// internal/githubapi swaps this for mock transports during tests.
	Transport http.RoundTripper
	// MaxRetries caps the number of retry attempts after the initial
	// request. Default 4 (so total = 5 attempts). Ignored when
	// DisableRetries is true.
	MaxRetries int
	// MinBackoff is the floor for the exponential backoff. Default 1s.
	MinBackoff time.Duration
	// MaxBackoff is the ceiling for the exponential backoff.
	// Default 30s.
	MaxBackoff time.Duration
	// DisableRetries turns off the inner retryablehttp loop entirely.
	// Callers that own retry classification at a higher layer (M6
	// action package's RetryPolicy) MUST set this true to avoid the
	// double-retry tower (1+MaxRetries)·(1+action.Retries) attempts,
	// which is especially harmful for non-idempotent POST /pulls and
	// POST /git/refs (would create duplicate PRs / refs on a 5xx after
	// a successful server-side write).
	DisableRetries bool
}

Options controls Client construction. Zero values are safe; see field docs for the defaults each one falls back to.

type RateLimitKind added in v1.2.1

type RateLimitKind int

RateLimitKind describes how GitHub signalled the rate limit.

const (
	// RateLimitSecondary indicates a secondary/abuse limit: the server
	// responded with a Retry-After header (GitHub docs: "secondary rate limits").
	RateLimitSecondary RateLimitKind = iota
	// RateLimitPrimary indicates primary quota exhaustion: the server set
	// x-ratelimit-remaining to "0" and x-ratelimit-reset to a reset epoch.
	RateLimitPrimary
)

type RateLimitedError added in v1.2.1

type RateLimitedError struct {
	// Kind is whether this was a secondary (Retry-After) or primary
	// (x-ratelimit-remaining=0) limit.
	Kind RateLimitKind
	// RetryAt is when GitHub says the limit resets. It may be zero if the
	// server did not supply a parseable reset time.
	RetryAt time.Time
}

RateLimitedError is returned (or wrapped) when all retry attempts are exhausted due to a GitHub rate-limit response, or when the requested wait exceeds rateLimitBackoffCap.

Callers that want to distinguish "rate limited" from "forbidden (no access)" can use errors.As:

var rle *httpx.RateLimitedError
if errors.As(err, &rle) { /* rate limited */ }

func ClassifyRateLimit added in v1.2.1

func ClassifyRateLimit(resp *http.Response) *RateLimitedError

ClassifyRateLimit inspects a non-2xx *http.Response (typically 403 or 429) and returns a *RateLimitedError if the response carries rate-limit headers, or nil if the response is a plain permission error / unrelated failure.

#531 should call this after receiving a 403 from a REST endpoint:

var rle *httpx.RateLimitedError
if rle = httpx.ClassifyRateLimit(resp); rle != nil {
    // rate limited — surface rle to caller
}

func (*RateLimitedError) Error added in v1.2.1

func (e *RateLimitedError) Error() string

Jump to

Keyboard shortcuts

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