Documentation
¶
Index ¶
- Variables
- func DecodeJSON[T any](resp *http.Response) (T, error)
- type Client
- func (c *Client) Close()
- func (c *Client) Delete(ctx context.Context, url string, opts ...RequestOption) (*http.Response, error)
- func (c *Client) Do(ctx context.Context, req *http.Request, reqOpts ...RequestOption) (*http.Response, error)
- func (c *Client) Get(ctx context.Context, url string, opts ...RequestOption) (*http.Response, error)
- func (c *Client) Head(ctx context.Context, url string, opts ...RequestOption) (*http.Response, error)
- func (c *Client) Options(ctx context.Context, url string, opts ...RequestOption) (*http.Response, error)
- func (c *Client) Patch(ctx context.Context, url string, contentType string, body io.Reader, ...) (*http.Response, error)
- func (c *Client) Post(ctx context.Context, url string, contentType string, body io.Reader, ...) (*http.Response, error)
- func (c *Client) Put(ctx context.Context, url string, contentType string, body io.Reader, ...) (*http.Response, error)
- type Config
- type ConfigError
- type Hook
- type Logger
- type Option
- func WithHeaders(h map[string]string) Option
- func WithHooks(hooks ...Hook) Option
- func WithLogger(l Logger) Option
- func WithRetry(maxRetries int, minBackoff, maxBackoff time.Duration) Option
- func WithTelemetry(t *TelemetryConfig) Option
- func WithTimeout(d time.Duration) Option
- func WithTransport(t *http.Transport) Option
- type RequestError
- type RequestOption
- type ResponseDecodeError
- type RetryConfig
- type TelemetryConfig
Constants ¶
This section is empty.
Variables ¶
var ErrClientClosed = errors.New("http client is closed")
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
type Config ¶
type Config struct {
Timeout time.Duration
Headers map[string]string
Logger Logger
Retry *RetryConfig
Transport *http.Transport
Hooks []Hook
Telemetry *TelemetryConfig
}
func DefaultConfig ¶
func DefaultConfig() *Config
type ConfigError ¶
ConfigError represents a validation error in the client configuration.
func NewConfigError ¶
func NewConfigError(field, message string) *ConfigError
func (*ConfigError) Error ¶
func (e *ConfigError) Error() string
type Hook ¶
type Hook interface {
// OnRequestStart is called before the request is sent.
OnRequestStart(req *http.Request)
// OnRequestEnd is called after a successful response is received.
OnRequestEnd(req *http.Request, resp *http.Response)
// OnError is called when the request fails with an error.
OnError(req *http.Request, err error)
}
Hook defines lifecycle callbacks for HTTP requests. All methods are optional — implement only the ones you need.
type Logger ¶
type Logger interface {
// Info logs an informational message with optional fields
Info(ctx context.Context, msg string, fields map[string]any)
// Error logs an error message with optional fields
Error(ctx context.Context, msg string, fields map[string]any)
// Warn logs a warning message with optional fields
Warn(ctx context.Context, msg string, fields map[string]any)
// Debug logs a debug message with optional fields
Debug(ctx context.Context, msg string, fields map[string]any)
// Close closes the logger and flushes any pending logs.
// Note: This should be called by the logger creator (usually in main()),
// not by the platform. Use defer logger.Close() after creating the logger.
// Returns an error if the logger fails to close or flush properly.
Close() error
}
Logger is the interface that any logger implementation must satisfy. This allows the client to be agnostic about the logging implementation.
The client does NOT call Close(). The caller who creates the logger is responsible for its lifecycle.
Example adapter for loki-logger-go:
// loki.Logger already satisfies this interface, pass it directly:
httpClient, _ := httpclient.New(
httpclient.DefaultConfig(),
httpclient.WithLogger(lokiLogger),
)
type Option ¶
type Option func(*Config)
func WithHeaders ¶
func WithLogger ¶
func WithTelemetry ¶
func WithTelemetry(t *TelemetryConfig) Option
WithTelemetry enables OpenTelemetry distributed tracing. The client initializes OTLP export and creates spans for every HTTP request. Pass nil to disable telemetry.
func WithTimeout ¶
func WithTransport ¶
WithTransport sets a custom HTTP transport for connection pooling and TLS configuration.
type RequestError ¶
RequestError represents a failure executing an HTTP request.
func (*RequestError) Error ¶
func (e *RequestError) Error() string
func (*RequestError) Unwrap ¶
func (e *RequestError) Unwrap() error
type RequestOption ¶
type RequestOption func(*requestOpts)
RequestOption configures per-request behavior, overriding client-level defaults.
func WithObfuscatedHeaders ¶
func WithObfuscatedHeaders(headers ...string) RequestOption
WithObfuscatedHeaders specifies header names whose values should be masked in logs. The headers are sent with their original values but logged as "********".
func WithRequestHeaders ¶
func WithRequestHeaders(headers http.Header) RequestOption
WithRequestHeaders adds extra headers only for this specific request.
func WithSkipLog ¶
func WithSkipLog() RequestOption
WithSkipLog disables logging for this specific request.
func WithTags ¶
func WithTags(tags map[string]string) RequestOption
WithTags adds custom key-value tags to the log entries for this request.
func WithTraceID ¶
func WithTraceID(traceID string) RequestOption
WithTraceID adds an X-Trace-Id header to the request for distributed tracing.
type ResponseDecodeError ¶
ResponseDecodeError represents a failure decoding an HTTP response body.
func (*ResponseDecodeError) Error ¶
func (e *ResponseDecodeError) Error() string
func (*ResponseDecodeError) Unwrap ¶
func (e *ResponseDecodeError) Unwrap() error
type RetryConfig ¶
type TelemetryConfig ¶
type TelemetryConfig struct {
ServiceName string
Version string
Environment string
OTLPEndpoint string
SampleAll bool
}
TelemetryConfig configures OpenTelemetry distributed tracing. When provided, the client automatically creates spans for every HTTP request and exports them via OTLP to the configured endpoint (Datadog Agent, Jaeger, etc).