http

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxRetries   = 10
	DefaultInitialDelay = 10 * time.Second
	DefaultMaxDelay     = 300 * time.Second
)

Variables

This section is empty.

Functions

func BuildRetryOptions

func BuildRetryOptions(ctx context.Context, url string, maxRetries uint, initialDelay, maxDelay time.Duration) []retry.Option

BuildRetryOptions constructs retry options with custom settings. Useful for tests or custom retry behavior.

func CheckStatusCode

func CheckStatusCode(resp *http.Response, body []byte, expectedCode int) error

func DefaultHTTPClient

func DefaultHTTPClient() *http.Client

30-second timeout used across most API clients.

func DefaultRetryOptions

func DefaultRetryOptions(ctx context.Context, url string) []retry.Option

func GetJSONBytes added in v0.1.2

func GetJSONBytes(ctx context.Context, url string, opts ...RequestOption) ([]byte, int, error)

GetJSONBytes performs a GET and returns the raw JSON response body together with the HTTP status code. On a non-2xx status it returns that status alongside a non-nil error so callers can branch on the code (for example 404 vs 401) while still surfacing transport failures (status 0).

func GetJSONCached

func GetJSONCached[T any](ctx context.Context, url string, cacheOpts *CacheOptions, opts ...RequestOption) (*T, error)

GetJSONCached is a cached variant of GetJSON that stores responses in the output directory. It checks the cache first and falls back to GetJSON on cache miss. Cache location: {outputDir}/http-cache/{sha256(url)}.json (or custom CacheKey if provided) Cache max age: HTTP_CACHE_MAX_AGE_DAYS env var (default: 1 day) or custom MaxAge if provided

func GetRetryOptions

func GetRetryOptions(ctx context.Context) ([]retry.Option, bool)

func GetStatusCodeFromError

func GetStatusCodeFromError(err error) (int, bool)

func IsRetryableStatusCode

func IsRetryableStatusCode(code int) bool

func NewHTTPClientWithTimeout

func NewHTTPClientWithTimeout(timeout time.Duration) *http.Client

func PostJSONBytes added in v0.1.3

func PostJSONBytes(ctx context.Context, url string, body []byte, opts ...RequestOption) ([]byte, int, error)

PostJSONBytes POSTs a raw JSON body (e.g. an SBOM document) verbatim and returns the raw JSON response body together with the HTTP status code. On a non-2xx status it returns that status alongside a non-nil error so callers can branch on the code (for example 401 vs 502).

func PostJSONCached

func PostJSONCached[T any](ctx context.Context, url string, reqBody any, cacheOpts *CacheOptions, opts ...RequestOption) (*T, error)

PostJSONCached is a cached variant of PostJSON that stores responses in the output directory. It checks the cache first based on the URL and request body, and falls back to PostJSON on cache miss. Cache location: {outputDir}/http-cache/{sha256(url+body)}.json (or custom CacheKey if provided) Cache max age: HTTP_CACHE_MAX_AGE_DAYS env var (default: 1 day) or custom MaxAge if provided

func PostMultipart added in v0.1.3

func PostMultipart(ctx context.Context, url, contentType string, body []byte, opts ...RequestOption) ([]byte, int, error)

PostMultipart POSTs a pre-built body with the given Content-Type (a multipart/form-data body from mime/multipart) and returns the raw response body together with the HTTP status code. On a non-2xx status it returns that status alongside a non-nil error so callers can branch on the code.

func ReadAndValidateResponse

func ReadAndValidateResponse(resp *http.Response, expectedCode int) ([]byte, error)

func ReadResponseBody

func ReadResponseBody(resp *http.Response) ([]byte, error)

func SetRetryOptions

func SetRetryOptions(ctx context.Context, opts []retry.Option) context.Context

SetRetryOptions stores retry options in context for use by HTTP clients. Used by tests to configure fast retry settings (see client_test.go setupTestContext). Note: deadcode tool reports this as unreachable but it's called from test code.

func TruncateBodyPreview

func TruncateBodyPreview(body []byte, maxLen int) string

Types

type CacheOptions

type CacheOptions struct {
	CacheKey string        // Optional custom cache key relative to outputDir, e.g. "nvd/CVE-2023-1234" becomes {outputDir}/nvd/CVE-2023-1234.json
	MaxAge   time.Duration // Optional max age (defaults to HTTP_CACHE_MAX_AGE_DAYS env var or 1 day)
}

CacheOptions holds optional cache configuration

type RequestOption

type RequestOption func(*requestConfig)

func WithHeader

func WithHeader(key, value string) RequestOption

type StatusCodeError

type StatusCodeError struct {
	StatusCode int
	Body       string
}

func (*StatusCodeError) Error

func (e *StatusCodeError) Error() string

Jump to

Keyboard shortcuts

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