Documentation
¶
Index ¶
- func NewClient(timeout time.Duration, opts ...ClientOption) *http.Client
- type ClientOption
- type LogResponse
- type Option
- func WithClient(client *http.Client) Option
- func WithHeader(name, value string) Option
- func WithHeaders(headers map[string]string) Option
- func WithLogResponse(mode LogResponse) Option
- func WithLogger(logger *slog.Logger) Option
- func WithMethod(method string) Option
- func WithName(name string) Option
- func WithResponseBodyLimit(limit int) Option
- func WithSubjectTemplate(tmpl *templates.StringTemplate) Option
- func WithTemplate(tmpl *templates.Template) Option
- func WithURL(url string) Option
- func WithValidateJSON() Option
- type Target
- func (t *Target) Render(payload notify.Payload) ([]byte, error)
- func (t *Target) Send(ctx context.Context, payload notify.Payload) (notify.DeliveryResult, error)
- func (t *Target) SendResult(ctx context.Context, payload notify.Payload) (notify.DeliveryResult, error)
- func (t *Target) Type() string
- func (t *Target) Validate(payload notify.Payload) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewClient ¶
func NewClient(timeout time.Duration, opts ...ClientOption) *http.Client
NewClient constructs an HTTP client for webhook delivery.
Timeout values less than or equal to zero default to 10 seconds. The returned client does not use proxy environment variables unless WithProxyFromEnvironment is provided. Use WithSkipTLSVerify to disable TLS certificate verification.
Types ¶
type ClientOption ¶
type ClientOption func(*clientOptions)
ClientOption configures the HTTP client created by NewClient.
func WithProxyFromEnvironment ¶
func WithProxyFromEnvironment() ClientOption
WithProxyFromEnvironment makes a NewClient-created client honor proxy environment variables.
func WithSkipTLSVerify ¶
func WithSkipTLSVerify() ClientOption
WithSkipTLSVerify disables TLS certificate verification for a NewClient-created client.
This should only be used for local development or trusted private endpoints with self-signed certificates.
type LogResponse ¶
type LogResponse string
LogResponse controls how much of a successful webhook response is logged.
const ( // LogResponseSummary logs only status, status code, duration, and truncation state. LogResponseSummary LogResponse = "summary" // LogResponseBody logs status fields and response body, but not response headers. LogResponseBody LogResponse = "body" // LogResponseFull logs status fields, response body, and response headers. LogResponseFull LogResponse = "full" // LogResponseNone suppresses successful webhook response logs. LogResponseNone LogResponse = "none" )
type Option ¶
type Option func(*Target)
Option configures a webhook target.
func WithClient ¶
WithClient configures the HTTP client used to send webhook requests.
func WithHeader ¶
WithHeader configures one additional HTTP request header.
func WithHeaders ¶
WithHeaders configures additional HTTP request headers.
func WithLogResponse ¶
func WithLogResponse(mode LogResponse) Option
WithLogResponse configures how much successful response information is logged.
func WithLogger ¶
WithLogger configures the target-specific logger.
func WithMethod ¶
WithMethod configures the HTTP method used for webhook requests.
func WithResponseBodyLimit ¶
WithResponseBodyLimit configures how many response-body bytes are read and logged.
func WithSubjectTemplate ¶
func WithSubjectTemplate(tmpl *templates.StringTemplate) Option
WithSubjectTemplate configures the notification subject template.
func WithTemplate ¶
WithTemplate configures the HTTP request body template.
func WithValidateJSON ¶
func WithValidateJSON() Option
WithValidateJSON enables validation of the rendered body as JSON.
type Target ¶
type Target struct {
// Name is an optional human-readable target name used in logs.
//
// When Name is empty, logs and delivery errors fall back to URL.
Name string
// URL is the webhook endpoint URL.
URL string
// Method is the HTTP method used for webhook requests.
//
// New defaults Method to POST when unset.
Method string
// Headers contains additional HTTP request headers.
//
// Headers are set after the default Content-Type header, so callers may
// override Content-Type when needed.
Headers map[string]string
// Template renders the HTTP request body.
Template *templates.Template
// SubjectTmpl renders the notification subject.
//
// The rendered subject is passed back into the body template data so body
// templates can use it as .Subject.
SubjectTmpl *templates.StringTemplate
// Client sends HTTP webhook requests.
//
// New defaults Client to NewClient with a 10 second timeout when unset.
// Provide a custom client for custom transports, proxies, tracing, mTLS,
// test servers, or different timeout behavior.
Client *http.Client
// Logger receives webhook-specific request, response, and error logs.
//
// New defaults Logger to a discard logger when unset.
Logger *slog.Logger
// ValidateJSON requires the rendered request body to be valid JSON.
//
// This is useful for JSON webhook integrations such as Slack, Discord, or
// custom HTTP APIs. When enabled, Render returns an error before sending if
// the template output is not valid JSON.
ValidateJSON bool
// LogResponse controls how much successful response information is logged.
//
// New defaults LogResponse to LogResponseSummary when unset. Error responses
// are logged with response body details regardless of this setting.
LogResponse LogResponse
// ResponseBodyLimit limits how many response-body bytes are read and logged.
//
// New defaults ResponseBodyLimit to 4096 when unset. Values less than or
// equal to zero are treated as the default by response-body reading.
ResponseBodyLimit int
}
Target delivers notifications to a webhook endpoint.
func New ¶
New constructs a webhook target from options.
It applies defaults for optional fields:
- Method defaults to POST.
- Client defaults to NewClient with a 10 second timeout and no proxy.
- Logger defaults to a discard logger.
- LogResponse defaults to LogResponseSummary.
- ResponseBodyLimit defaults to 4096 bytes.
Template and SubjectTmpl are not validated by New. They are rendered by Render, Validate, Send, or SendResult, which return errors for incomplete configuration.
The returned target is safe to pass to notify.Receiver.Targets.
func NewFromTarget ¶
NewFromTarget constructs a webhook target from an existing Target value.
Additional options are applied after the initial target value, then defaults are filled in the same way as New.
func (*Target) SendResult ¶
func (t *Target) SendResult(ctx context.Context, payload notify.Payload) (notify.DeliveryResult, error)
SendResult renders and posts a webhook notification with response details.