Documentation
¶
Index ¶
- Constants
- func EnsureTraceID(ctx context.Context) string
- func GenerateTraceParent() string
- func IsErrorType(err error, errorType ErrorType) bool
- func IsHTTPStatusError(err error, statusCode int) bool
- func IsSuccessStatus(statusCode int) bool
- func TraceIDFromContext(ctx context.Context) (string, bool)
- func TraceParentFromContext(ctx context.Context) (string, bool)
- func TraceStateFromContext(ctx context.Context) (string, bool)
- func WithTraceID(ctx context.Context, traceID string) context.Context
- func WithTraceParent(ctx context.Context, traceParent string) context.Context
- func WithTraceState(ctx context.Context, traceState string) context.Context
- type BasicAuth
- type Builder
- func (b *Builder) Build() Client
- func (b *Builder) WithBasicAuth(username, password string) *Builder
- func (b *Builder) WithDefaultHeader(key, value string) *Builder
- func (b *Builder) WithHTTPClient(client *nethttp.Client) *Builder
- func (b *Builder) WithRequestInterceptor(interceptor RequestInterceptor) *Builder
- func (b *Builder) WithResponseInterceptor(interceptor ResponseInterceptor) *Builder
- func (b *Builder) WithRetries(maxRetries int, retryDelay time.Duration) *Builder
- func (b *Builder) WithTimeout(timeout time.Duration) *Builder
- func (b *Builder) WithTraceIDExtractor(extractor func(_ context.Context) (string, bool)) *Builder
- func (b *Builder) WithTraceIDGenerator(gen func() string) *Builder
- func (b *Builder) WithTraceIDHeader(name string) *Builder
- func (b *Builder) WithTransport(transport nethttp.RoundTripper) *Builder
- func (b *Builder) WithW3CTrace(enabled bool) *Builder
- type Client
- type ClientError
- func NewHTTPError(message string, statusCode int, body []byte) ClientError
- func NewInterceptorError(message, stage string, wrapped error) ClientError
- func NewNetworkError(message string, wrapped error) ClientError
- func NewTimeoutError(message string, timeout time.Duration) ClientError
- func NewValidationError(message, field string) ClientError
- type Config
- type ErrorType
- type Request
- type RequestInterceptor
- type Response
- type ResponseInterceptor
- type Stats
Constants ¶
const ( // DefaultTimeout is the default request timeout duration DefaultTimeout = 30 * time.Second // DefaultMaxRetries is the default maximum number of retries for failed requests DefaultMaxRetries = 0 // DefaultRetryDelay is the default delay between retries DefaultRetryDelay = 1 * time.Second )
const ( // HeaderXRequestID is the standard header name for request tracing HeaderXRequestID = gobrickstrace.HeaderXRequestID // HeaderTraceParent is the W3C trace context header name HeaderTraceParent = gobrickstrace.HeaderTraceParent // HeaderTraceState is the W3C trace context "tracestate" header name HeaderTraceState = gobrickstrace.HeaderTraceState )
Variables ¶
This section is empty.
Functions ¶
func EnsureTraceID ¶
EnsureTraceID returns an existing trace ID from context or generates a new one
func GenerateTraceParent ¶
func GenerateTraceParent() string
GenerateTraceParent creates a minimal W3C traceparent header value. Format: version(2)-trace-id(32)-span-id(16)-flags(2), e.g., "00-<32>-<16>-01"
func IsErrorType ¶
IsErrorType checks if an error is of a specific type
func IsHTTPStatusError ¶
IsHTTPStatusError checks if an error is an HTTP error with a specific status code
func IsSuccessStatus ¶
IsSuccessStatus checks if a status code represents success (2xx)
func TraceIDFromContext ¶
TraceIDFromContext returns a trace ID from context if present
func TraceParentFromContext ¶
TraceParentFromContext returns a traceparent from context if present
func TraceStateFromContext ¶
TraceStateFromContext returns a tracestate from context if present
func WithTraceID ¶
WithTraceID adds a trace ID to the context for HTTP client propagation
func WithTraceParent ¶
WithTraceParent adds a W3C traceparent value to the context
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a fluent interface for configuring the REST client
func NewBuilder ¶
NewBuilder creates a new client builder
func (*Builder) WithBasicAuth ¶
WithBasicAuth sets basic authentication credentials
func (*Builder) WithDefaultHeader ¶
WithDefaultHeader adds a default header that will be sent with all requests
func (*Builder) WithHTTPClient ¶
WithHTTPClient allows providing a custom *http.Client instance. If the provided client's Timeout is zero, the builder applies its configured Timeout; otherwise it is used as-is. The client's Transport is preserved unless explicitly overridden via WithTransport.
func (*Builder) WithRequestInterceptor ¶
func (b *Builder) WithRequestInterceptor(interceptor RequestInterceptor) *Builder
WithRequestInterceptor adds a request interceptor
func (*Builder) WithResponseInterceptor ¶
func (b *Builder) WithResponseInterceptor(interceptor ResponseInterceptor) *Builder
WithResponseInterceptor adds a response interceptor
func (*Builder) WithRetries ¶
WithRetries sets the retry configuration
func (*Builder) WithTimeout ¶
WithTimeout sets the request timeout
func (*Builder) WithTraceIDExtractor ¶
WithTraceIDExtractor sets a function to extract a trace ID from context
func (*Builder) WithTraceIDGenerator ¶
WithTraceIDGenerator sets the generator used when no trace ID is present
func (*Builder) WithTraceIDHeader ¶
WithTraceIDHeader sets the header name used for the trace ID (default: X-Request-ID)
func (*Builder) WithTransport ¶
func (b *Builder) WithTransport(transport nethttp.RoundTripper) *Builder
WithTransport sets a custom RoundTripper while still letting the builder manage other client settings.
func (*Builder) WithW3CTrace ¶
WithW3CTrace enables or disables W3C trace context propagation
type Client ¶
type Client interface {
Get(ctx context.Context, req *Request) (*Response, error)
Post(ctx context.Context, req *Request) (*Response, error)
Put(ctx context.Context, req *Request) (*Response, error)
Patch(ctx context.Context, req *Request) (*Response, error)
Delete(ctx context.Context, req *Request) (*Response, error)
Do(ctx context.Context, method string, req *Request) (*Response, error)
}
Client defines the REST client interface for making HTTP requests
type ClientError ¶
ClientError represents different types of REST client errors
func NewHTTPError ¶
func NewHTTPError(message string, statusCode int, body []byte) ClientError
NewHTTPError creates a new HTTP error
func NewInterceptorError ¶
func NewInterceptorError(message, stage string, wrapped error) ClientError
NewInterceptorError creates a new interceptor error
func NewNetworkError ¶
func NewNetworkError(message string, wrapped error) ClientError
NewNetworkError creates a new network error
func NewTimeoutError ¶
func NewTimeoutError(message string, timeout time.Duration) ClientError
NewTimeoutError creates a new timeout error
func NewValidationError ¶
func NewValidationError(message, field string) ClientError
NewValidationError creates a new validation error
type Config ¶
type Config struct {
Timeout time.Duration
MaxRetries int
RetryDelay time.Duration
RequestInterceptors []RequestInterceptor
ResponseInterceptors []ResponseInterceptor
BasicAuth *BasicAuth
DefaultHeaders map[string]string
// LogPayloads enables debug-level logging of headers and body payloads
LogPayloads bool
// MaxPayloadLogBytes caps the number of body bytes logged when LogPayloads is enabled
MaxPayloadLogBytes int
// TraceIDHeader configures the header name used for trace ID propagation (default: X-Request-ID)
TraceIDHeader string
// NewTraceID generates a new trace ID when none is present (default: uuid)
NewTraceID func() string
// TraceIDExtractor allows advanced extraction of a trace ID from context; return ok=false to fallback to generator
TraceIDExtractor func(_ context.Context) (traceID string, ok bool)
// EnableW3CTrace enables W3C Trace Context (traceparent/tracestate) propagation and generation
EnableW3CTrace bool
}
Config holds the REST client configuration
type RequestInterceptor ¶
RequestInterceptor is called before sending the request
func NewTraceIDInterceptor ¶
func NewTraceIDInterceptor() RequestInterceptor
NewTraceIDInterceptor creates a request interceptor that adds trace ID headers This provides an alternative approach for users who want explicit control
func NewTraceIDInterceptorFor ¶
func NewTraceIDInterceptorFor(header string) RequestInterceptor
NewTraceIDInterceptorFor creates an interceptor that uses a custom header name