Documentation
¶
Overview ¶
Package client provides an HTTP client with retry logic for registry APIs.
Index ¶
- Variables
- func BuildURLs(urls URLBuilder, name, version string) map[string]string
- type BaseURLs
- type Client
- func (c *Client) GetBody(ctx context.Context, url string) ([]byte, error)
- func (c *Client) GetJSON(ctx context.Context, url string, v any) error
- func (c *Client) GetText(ctx context.Context, url string) (string, error)
- func (c *Client) Head(ctx context.Context, url string) (int, error)
- func (c *Client) WithRateLimiter(rl RateLimiter) *Client
- func (c *Client) WithUserAgent(ua string) *Client
- type HTTPError
- type NotFoundError
- type Option
- type RateLimitError
- type RateLimiter
- type URLBuilder
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound is returned when a package or version is not found.
Functions ¶
Types ¶
type BaseURLs ¶
type BaseURLs struct {
RegistryFn func(name, version string) string
DownloadFn func(name, version string) string
DocumentationFn func(name, version string) string
PURLFn func(name, version string) string
}
BaseURLs provides a default URLBuilder implementation.
func (*BaseURLs) Documentation ¶
type Client ¶
type Client struct {
HTTPClient *http.Client
UserAgent string
MaxRetries int
BaseDelay time.Duration
RateLimiter RateLimiter
}
Client is an HTTP client with retry logic for registry APIs.
func DefaultClient ¶
func DefaultClient() *Client
DefaultClient returns a client with sensible defaults.
func (*Client) WithRateLimiter ¶
func (c *Client) WithRateLimiter(rl RateLimiter) *Client
WithRateLimiter returns a copy of the client with the given rate limiter.
func (*Client) WithUserAgent ¶
WithUserAgent returns a copy of the client with the given user agent.
type HTTPError ¶
HTTPError represents an HTTP error response.
func (*HTTPError) IsNotFound ¶
IsNotFound returns true if the error represents a 404 response.
type NotFoundError ¶
NotFoundError wraps ErrNotFound with additional context.
func (*NotFoundError) Error ¶
func (e *NotFoundError) Error() string
func (*NotFoundError) Unwrap ¶
func (e *NotFoundError) Unwrap() error
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithHTTPClient ¶
WithHTTPClient swaps in a caller-supplied *http.Client. Use this when the application has its own connection pool, mTLS config, or auth-injecting transport that other Options like WithTimeout can't express. The supplied client's Timeout, Jar, and Transport are preserved as-is.
func WithMaxRetries ¶
WithMaxRetries sets the maximum number of retries.
func WithSafeHTTP ¶
func WithSafeHTTP() Option
WithSafeHTTP wraps the client's underlying *http.Client with the safehttp transport: dial-time IP gate (rejects loopback, RFC1918, CGNAT, link-local, multicast, unspecified), redirect chain capped at 10, non-http(s) schemes rejected on redirect. DNS is resolved at dial time and the connection dials the resolved IP directly so a rebind between resolve and connect cannot escape the gate. Suitable for any code path that fetches from URLs an attacker might control (a malicious registry response, a manifest-supplied URL, a redirect target).
func WithTimeout ¶
WithTimeout sets the HTTP client timeout.
func WithTransport ¶
func WithTransport(rt http.RoundTripper) Option
WithTransport replaces the underlying http.RoundTripper without touching the Client's other settings (timeout, jar, etc.). Useful for wrapping the transport in middleware (auth, logging, retry, rate-limit) while keeping the rest of the configured shape intact.
type RateLimitError ¶
type RateLimitError struct {
RetryAfter int // seconds
}
RateLimitError is returned when the registry rate limits requests.
func (*RateLimitError) Error ¶
func (e *RateLimitError) Error() string
type RateLimiter ¶
RateLimiter controls request pacing.