httpclient

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 6 Imported by: 0

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

View Source
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.

View Source
const MaxResponseSize = 1 << 20

MaxResponseSize limits HTTP response bodies to 1 MB to prevent OOM.

Variables

This section is empty.

Functions

func Do

func Do(req *http.Request) (*http.Response, error)

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

func DownloadBytesCtx(ctx context.Context, url string, maxBytes int64) ([]byte, error)

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

func DownloadCtx(ctx context.Context, url, destPath string, maxBytes int64) (int64, error)

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

func GetJSON(url string) ([]byte, error)

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

func GetJSONCtx(ctx context.Context, url string) ([]byte, error)

GetJSONCtx performs a context-aware GET request and returns the response body. Returns an error for non-2xx status codes or on ctx cancellation.

func Init

func Init(timeout time.Duration)

Init creates a new HTTP client with the given timeout. Must be called once during startup before any external calls.

func Truncate

func Truncate(s string, maxLen int) string

Truncate shortens a string to maxLen characters, appending "..." if truncated.

Types

This section is empty.

Jump to

Keyboard shortcuts

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