Skip to content

Commit

Permalink
Alternative implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Jun 13, 2019
1 parent a10d009 commit 6e37db9
Show file tree
Hide file tree
Showing 4 changed files with 400 additions and 606 deletions.
209 changes: 61 additions & 148 deletions dnsimple/webhook/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ import (
"github.com/dnsimple/dnsimple-go/dnsimple"
)

func switchEvent(name string, payload []byte) (Event, error) {
var event Event
func switchEventData(event *EventContainer) (EventDataContainer, error) {
var dataContainer EventDataContainer

switch name {
switch event.Name {
case // account
"account.billing_settings_update",
"account.update",
"account.user_invitation_accept",
"account.user_invite":
event = &AccountEvent{}
dataContainer = &AccountEventData{}
case // certificate
"certificate.remove_private_key":
event = &CertificateEvent{}
dataContainer = &CertificateEventData{}
case // contact
"contact.create",
"contact.delete",
"contact.update":
event = &ContactEvent{}
dataContainer = &ContactEventData{}
case // dnssec
"dnssec.rotation_complete",
"dnssec.rotation_start":
event = &DNSSECEvent{}
dataContainer = &DNSSECEventData{}
case // domain
"domain.auto_renewal_disable",
"domain.auto_renewal_enable",
Expand All @@ -38,268 +38,181 @@ func switchEvent(name string, payload []byte) (Event, error) {
"domain.resolution_disable",
"domain.resolution_enable",
"domain.transfer": // TODO
event = &DomainEvent{}
dataContainer = &DomainEventData{}
case // email forward
"email_forward.create",
"email_forward.delete",
"email_forward.update":
event = &EmailForwardEvent{}
dataContainer = &EmailForwardEventData{}
case // webhook
"webhook.create",
"webhook.delete":
event = &WebhookEvent{}
dataContainer = &WebhookEventData{}
case // whois privacy
"whois_privacy.disable",
"whois_privacy.enable",
"whois_privacy.purchase",
"whois_privacy.renew": // TODO
event = &WhoisPrivacyEvent{}
dataContainer = &WhoisPrivacyEventData{}
case // zone
"zone.create",
"zone.delete":
event = &ZoneEvent{}
dataContainer = &ZoneEventData{}
case // zone record
"zone_record.create",
"zone_record.delete",
"zone_record.update":
event = &ZoneRecordEvent{}
dataContainer = &ZoneRecordEventData{}
default:
event = &GenericEvent{}
dataContainer = &GenericEventData{}
}

return event, event.parse(payload)
err := dataContainer.unmarshalEventData(event.payload)
return dataContainer, err
}

//
// GenericEvent
//

// GenericEvent represents a generic event, where the data is a simple map of strings.
type GenericEvent struct {
AbstractEvent
Data interface{} `json:"data"`
}

func (e *GenericEvent) parse(payload []byte) error {
e.eventPayload = payload
return unmashalEvent(payload, e)
}
// GenericEventData represents the data node for a generic event, where the data is a simple map of strings.
type GenericEventData map[string]interface{}

// ParseGenericEvent unpacks the data into a GenericEvent.
func ParseGenericEvent(e *GenericEvent, payload []byte) error {
return e.parse(payload)
func (d *GenericEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// AccountEvent
//

// AccountEvent represents the base event sent for an account action.
type AccountEvent struct {
AbstractEvent
Data *AccountEvent `json:"data"`
// AccountEventData represents the data node for an Account event.
type AccountEventData struct {
Account *dnsimple.Account `json:"account"`
}

// ParseAccountEvent unpacks the data into an AccountEvent.
func ParseAccountEvent(e *AccountEvent, payload []byte) error {
return e.parse(payload)
}

func (e *AccountEvent) parse(payload []byte) error {
e.eventPayload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *AccountEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// CertificateEvent
//

// CertificateEvent represents the base event sent for a certificate action.
type CertificateEvent struct {
AbstractEvent
Data *CertificateEvent `json:"data"`
// CertificateEventData represents the data node for a Certificate event.
type CertificateEventData struct {
Certificate *dnsimple.Certificate `json:"certificate"`
}

// ParseCertificateEvent unpacks the data into a CertificateEvent.
func ParseCertificateEvent(e *CertificateEvent, payload []byte) error {
return e.parse(payload)
}

func (e *CertificateEvent) parse(payload []byte) error {
e.eventPayload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *CertificateEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// ContactEvent
//

// ContactEvent represents the base event sent for a contact action.
type ContactEvent struct {
AbstractEvent
Data *ContactEvent `json:"data"`
// ContactEventData represents the data node for a Contact event.
type ContactEventData struct {
Contact *dnsimple.Contact `json:"contact"`
}

// ParseContactEvent unpacks the data into a ContactEvent.
func ParseContactEvent(e *ContactEvent, payload []byte) error {
return e.parse(payload)
}

func (e *ContactEvent) parse(payload []byte) error {
e.eventPayload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *ContactEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// DNSSECEvent
//

// DNSSECEvent represents the base event sent for a DNSSEC action.
type DNSSECEvent struct {
AbstractEvent
Data *DNSSECEvent `json:"data"`
// DNSSECEventData represents the data node for a DNSSEC event.
type DNSSECEventData struct {
DelegationSignerRecord *dnsimple.DelegationSignerRecord `json:"delegation_signer_record"`
//DNSSECConfig *dnsimple.DNSSECConfig `json:"dnssec"`
}

// ParseDNSSECEvent unpacks the payload into a DNSSECEvent.
func ParseDNSSECEvent(e *DNSSECEvent, payload []byte) error {
return e.parse(payload)
}

func (e *DNSSECEvent) parse(payload []byte) error {
e.eventPayload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *DNSSECEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// DomainEvent
//

// DomainEvent represents the base event sent for a domain action.
type DomainEvent struct {
AbstractEvent
Data *DomainEvent `json:"data"`
// DomainEventData represents the data node for a Contact event.
type DomainEventData struct {
Auto bool `json:"auto"`
Domain *dnsimple.Domain `json:"domain"`
Registrant *dnsimple.Contact `json:"registrant"`
Delegation *dnsimple.Delegation `json:"name_servers"`
}

// ParseDomainEvent unpacks the payload into a DomainEvent.
func ParseDomainEvent(e *DomainEvent, payload []byte) error {
return e.parse(payload)
}

func (e *DomainEvent) parse(payload []byte) error {
e.eventPayload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *DomainEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// EmailForwardEvent
//

// EmailForwardEvent represents the base event sent for an email forward action.
type EmailForwardEvent struct {
AbstractEvent
Data *EmailForwardEvent `json:"data"`
// EmailForwardEventData represents the data node for a EmailForward event.
type EmailForwardEventData struct {
EmailForward *dnsimple.EmailForward `json:"email_forward"`
}

// ParseEmailForwardEvent unpacks the payload into a EmailForwardEvent.
func ParseEmailForwardEvent(e *EmailForwardEvent, payload []byte) error {
return e.parse(payload)
}

func (e *EmailForwardEvent) parse(payload []byte) error {
e.eventPayload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *EmailForwardEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// WebhookEvent
//

// WebhookEvent represents the base event sent for a webhook action.
type WebhookEvent struct {
AbstractEvent
Data *WebhookEvent `json:"data"`
// WebhookEventData represents the data node for a Webhook event.
type WebhookEventData struct {
Webhook *dnsimple.Webhook `json:"webhook"`
}

// ParseWebhookEvent unpacks the data into a WebhookEvent.
func ParseWebhookEvent(e *WebhookEvent, payload []byte) error {
return e.parse(payload)
}

func (e *WebhookEvent) parse(payload []byte) error {
e.eventPayload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *WebhookEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// WhoisPrivacyEvent
//

// WhoisPrivacyEvent represents the base event sent for a whois privacy action.
type WhoisPrivacyEvent struct {
AbstractEvent
Data *WhoisPrivacyEvent `json:"data"`
// WhoisPrivacyEventData represents the data node for a WhoisPrivacy event.
type WhoisPrivacyEventData struct {
Domain *dnsimple.Domain `json:"domain"`
WhoisPrivacy *dnsimple.WhoisPrivacy `json:"whois_privacy"`
}

// ParseWhoisPrivacyEvent unpacks the data into a WhoisPrivacyEvent.
func ParseWhoisPrivacyEvent(e *WhoisPrivacyEvent, payload []byte) error {
return e.parse(payload)
}

func (e *WhoisPrivacyEvent) parse(payload []byte) error {
e.eventPayload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *WhoisPrivacyEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// ZoneEvent
//

// ZoneEvent represents the base event sent for a zone action.
type ZoneEvent struct {
AbstractEvent
Data *ZoneEvent `json:"data"`
// ZoneEventData represents the data node for a Zone event.
type ZoneEventData struct {
Zone *dnsimple.Zone `json:"zone"`
}

// ParseZoneEvent unpacks the data into a ZoneEvent.
func ParseZoneEvent(e *ZoneEvent, payload []byte) error {
return e.parse(payload)
}

func (e *ZoneEvent) parse(payload []byte) error {
e.eventPayload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *ZoneEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// ZoneRecordEvent
//

// ZoneRecordEvent represents the base event sent for a zone record action.
type ZoneRecordEvent struct {
AbstractEvent
Data *ZoneRecordEvent `json:"data"`
// ZoneRecordEventData represents the data node for a ZoneRecord event.
type ZoneRecordEventData struct {
ZoneRecord *dnsimple.ZoneRecord `json:"zone_record"`
}

// ParseZoneRecordEvent unpacks the data into a ZoneRecordEvent.
func ParseZoneRecordEvent(e *ZoneRecordEvent, payload []byte) error {
return e.parse(payload)
}

func (e *ZoneRecordEvent) parse(payload []byte) error {
e.eventPayload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *ZoneRecordEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}
Loading

0 comments on commit 6e37db9

Please sign in to comment.