http

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 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 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 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 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