httputil

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package httputil provides shared HTTP utilities for API clients.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAPIError

func IsAPIError(err error) bool

IsAPIError reports whether err (or anything it wraps) is an *APIError.

Types

type APIError

type APIError struct {
	Provider   Provider
	StatusCode int
	Body       string
	Hint       string
}

APIError is a typed error carrying an upstream HTTP status code along with a provider-specific, user-actionable hint. Clients construct one via Classify when an upstream returns a non-2xx response; callers can recover the status code and provider via errors.As.

func Classify

func Classify(provider Provider, statusCode int, body []byte) *APIError

Classify produces an APIError with provider-specific guidance for the given status code. Status codes outside the curated set get a generic hint so the caller can pass through unknown failure modes without losing information.

func (*APIError) Error

func (e *APIError) Error() string

type HTTPDoer

type HTTPDoer interface {
	Do(*http.Request) (*http.Response, error)
}

HTTPDoer is the interface satisfied by *http.Client; use it in API client structs so tests can inject a mock without a real network.

type Provider

type Provider string

Provider names a supported upstream API.

const (
	ProviderJIRA   Provider = "JIRA"
	ProviderGitHub Provider = "GitHub"
	ProviderSnyk   Provider = "Snyk"
)

type RateLimiter

type RateLimiter struct {
	BaseDelay  time.Duration
	MaxDelay   time.Duration
	MaxRetries int
}

RateLimiter configures exponential backoff with jitter for rate-limited APIs.

func Default

func Default() *RateLimiter

Default returns a RateLimiter with the standard configuration used across all API clients (2 s base, 30 s max, 5 retries).

func (*RateLimiter) Backoff

func (r *RateLimiter) Backoff(attempt int, retryAfter string) time.Duration

Backoff returns the delay before the next retry attempt. If the Retry-After response header is a valid integer, that value is used directly. Otherwise exponential backoff with ±30 % jitter is applied, capped at MaxDelay.

Jump to

Keyboard shortcuts

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