httpclient

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package httpclient provides a common HTTP client for API sources.

Index

Constants

View Source
const (
	// DefaultMaxRetries is the default number of retry attempts for transient failures.
	DefaultMaxRetries = 3

	// DefaultInitialBackoff is the starting backoff duration before the first retry.
	DefaultInitialBackoff = 1 * time.Second
)
View Source
const GitHubDefaultURL = "https://api.github.com"

GitHubDefaultURL is the default GitHub API endpoint.

Variables

This section is empty.

Functions

func Get added in v1.1.3

func Get[T any](c *Client, ctx context.Context, endpoint string) (T, error)

Get performs a typed HTTP GET request and decodes the JSON response into T.

func GitHubRateLimitHook added in v1.1.3

func GitHubRateLimitHook(headers http.Header)

GitHubRateLimitHook inspects GitHub API rate-limit response headers and warns to stderr when the remaining request count is low.

func NormalizeBaseURL added in v1.0.0

func NormalizeBaseURL(baseURL, defaultURL string) string

NormalizeBaseURL ensures URL has no trailing slash and applies a default if empty. This is used by clients that need consistent URL handling for API endpoints.

Types

type AuthType

type AuthType int

AuthType defines how authentication is performed.

const (
	// AuthNone means no authentication header is set.
	AuthNone AuthType = iota
	// AuthBearer uses "Authorization: Bearer <token>".
	AuthBearer
	// AuthToken uses "Authorization: token <token>" (Snyk style).
	AuthToken
	// AuthBasic uses HTTP Basic auth with token as username and empty password.
	AuthBasic
)

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a configurable HTTP client for API requests.

func New

func New(cfg Config) *Client

New creates a new HTTP client with the given configuration.

func NewWithHTTPClient

func NewWithHTTPClient(cfg Config, httpClient *http.Client) *Client

NewWithHTTPClient creates a new HTTP client with a custom http.Client. This is primarily intended for testing.

func (*Client) BaseURL

func (c *Client) BaseURL() string

BaseURL returns the configured base URL.

type Config

type Config struct {
	BaseURL        string
	Token          string
	AuthType       AuthType
	Accept         string
	ExtraHeaders   map[string]string
	Timeout        time.Duration
	MaxRetries     int           // Max retry attempts (0 = use default; -1 = disable)
	InitialBackoff time.Duration // Starting backoff before first retry (0 = use default)
	OnResponse     ResponseHook  // Optional callback after successful responses
}

Config holds configuration for the HTTP client.

func GitHubConfig added in v1.0.0

func GitHubConfig(baseURL, token string, timeout time.Duration) Config

GitHubConfig returns httpclient.Config pre-configured for GitHub API.

func GitHubConfigWithVersion added in v1.0.0

func GitHubConfigWithVersion(baseURL, token string, timeout time.Duration, version string) Config

GitHubConfigWithVersion returns a Config including the API version header. Use this for APIs that require the X-GitHub-Api-Version header.

type ResponseHook added in v1.1.3

type ResponseHook func(headers http.Header)

ResponseHook is called after each successful HTTP response (status 200). It receives the response headers and can inspect rate-limit or other metadata.

Jump to

Keyboard shortcuts

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