Documentation
¶
Index ¶
- Constants
- func BuildRetryOptions(ctx context.Context, url string, maxRetries uint, ...) []retry.Option
- func CheckStatusCode(resp *http.Response, body []byte, expectedCode int) error
- func DefaultHTTPClient() *http.Client
- func DefaultRetryOptions(ctx context.Context, url string) []retry.Option
- func GetJSONBytes(ctx context.Context, url string, opts ...RequestOption) ([]byte, int, error)
- func GetJSONCached[T any](ctx context.Context, url string, cacheOpts *CacheOptions, ...) (*T, error)
- func GetRetryOptions(ctx context.Context) ([]retry.Option, bool)
- func GetStatusCodeFromError(err error) (int, bool)
- func IsRetryableStatusCode(code int) bool
- func NewHTTPClientWithTimeout(timeout time.Duration) *http.Client
- func PostJSONBytes(ctx context.Context, url string, body []byte, opts ...RequestOption) ([]byte, int, error)
- func PostJSONCached[T any](ctx context.Context, url string, reqBody any, cacheOpts *CacheOptions, ...) (*T, error)
- func PostMultipart(ctx context.Context, url, contentType string, body []byte, ...) ([]byte, int, error)
- func ReadAndValidateResponse(resp *http.Response, expectedCode int) ([]byte, error)
- func ReadResponseBody(resp *http.Response) ([]byte, error)
- func SetRetryOptions(ctx context.Context, opts []retry.Option) context.Context
- func TruncateBodyPreview(body []byte, maxLen int) string
- type CacheOptions
- type RequestOption
- type StatusCodeError
Constants ¶
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 DefaultHTTPClient ¶
30-second timeout used across most API clients.
func GetJSONBytes ¶ added in v0.1.2
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 GetStatusCodeFromError ¶
func IsRetryableStatusCode ¶
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 SetRetryOptions ¶
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 ¶
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 ¶
func (*StatusCodeError) Error ¶
func (e *StatusCodeError) Error() string