/
webhook_subscription.go
144 lines (111 loc) · 4.15 KB
/
webhook_subscription.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package dwolla
import (
"context"
"errors"
"fmt"
)
// WebhookSubscriptionService is the webhook subscription service interface
//
// see: https://docsv2.dwolla.com/#webhook-subscriptions
type WebhookSubscriptionService interface {
Create(context.Context, *WebhookSubscriptionRequest) (*WebhookSubscription, error)
Retrieve(context.Context, string) (*WebhookSubscription, error)
List(context.Context) (*WebhookSubscriptions, error)
Remove(context.Context, string) error
}
// WebhookSubscriptionServiceOp is an implementation of the webhook
// subscription service interface
type WebhookSubscriptionServiceOp struct {
client *Client
}
// WebhookSubscription is a webhook subscription
type WebhookSubscription struct {
Resource
ID string `json:"id"`
URL string `json:"url"`
Created string `json:"created"`
}
// WebhookSubscriptions is a collection of webhook subscriptions
type WebhookSubscriptions struct {
Collection
Embedded map[string][]WebhookSubscription `json:"_embedded"`
Total int `json:"total"`
}
// WebhookSubscriptionRequest is a webhook subscription request
type WebhookSubscriptionRequest struct {
URL string `json:"url,omitempty"`
Secret string `json:"secret,omitempty"`
Paused bool `json:"paused"`
}
// Create creates a webhook subscription
func (w *WebhookSubscriptionServiceOp) Create(ctx context.Context, body *WebhookSubscriptionRequest) (*WebhookSubscription, error) {
var subscription WebhookSubscription
if err := w.client.Post(ctx, "webhook-subscriptions", body, nil, &subscription); err != nil {
return nil, err
}
subscription.client = w.client
return &subscription, nil
}
// Retrieve retrieves the webhook subscription matching id
func (w *WebhookSubscriptionServiceOp) Retrieve(ctx context.Context, id string) (*WebhookSubscription, error) {
var subscription WebhookSubscription
if err := w.client.Get(ctx, fmt.Sprintf("webhook-subscriptions/%s", id), nil, nil, &subscription); err != nil {
return nil, err
}
subscription.client = w.client
return &subscription, nil
}
// List returns a list of webhook subscriptions
func (w *WebhookSubscriptionServiceOp) List(ctx context.Context) (*WebhookSubscriptions, error) {
var subscriptions WebhookSubscriptions
if err := w.client.Get(ctx, "webhook-subscriptions", nil, nil, &subscriptions); err != nil {
return nil, err
}
subscriptions.client = w.client
for i := range subscriptions.Embedded["webhook-subscriptions"] {
subscriptions.Embedded["webhook-subscriptions"][i].client = w.client
}
return &subscriptions, nil
}
// Remove removes the webhook subscription matching the id
func (w *WebhookSubscriptionServiceOp) Remove(ctx context.Context, id string) error {
return w.client.Delete(ctx, fmt.Sprintf("webhook-subscriptions/%s", id), nil, nil)
}
// Pause pauses the webhook subscription
func (w *WebhookSubscription) Pause(ctx context.Context) error {
if _, ok := w.Links["self"]; !ok {
return errors.New("No self resource link")
}
body := &WebhookSubscriptionRequest{Paused: true}
return w.client.Post(ctx, w.Links["self"].Href, body, nil, w)
}
// Remove removes the webhook subscription
func (w *WebhookSubscription) Remove(ctx context.Context) error {
if _, ok := w.Links["self"]; !ok {
return errors.New("No self resource link")
}
return w.client.Delete(ctx, w.Links["self"].Href, nil, nil)
}
// Unpause unpauses the webhook subscription
func (w *WebhookSubscription) Unpause(ctx context.Context) error {
if _, ok := w.Links["self"]; !ok {
return errors.New("No self resource link")
}
body := &WebhookSubscriptionRequest{Paused: false}
return w.client.Post(ctx, w.Links["self"].Href, body, nil, w)
}
// RetrieveWebhooks returns webhooks for this webhook subscription
func (w *Webhook) RetrieveWebhooks(ctx context.Context) (*Webhooks, error) {
var webhooks Webhooks
if _, ok := w.Links["webhooks"]; !ok {
return nil, errors.New("No webhooks resource link")
}
if err := w.client.Get(ctx, w.Links["webhooks"].Href, nil, nil, &webhooks); err != nil {
return nil, err
}
webhooks.client = w.client
for i := range webhooks.Embedded["webhooks"] {
webhooks.Embedded["webhooks"][i].client = w.client
}
return &webhooks, nil
}