Documentation ¶
Overview ¶
Package handler implements types for webhook consumption.
The Dispatcher type is a simple http.Handler for receiving webhooks. Care should be taken to not block in handler events (lengthy operations should use goroutines or channels, see examples).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Dispatcher ¶
type Dispatcher struct { Key APIKey Logger *log.Logger Bounce func(*types.Bounce) error Click func(*types.Click) error Complaint func(*types.Complaint) error Delivered func(*types.Delivered) error Drop func(*types.Drop) error Open func(*types.Open) error Unsubscribe func(*types.Unsubscribe) error }
Dispatcher is a utility type to dispatch webhook events to handler functions. If Logger is not nil errors will be logged to it when a Dispatcher is used as an http.Handler. If Key is not nil, the signature of webhooks will be validated and invalid payloads will not be dispatched.
Example ¶
unsub := func(u *types.Unsubscribe) error { log.Println("Unsubscribed:", u.Recipient) return nil } d := &Dispatcher{ Unsubscribe: unsub, } http.ListenAndServe(`127.0.0.1:0`, d)
Output:
Example (Channels) ¶
uchan := make(chan *types.Unsubscribe, 1024) go unsubscribes(uchan) unsub := func(u *types.Unsubscribe) error { select { case uchan <- u: default: return fmt.Errorf("rate limited") } return nil } d := &Dispatcher{ Unsubscribe: unsub, } http.ListenAndServe(`127.0.0.1:0`, d)
Output:
func (*Dispatcher) Dispatch ¶
func (d *Dispatcher) Dispatch(ev types.Type) error
Dispatch dispatches ev.
func (*Dispatcher) HTTPRequest ¶
func (d *Dispatcher) HTTPRequest(r *http.Request) error
HTTPRequest dispatches an http.Request.
func (*Dispatcher) Reader ¶
func (d *Dispatcher) Reader(r io.Reader) error
Reader dispatches the content of r.
func (*Dispatcher) ServeHTTP ¶
func (d *Dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler. If a handler function returns an error, the error will be returned to the caller with status code 429.