Documentation
¶
Overview ¶
Package rest provides an HTTP client with middleware support, retry logic, and optional OpenTelemetry instrumentation.
Index ¶
- Variables
- func IsClientError(response *resty.Response) bool
- func IsNotFound(response *resty.Response) bool
- func IsServerError(response *resty.Response) bool
- func IsUnauthorized(response *resty.Response) bool
- type Client
- func (c *Client) AddMiddleware(middleware Middleware)
- func (c *Client) GetMiddlewares() []Middleware
- func (c *Client) GetRestClient() *resty.Client
- func (c *Client) GetRestConfig() *Config
- func (c *Client) HandleResponse(response *resty.Response) error
- func (c *Client) MakeRequest(ctx context.Context, method string, url string, body string, ...) (*resty.Response, error)
- func (c *Client) MakeRequestWithTrace(ctx context.Context, method string, url string, body string, ...) (*resty.Response, error)
- func (c *Client) SetMiddlewares(middlewares ...Middleware)
- type ClientOption
- type Config
- type ExecutionError
- type LoggingMiddleware
- type Middleware
- type NoOpMiddleware
- type OTelLoggingMiddleware
- type OTelMetricsMiddleware
- func (m *OTelMetricsMiddleware) AfterRequest(ctx context.Context, info RequestInfo)
- func (m *OTelMetricsMiddleware) BeforeRequest(ctx context.Context, method string, url string, body string, ...) context.Context
- func (m *OTelMetricsMiddleware) RecordRetry(ctx context.Context, method string, attempt int)
- type OTelTracingMiddleware
- type RequestInfo
- type ResourceNotFoundError
- type ResponseError
- type ServerError
- type UnauthorizedError
Constants ¶
This section is empty.
Variables ¶
var ( ErrResourceNotFound = errors.New("resource not found") ErrServer = errors.New("server error") ErrResponse = errors.New("response error") )
Sentinel errors for use with errors.Is.
Functions ¶
func IsClientError ¶
IsClientError returns true for any HTTP 4xx status code. Note: this overlaps with IsUnauthorized and IsNotFound; in HandleResponse, those are checked first so IsClientError only catches remaining 4xx codes.
func IsNotFound ¶
IsNotFound returns true for HTTP 404 (Not Found).
func IsServerError ¶
IsServerError returns true for HTTP 5xx status codes.
func IsUnauthorized ¶
IsUnauthorized returns true for HTTP 401 (Unauthorized) and 403 (Forbidden). Both indicate an access control failure; use response.StatusCode() to distinguish them.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps a resty HTTP client with middleware and OTel support.
func NewClient ¶
func NewClient(options ...ClientOption) *Client
NewClient creates a new REST client with the given options. For custom TLS configuration, use GetRestClient() to access the underlying resty client and call SetTLSClientConfig().
func (*Client) AddMiddleware ¶
func (c *Client) AddMiddleware(middleware Middleware)
AddMiddleware appends a middleware to the chain.
func (*Client) GetMiddlewares ¶
func (c *Client) GetMiddlewares() []Middleware
GetMiddlewares returns a copy of the current middleware chain.
func (*Client) GetRestClient ¶
GetRestClient returns the underlying resty client. Mutations to this client after NewClient returns are not thread-safe for concurrent use with doRequest.
func (*Client) GetRestConfig ¶
GetRestConfig returns a copy of the current REST configuration.
func (*Client) HandleResponse ¶
HandleResponse checks the HTTP status code and returns a typed error for non-success responses. Checks are ordered from most specific to least: 401/403 -> 404 -> 5xx -> other 4xx.
func (*Client) MakeRequest ¶
func (c *Client) MakeRequest(ctx context.Context, method string, url string, body string, headers map[string]string) (*resty.Response, error)
MakeRequest executes an HTTP request without resty trace.
The body parameter is a string; for binary payloads, use GetRestClient() and build the request directly with resty's SetBody(interface{}).
func (*Client) MakeRequestWithTrace ¶
func (c *Client) MakeRequestWithTrace(ctx context.Context, method string, url string, body string, headers map[string]string) (*resty.Response, error)
MakeRequestWithTrace executes an HTTP request with resty trace enabled.
The body parameter is a string; for binary payloads, use GetRestClient() and build the request directly with resty's SetBody(interface{}).
func (*Client) SetMiddlewares ¶
func (c *Client) SetMiddlewares(middlewares ...Middleware)
SetMiddlewares replaces the entire middleware chain.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption configures a Client during construction.
func WithMiddleware ¶
func WithMiddleware(middleware Middleware) ClientOption
WithMiddleware appends a single middleware to the existing middleware chain. The lock is not held here because option functions run only during NewClient construction.
func WithMiddlewares ¶
func WithMiddlewares(middlewares ...Middleware) ClientOption
WithMiddlewares replaces the entire middleware chain with the provided middlewares. Use WithMiddleware to append instead. The lock is not held here because option functions run only during NewClient construction.
func WithOTelConfig ¶
func WithOTelConfig(cfg *otel.Config) ClientOption
WithOTelConfig sets the OpenTelemetry configuration for the REST client. When set, adds OTel tracing, metrics, and logging middleware automatically.
func WithRestConfig ¶
func WithRestConfig(restConfig Config) ClientOption
WithRestConfig sets the REST client configuration.
type Config ¶
type Config struct {
RetryCount int `yaml:"retryCount" mapstructure:"retryCount"`
RetryWaitTime time.Duration `yaml:"retryWaitTime" mapstructure:"retryWaitTime"`
RetryMaxWaitTime time.Duration `yaml:"retryMaxWaitTime" mapstructure:"retryMaxWaitTime"`
Timeout time.Duration `yaml:"timeout" mapstructure:"timeout"`
// MaxResponseBodyLog limits the number of bytes of response body stored in logs/errors.
// 0 means unlimited. Default is 1024.
MaxResponseBodyLog int `yaml:"maxResponseBodyLog" mapstructure:"maxResponseBodyLog"`
// OpenTelemetry Configuration (optional - nil disables telemetry)
OTelConfig *otel.Config `yaml:"-" mapstructure:"-"` // Not serializable from config files
}
Config holds configuration for the REST client.
func DefaultRestConfig ¶
func DefaultRestConfig() *Config
DefaultRestConfig returns a default REST configuration with sensible defaults.
type ExecutionError ¶
ExecutionError represents an error during request execution (e.g. network failure).
func NewExecutionError ¶
func NewExecutionError(msg string, err error) *ExecutionError
func (*ExecutionError) Error ¶
func (e *ExecutionError) Error() string
func (*ExecutionError) Unwrap ¶
func (e *ExecutionError) Unwrap() error
type LoggingMiddleware ¶
type LoggingMiddleware struct{}
LoggingMiddleware logs HTTP requests and responses
func NewLoggingMiddleware ¶
func NewLoggingMiddleware() *LoggingMiddleware
NewLoggingMiddleware creates a new LoggingMiddleware instance
func (*LoggingMiddleware) AfterRequest ¶
func (m *LoggingMiddleware) AfterRequest(ctx context.Context, info RequestInfo)
AfterRequest logs the completion of the request with timing information
type Middleware ¶
type NoOpMiddleware ¶
type NoOpMiddleware struct{}
NoOpMiddleware is a middleware that does nothing - useful for testing and as a placeholder
func NewNoOpMiddleware ¶
func NewNoOpMiddleware() *NoOpMiddleware
NewNoOpMiddleware creates a new NoOpMiddleware instance
func (*NoOpMiddleware) AfterRequest ¶
func (m *NoOpMiddleware) AfterRequest(ctx context.Context, info RequestInfo)
AfterRequest does nothing
type OTelLoggingMiddleware ¶
type OTelLoggingMiddleware struct {
// contains filtered or unexported fields
}
OTelLoggingMiddleware implements structured logging with trace correlation for HTTP client
func NewOTelLoggingMiddleware ¶
func NewOTelLoggingMiddleware(cfg *pkgotel.Config) *OTelLoggingMiddleware
NewOTelLoggingMiddleware creates a new OpenTelemetry logging middleware
func (*OTelLoggingMiddleware) AfterRequest ¶
func (m *OTelLoggingMiddleware) AfterRequest(ctx context.Context, info RequestInfo)
AfterRequest logs the completion of the request with trace correlation
type OTelMetricsMiddleware ¶
type OTelMetricsMiddleware struct {
// contains filtered or unexported fields
}
OTelMetricsMiddleware implements metrics collection for HTTP client requests
func NewOTelMetricsMiddleware ¶
func NewOTelMetricsMiddleware(cfg *pkgotel.Config) *OTelMetricsMiddleware
NewOTelMetricsMiddleware creates a new OpenTelemetry metrics middleware
func (*OTelMetricsMiddleware) AfterRequest ¶
func (m *OTelMetricsMiddleware) AfterRequest(ctx context.Context, info RequestInfo)
AfterRequest records response metrics
func (*OTelMetricsMiddleware) BeforeRequest ¶
func (m *OTelMetricsMiddleware) BeforeRequest(ctx context.Context, method string, url string, body string, headers map[string]string) context.Context
BeforeRequest records request size metrics
func (*OTelMetricsMiddleware) RecordRetry ¶
func (m *OTelMetricsMiddleware) RecordRetry(ctx context.Context, method string, attempt int)
RecordRetry records a retry attempt (to be called by retry logic)
type OTelTracingMiddleware ¶
type OTelTracingMiddleware struct {
// contains filtered or unexported fields
}
OTelTracingMiddleware implements distributed tracing for HTTP client requests
func NewOTelTracingMiddleware ¶
func NewOTelTracingMiddleware(cfg *pkgotel.Config) *OTelTracingMiddleware
NewOTelTracingMiddleware creates a new OpenTelemetry tracing middleware
func (*OTelTracingMiddleware) AfterRequest ¶
func (m *OTelTracingMiddleware) AfterRequest(ctx context.Context, info RequestInfo)
AfterRequest ends the span and records the response status
type RequestInfo ¶
type ResourceNotFoundError ¶
ResourceNotFoundError represents a 404 Not Found response.
func NewResourceNotFoundError ¶
func NewResourceNotFoundError(statusCode int, msg string, respBody string) *ResourceNotFoundError
func (*ResourceNotFoundError) Error ¶
func (e *ResourceNotFoundError) Error() string
func (*ResourceNotFoundError) Unwrap ¶ added in v2.7.13
func (e *ResourceNotFoundError) Unwrap() error
type ResponseError ¶
ResponseError represents a client-side HTTP error (HTTP 4xx, excluding 401/403/404).
func NewResponseError ¶
func NewResponseError(statusCode int, msg string, respBody string) *ResponseError
func (*ResponseError) Error ¶
func (e *ResponseError) Error() string
func (*ResponseError) Unwrap ¶ added in v2.7.13
func (e *ResponseError) Unwrap() error
type ServerError ¶
ServerError represents a server-side failure (HTTP 5xx).
func NewServerError ¶
func NewServerError(statusCode int, msg string, respBody string) *ServerError
NewServerError creates a new ServerError
func (*ServerError) Error ¶
func (e *ServerError) Error() string
func (*ServerError) Unwrap ¶ added in v2.7.13
func (e *ServerError) Unwrap() error
type UnauthorizedError ¶
type UnauthorizedError struct {
}
UnauthorizedError represents an authentication or authorization failure (HTTP 401/403).
func NewUnauthorizedError ¶
func NewUnauthorizedError(statusCode int, msg string, respBody string) *UnauthorizedError
NewUnauthorizedError creates a new UnauthorizedError
func (*UnauthorizedError) Error ¶
func (e *UnauthorizedError) Error() string
func (*UnauthorizedError) Unwrap ¶ added in v2.7.13
func (e *UnauthorizedError) Unwrap() error