webhook

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package webhook provides HTTP webhook client and types for external integrations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client sends webhooks to external endpoints with retry logic.

func NewClient

func NewClient(cfg Config) *Client

NewClient creates a new webhook client with the given configuration.

func (*Client) Send

func (c *Client) Send(ctx context.Context, webhook *Webhook) (*DeliveryResult, error)

Send delivers a webhook to its target URL with retry logic.

func (*Client) SendBatch

func (c *Client) SendBatch(ctx context.Context, webhooks []*Webhook) ([]DeliveryResult, error)

SendBatch delivers multiple webhooks concurrently and returns all results.

func (*Client) ValidateEndpoint

func (c *Client) ValidateEndpoint(ctx context.Context, url string) error

ValidateEndpoint checks if a webhook endpoint is reachable.

type Config

type Config struct {
	Timeout       time.Duration
	MaxRetries    int
	RetryBackoff  time.Duration
	MaxConcurrent int
}

Config holds configuration for the webhook client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default configuration.

type DeliveryResult

type DeliveryResult struct {
	WebhookID    string        `json:"webhook_id"`
	StatusCode   int           `json:"status_code"`
	ResponseBody string        `json:"response_body,omitempty"`
	Duration     time.Duration `json:"duration_ms"`
	Attempts     int           `json:"attempts"`
	Success      bool          `json:"success"`
	Error        string        `json:"error,omitempty"`
	DeliveredAt  time.Time     `json:"delivered_at"`
}

DeliveryResult represents the outcome of a webhook delivery attempt.

type RetryPolicy

type RetryPolicy struct {
	MaxAttempts    int           `json:"max_attempts"`
	InitialBackoff time.Duration `json:"initial_backoff"`
	MaxBackoff     time.Duration `json:"max_backoff"`
	Multiplier     float64       `json:"multiplier"`
}

RetryPolicy defines the retry behavior for failed webhook deliveries.

func DefaultRetryPolicy

func DefaultRetryPolicy() *RetryPolicy

DefaultRetryPolicy returns a sensible default retry policy.

func (*RetryPolicy) CalculateBackoff

func (p *RetryPolicy) CalculateBackoff(attempt int) time.Duration

CalculateBackoff returns the backoff duration for the given attempt number.

type Webhook

type Webhook struct {
	ID          string            `json:"id"`
	URL         string            `json:"url"`
	EventType   string            `json:"event_type"`
	EventID     string            `json:"event_id"`
	Payload     json.RawMessage   `json:"payload"`
	Headers     map[string]string `json:"headers,omitempty"`
	Secret      string            `json:"-"` // For HMAC signing, not serialized
	Timeout     time.Duration     `json:"-"`
	RetryPolicy *RetryPolicy      `json:"-"`
}

Webhook represents a webhook request to be sent to an external endpoint.

Jump to

Keyboard shortcuts

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