Documentation
¶
Index ¶
- Constants
- Variables
- type LoggerDecorator
- type LoggerOption
- type NetworkError
- type Request
- type RequestOption
- type Response
- type RetryDecorator
- type RetryOption
- func WithRetryBackoff() RetryOption
- func WithRetryCount(max int) RetryOption
- func WithRetryDelay(interval time.Duration) RetryOption
- func WithRetryLogger(logger *slog.Logger) RetryOption
- func WithRetryOnNetworkErrors() RetryOption
- func WithRetryOnServerErrors() RetryOption
- func WithRetryOnStatus(statusCodes ...int) RetryOption
- type SenderOption
- func WithDefaultHeaders(headers map[string]string) SenderOption
- func WithDefaultMethod(method string) SenderOption
- func WithDefaultTimeout(timeout time.Duration) SenderOption
- func WithHTTPClient(client *http.Client) SenderOption
- func WithMaxRetries(retries int) SenderOption
- func WithRetryInterval(interval time.Duration) SenderOption
- type WebhookSender
Constants ¶
const DefaultMaxRetries = 3
DefaultMaxRetries is the default number of retry attempts
const DefaultRetryInterval = 500 * time.Millisecond
DefaultRetryInterval is the default interval between retry attempts
Variables ¶
var ( // ErrInvalidURL is returned when the webhook URL is invalid or empty ErrInvalidURL = errors.New("invalid webhook URL") // ErrInvalidMethod is returned when the HTTP method is invalid ErrInvalidMethod = errors.New("invalid HTTP method") // ErrMarshalParams is returned when there's an error marshaling request parameters ErrMarshalParams = errors.New("failed to marshal request parameters") // ErrCreateRequest is returned when there's an error creating the HTTP request ErrCreateRequest = errors.New("failed to create HTTP request") // ErrSendRequest is returned when there's an error sending the HTTP request ErrSendRequest = errors.New("failed to send HTTP request") // ErrReadResponse is returned when there's an error reading the HTTP response ErrReadResponse = errors.New("failed to read HTTP response") // ErrResponseTimeout is returned when the webhook request times out ErrResponseTimeout = errors.New("webhook request timed out") )
Error definitions for the webhook package
Functions ¶
This section is empty.
Types ¶
type LoggerDecorator ¶
type LoggerDecorator struct {
// contains filtered or unexported fields
}
LoggerDecorator wraps a WebhookSender and adds logging functionality
func (*LoggerDecorator) Send ¶
func (l *LoggerDecorator) Send(ctx context.Context, url string, params any, opts ...RequestOption) (*Response, error)
Send sends a webhook with logging before and after the request
type LoggerOption ¶
type LoggerOption func(*LoggerDecorator)
LoggerOption configures the logger decorator
func WithHideParams ¶
func WithHideParams() LoggerOption
WithHideParams prevents request parameters from being logged This is useful for security/privacy when parameters contain sensitive information
func WithMaskedFields ¶
func WithMaskedFields(fields ...string) LoggerOption
WithMaskedFields specifies field names whose values should be masked with asterisks This is useful for logging that fields exist while hiding their sensitive values
type NetworkError ¶
NetworkError represents a network-related error when sending a webhook
func (*NetworkError) Error ¶
func (e *NetworkError) Error() string
func (*NetworkError) Unwrap ¶
func (e *NetworkError) Unwrap() error
type Request ¶
type Request struct {
URL string
Method string
Headers map[string]string
Params any
Timeout time.Duration
}
Request represents a webhook request
type RequestOption ¶
type RequestOption func(*requestOptions)
RequestOption configures a single webhook request
func WithHeader ¶
func WithHeader(key, value string) RequestOption
WithHeader adds a header to the request
func WithHeaders ¶
func WithHeaders(headers map[string]string) RequestOption
WithHeaders sets multiple headers for the request
func WithMethod ¶
func WithMethod(method string) RequestOption
WithMethod sets the HTTP method for a request
func WithRequestTimeout ¶
func WithRequestTimeout(timeout time.Duration) RequestOption
WithRequestTimeout sets a timeout for this specific request
type Response ¶
type Response struct {
StatusCode int
Body []byte
Headers http.Header
Duration time.Duration
Request *Request // Reference to the original request
}
Response represents a webhook response
func (*Response) IsSuccessful ¶
IsSuccessful returns true if the response status code is in the 2xx range
type RetryDecorator ¶
type RetryDecorator struct {
// contains filtered or unexported fields
}
RetryDecorator wraps a WebhookSender and adds retry functionality
func (*RetryDecorator) Send ¶
func (r *RetryDecorator) Send(ctx context.Context, url string, params any, opts ...RequestOption) (*Response, error)
Send sends a webhook with retry logic based on configuration
type RetryOption ¶
type RetryOption func(*RetryDecorator)
RetryOption configures the retry decorator
func WithRetryBackoff ¶
func WithRetryBackoff() RetryOption
WithRetryBackoff enables exponential backoff for retry intervals The interval will double after each retry attempt
func WithRetryCount ¶
func WithRetryCount(max int) RetryOption
WithRetryCount sets the maximum number of retry attempts
func WithRetryDelay ¶
func WithRetryDelay(interval time.Duration) RetryOption
WithRetryDelay sets the interval between retry attempts
func WithRetryLogger ¶
func WithRetryLogger(logger *slog.Logger) RetryOption
WithRetryLogger sets a logger for retry operations
func WithRetryOnNetworkErrors ¶
func WithRetryOnNetworkErrors() RetryOption
WithRetryOnNetworkErrors configures the decorator to retry on network-related errors
func WithRetryOnServerErrors ¶
func WithRetryOnServerErrors() RetryOption
WithRetryOnServerErrors configures the decorator to retry on all 5xx server errors
func WithRetryOnStatus ¶
func WithRetryOnStatus(statusCodes ...int) RetryOption
WithRetryOnStatus adds specific HTTP status codes that should trigger a retry
type SenderOption ¶
type SenderOption func(*webhookSender)
SenderOption configures the webhook sender
func WithDefaultHeaders ¶
func WithDefaultHeaders(headers map[string]string) SenderOption
WithDefaultHeaders sets default headers for all requests
func WithDefaultMethod ¶
func WithDefaultMethod(method string) SenderOption
WithDefaultMethod sets the default HTTP method for all requests
func WithDefaultTimeout ¶
func WithDefaultTimeout(timeout time.Duration) SenderOption
WithDefaultTimeout sets the default timeout for all requests
func WithHTTPClient ¶
func WithHTTPClient(client *http.Client) SenderOption
WithHTTPClient sets a custom HTTP client
func WithMaxRetries ¶
func WithMaxRetries(retries int) SenderOption
WithMaxRetries sets the maximum number of retries for failed requests
func WithRetryInterval ¶
func WithRetryInterval(interval time.Duration) SenderOption
WithRetryInterval sets the interval between retries
type WebhookSender ¶
type WebhookSender interface {
// Send webhooks with minimal required parameters and optional request options
Send(ctx context.Context, url string, params any, opts ...RequestOption) (*Response, error)
}
WebhookSender is the interface for sending webhooks
func NewLoggerDecorator ¶
func NewLoggerDecorator(sender WebhookSender, logger *slog.Logger, opts ...LoggerOption) WebhookSender
NewLoggerDecorator creates a new webhook sender with logging capabilities
func NewRetryDecorator ¶
func NewRetryDecorator(sender WebhookSender, opts ...RetryOption) WebhookSender
NewRetryDecorator creates a new webhook sender with retry capabilities
func NewWebhookSender ¶
func NewWebhookSender(opts ...SenderOption) WebhookSender
NewWebhookSender creates a new webhook sender