httpfetch

package
v0.24.2 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package httpfetch is a minimal HTTP client wrapper that exposes a streaming Get plus an atomic Download (temp file in dest dir + rename). It's the shared transport for stepman's V2 inventory fetches and precompiled binary downloads.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	// Get streams the body of url. The caller closes the returned reader.
	// Non-2xx responses are returned as an error.
	Get(ctx context.Context, url string) (io.ReadCloser, error)
	// Download fetches url and atomically writes it to destPath. Missing
	// parent directories are created. A temp file is created alongside
	// destPath and renamed on success so partial downloads never appear at
	// the final path.
	Download(ctx context.Context, destPath, url string) error
	// DownloadWithHash behaves like Download but also verifies that the
	// downloaded content matches expectedHash ("sha256-<hex>"). The temp file
	// is removed and an error is returned if the hash does not match, so a
	// mismatched file never appears at destPath.
	DownloadWithHash(ctx context.Context, destPath, url, expectedHash string) error
}

Client streams or atomically downloads HTTP resources. Implementations returned by NewClient retry transient failures on both Get and Download.

func NewClient

func NewClient(logger Logger) Client

NewClient returns a Client backed by a retryablehttp client, so callers get transient-failure retries by default. Use NewWithClient to supply a specific *http.Client (e.g. a test server's client).

func NewWithClient

func NewWithClient(httpClient *http.Client) Client

NewWithClient returns a Client backed by the given httpClient, which must be non-nil. Prefer NewClient unless you need a specific transport.

type Logger

type Logger interface {
	Debugf(format string, v ...any)
}

Logger is the minimal logging interface Client needs; the retry adapter only emits debug lines.

type StatusError

type StatusError struct {
	URL  string
	Code int
	Body string
}

StatusError is returned by Get when the server responds with a non-2xx status, so callers can branch on the code (e.g. treat 404 as "not found") via errors.As. Body holds a bounded snippet of the response body, which usually explains the failure (a 404 page, S3's XML error, …).

func (*StatusError) Error

func (e *StatusError) Error() string

Jump to

Keyboard shortcuts

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