httpclient

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package httpclient provides an HTTP client with auth, retry, and pagination support.

Package httpclient provides an HTTP client with authentication, retry with exponential backoff, response pagination, and configurable redirect handling.

The client is built for a caller that drives every knob from the outside, which is why it is not a wrapper over azdext.ResilientClient: that type fixes its options at construction and cannot carry per-request headers, a TLS opt out, a redirect policy, a response size limit, or an explicit auth scope.

Pagination is likewise its own implementation rather than azdext.Pager, because Pager reads only nextLink. This package also follows Microsoft Graph's @odata.nextLink and the RFC 5988 Link header, and it merges the sibling fields of the first page into the combined result.

Requests are retried on network errors and on 408, 429, 500, 502, 503, and 504. A Retry-After header, in either its second or millisecond form, overrides the computed backoff, and the backoff itself is jittered so that clients throttled by one service do not retry in lockstep.

Redirect targets are chosen by the server rather than the caller, so they are validated with azdext.SSRFSafeRedirect on top of the caller's own follow and hop-count policy.

Index

Constants

View Source
const (
	// DefaultMaxRedirects is the maximum number of HTTP redirects to follow.
	DefaultMaxRedirects = 10

	// DefaultMaxRetries is the default number of retry attempts for failed requests.
	DefaultMaxRetries = 3

	// DefaultMaxResponseSize is the maximum response body size in bytes (100 MB).
	DefaultMaxResponseSize = 100 * 1024 * 1024

	// MaxBodySizeForRetry is the maximum request body size that will be buffered
	// in memory for retry attempts (10 MB).
	MaxBodySizeForRetry = 10 * 1024 * 1024

	// DefaultMaxPaginationPages is the maximum number of pages to follow during pagination.
	DefaultMaxPaginationPages = 1000

	// BinaryDetectionBytes is the number of leading bytes inspected when detecting binary content.
	BinaryDetectionBytes = 512

	// MaxRetryAfterDuration caps how long a Retry-After header can delay a retry.
	// Without a cap, a misconfigured or hostile server could stall the client
	// indefinitely by returning 503 with Retry-After: 86400.
	MaxRetryAfterDuration = 120 * time.Second

	// MaxDrainBytes bounds how much of a retryable response body is read and
	// discarded before the next attempt. Draining lets the connection be reused;
	// the bound stops a large body from stalling the retry.
	MaxDrainBytes = 1 << 20 // 1 MiB

	// MaxRetryBackoff caps the computed exponential backoff delay.
	MaxRetryBackoff = 30 * time.Second
)
View Source
const DefaultMaxPages = 1000

DefaultMaxPages is the default maximum number of pages to fetch during pagination.

View Source
const DefaultMaxPaginationSize int64 = 1 * 1024 * 1024 * 1024

DefaultMaxPaginationSize is the default aggregate size limit for all paginated responses (1GB). This prevents unbounded memory growth when APIs return many pages.

Variables

View Source
var ErrPaginationPageLimitExceeded = fmt.Errorf("pagination page count limit exceeded")

ErrPaginationPageLimitExceeded is returned when the number of pages fetched exceeds the maximum page count.

View Source
var ErrPaginationSizeLimitExceeded = fmt.Errorf("pagination aggregate size limit exceeded")

ErrPaginationSizeLimitExceeded is returned when paginated responses exceed the aggregate size limit.

View Source
var UserAgent = ""

UserAgent is the default User-Agent header value. It can be overridden by the caller.

View Source
var Version = "0.0.0-dev"

Version is the version string used in the User-Agent header. It can be overridden at build time or by the caller.

Functions

func DetectContentType

func DetectContentType(body []byte, contentType string) bool

DetectContentType attempts to determine if content is binary

func IsJSON

func IsJSON(data []byte) bool

IsJSON checks if content appears to be JSON

func RedactSensitiveHeader

func RedactSensitiveHeader(key, value string) string

RedactSensitiveHeader redacts sensitive header values

func RedactToken

func RedactToken(token string) string

RedactToken redacts sensitive parts of an authorization token

func RedactURL added in v0.5.4

func RedactURL(rawURL string) string

RedactURL redacts sensitive query parameters from a URL string. It replaces values of known secret-bearing query keys with [REDACTED]. If the URL cannot be parsed, the original URL is returned unchanged.

func ShouldSkipAuth

func ShouldSkipAuth(url string, headers map[string]string, skipAuth bool) bool

ShouldSkipAuth determines if authentication should be skipped

Types

type Client

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

Client wraps HTTP client functionality

func NewClient

func NewClient(tokenProvider TokenProvider, insecure bool, timeout time.Duration) *Client

NewClient creates a new HTTP Client with retry, redirect, and TLS configuration.

func (*Client) Execute

func (c *Client) Execute(ctx context.Context, opts RequestOptions) (*Response, error)

Execute performs an HTTP request with the given options

type Formatter

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

Formatter handles response formatting and output

func NewFormatter

func NewFormatter(verbose bool, format string) *Formatter

NewFormatter creates a new formatter

func (*Formatter) Format

func (f *Formatter) Format(resp *Response) (string, error)

Format formats the response for output

func (*Formatter) WriteOutput

func (f *Formatter) WriteOutput(output string, outputFile string) error

WriteOutput writes the formatted output to the appropriate destination

func (*Formatter) WriteRawOutput

func (f *Formatter) WriteRawOutput(data []byte, outputFile string) error

WriteRawOutput writes raw bytes to a file or stdout

type MockTokenProvider

type MockTokenProvider struct {
	Token string
	Error error
}

MockTokenProvider is a mock implementation of TokenProvider for testing.

func (*MockTokenProvider) GetToken

func (m *MockTokenProvider) GetToken(ctx context.Context, scope string) (string, error)

GetToken returns the configured token or error.

type OutputFormat

type OutputFormat string

OutputFormat represents the output format type

const (
	FormatAuto OutputFormat = "auto"
	FormatJSON OutputFormat = "json"
	FormatRaw  OutputFormat = "raw"
)

Output format values.

type RequestOptions

type RequestOptions struct {
	Method          string
	URL             string
	Body            io.Reader
	Headers         map[string]string
	Scope           string
	SkipAuth        bool
	Verbose         bool
	Timeout         time.Duration
	Insecure        bool
	FollowRedirects bool
	MaxRedirects    int
	OutputFile      string
	Format          string
	TokenProvider   TokenProvider
	Binary          bool
	Retry           int
	MaxResponseSize int64
	Paginate        bool
	// MaxPaginationSize is the maximum aggregate size in bytes for all paginated
	// responses combined. Defaults to 1GB if unset or zero.
	MaxPaginationSize int64
	// MaxPages is the maximum number of pages to fetch during pagination.
	// Defaults to 1000 if unset or zero.
	MaxPages int
}

RequestOptions contains options for HTTP requests

type Response

type Response struct {
	StatusCode int
	Status     string
	Headers    http.Header
	Body       []byte
	Duration   time.Duration
}

Response contains HTTP response data

type TokenProvider

type TokenProvider interface {
	GetToken(ctx context.Context, scope string) (string, error)
}

TokenProvider supplies OAuth bearer tokens for a given scope.

Jump to

Keyboard shortcuts

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