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 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 PostJSONCached[T any](ctx context.Context, url string, reqBody any, cacheOpts *CacheOptions, ...) (*T, 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 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 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 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