1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
// Package webhook provides the support for reading and parsing the events
// sent from DNSimple via webhook.
package webhook
import (
"encoding/json"
"github.com/dnsimple/dnsimple-go/dnsimple"
)
// Actor represents the entity that triggered the event. It can be either an user,
// a DNSimple support representative or the DNSimple system.
type Actor struct {
ID string `json:"id"`
Entity string `json:"entity"`
Pretty string `json:"pretty"`
}
// Account represents the account that this event is attached to.
type Account struct {
dnsimple.Account
// Display is a string that can be used as a display label
// and it is sent in a webhook payload
// It generally represent the name of the account.
Display string `json:"display,omitempty"`
// Identifier is a human-readable string identifier
// and it is sent in a webhook payload
// It generally represent the StringID or email of the account.
Identifier string `json:"identifier,omitempty"`
}
// Event represents a webhook event.
type Event struct {
APIVersion string `json:"api_version"`
RequestID string `json:"request_identifier"`
Name string `json:"name"`
Actor *Actor `json:"actor"`
Account *Account `json:"account"`
data EventDataContainer
payload []byte
}
// EventDataContainer defines the container for the event payload data.
type EventDataContainer interface {
unmarshalEventData([]byte) error
}
// GetData returns the data container for the specific event type.
func (e *Event) GetData() EventDataContainer {
return e.data
}
// GetPayload returns the data payload.
func (e *Event) GetPayload() []byte {
return e.payload
}
// ParseEvent takes an event payload and attempts to deserialize the payload into an Event.
//
// The event data will be set with a data type that matches the event action in the payload.
// If no direct match is found, then a GenericEventData is assigned.
//
// The event data type is an EventContainerData interface. Therefore, you must perform typecasting
// to access any type-specific field.
func ParseEvent(payload []byte) (*Event, error) {
e := &Event{payload: payload}
if err := json.Unmarshal(payload, e); err != nil {
return nil, err
}
data, err := switchEventData(e)
if err != nil {
return nil, err
}
e.data = data
return e, nil
}
type eventDataStruct struct {
Data interface{} `json:"data"`
}
func unmarshalEventData(data []byte, v interface{}) error {
return json.Unmarshal(data, &eventDataStruct{Data: v})
}
|