handler

package
v0.0.0-...-8c17a97 Latest Latest
Warning

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

Go to latest
Published: May 28, 2018 License: MIT Imports: 6 Imported by: 0

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

func ParseRequest

func ParseRequest(r *http.Request) (types.Type, error)

ParseRequest decodes a webhook Type from r.

Types

type APIKey

type APIKey interface {
	Key() string
}

APIKey is an interface to API key.

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.

type Key

type Key string

Key is an APIKey.

func (Key) Key

func (k Key) Key() string

Key implements APIKey.

Jump to

Keyboard shortcuts

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