Documentation
¶
Overview ¶
Package httpclient provides a shared HTTP client for all external API calls. The client is safe for concurrent use and reuses connections. Call Init once during startup to set the timeout; the default is 10s.
Index ¶
- Constants
- func Do(req *http.Request) (*http.Response, error)
- func DownloadBytesCtx(ctx context.Context, url string, maxBytes int64) ([]byte, error)
- func DownloadCtx(ctx context.Context, url, destPath string, maxBytes int64) (int64, error)
- func GetJSON(url string) ([]byte, error)
- func GetJSONCtx(ctx context.Context, url string) ([]byte, error)
- func Init(timeout time.Duration)
- func Truncate(s string, maxLen int) string
Constants ¶
const DefaultMaxDownloadSize = 200 << 20
DefaultMaxDownloadSize caps a single DownloadCtx response at 200 MB — comfortably larger than any truestamp release tarball today, small enough to prevent a runaway redirect from filling the disk.
const MaxResponseSize = 1 << 20
MaxResponseSize limits HTTP response bodies to 1 MB to prevent OOM.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do executes an HTTP request using the shared client. The request's existing context.Context (if any) is respected; callers that want cancellation should attach one via http.Request.WithContext before calling.
func DownloadBytesCtx ¶ added in v0.3.1
DownloadBytesCtx is like DownloadCtx but returns the body in memory. Intended for small artifacts (checksums.txt, signature bundles) that exceed MaxResponseSize occasionally but are still known to be small (<1 MB). Pass 0 for maxBytes to default to 1 MB.
func DownloadCtx ¶ added in v0.3.1
DownloadCtx streams the body of a GET request to destPath. The destination is created (or truncated) with 0644 permissions. The response body is read through an io.LimitReader whose cap is the larger of maxBytes and DefaultMaxDownloadSize; pass 0 to use the default.
Unlike GetJSONCtx, this function is safe for multi-MB responses and never buffers the full body in memory.
Returns the number of bytes written on success.
func GetJSON ¶
GetJSON performs a GET request with context.Background and returns the response body. Prefer GetJSONCtx when a cancellable context is available (e.g. from Cobra's cmd.Context()).
func GetJSONCtx ¶ added in v0.3.0
GetJSONCtx performs a context-aware GET request and returns the response body. Returns an error for non-2xx status codes or on ctx cancellation.
Types ¶
This section is empty.