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
- type Client
- func (c *Client) Binary(ctx context.Context, target string) ([]byte, string, error)
- func (c *Client) Get(ctx context.Context, target string, header http.Header) ([]byte, *http.Response, error)
- func (c *Client) HTTPClient() *http.Client
- func (c *Client) ImgB64(ctx context.Context, target string) (string, error)
- func (c *Client) PostForm(ctx context.Context, target string, values url.Values) ([]byte, *http.Response, error)
- func (c *Client) PostJSON(ctx context.Context, target string, body any, header http.Header) ([]byte, *http.Response, error)
- type Options
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
ImgB64 fetches an image and returns a `data:<mime>;base64,<payload>` string suitable for embedding directly in SVG/HTML.
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.