This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
webhook.go
181 lines (152 loc) · 3.84 KB
/
webhook.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package oauth
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/robdimsdale/wundergo"
)
// Webhooks gets all webhooks for all lists.
func (c oauthClient) Webhooks() ([]wundergo.Webhook, error) {
lists, err := c.Lists()
if err != nil {
return nil, err
}
listCount := len(lists)
c.logger.Debug(
"webhooks",
map[string]interface{}{"listCount": listCount},
)
webhooksChan := make(chan []wundergo.Webhook, listCount)
idErrChan := make(chan idErr, listCount)
for _, l := range lists {
go func(list wundergo.List) {
c.logger.Debug(
"webhooks - getting webhooks for list",
map[string]interface{}{"listID": list.ID},
)
webhooks, err := c.WebhooksForListID(list.ID)
idErrChan <- idErr{idType: "list", id: list.ID, err: err}
webhooksChan <- webhooks
}(l)
}
e := multiIDErr{}
for i := 0; i < listCount; i++ {
idErr := <-idErrChan
if idErr.err != nil {
c.logger.Debug(
"webhooks - error received getting webhooks for list",
map[string]interface{}{"listID": idErr.id, "err": err},
)
e.addError(idErr)
}
}
totalWebhooks := []wundergo.Webhook{}
for i := 0; i < listCount; i++ {
webhooks := <-webhooksChan
totalWebhooks = append(totalWebhooks, webhooks...)
}
if len(e.errors()) > 0 {
return totalWebhooks, e
}
return totalWebhooks, nil
}
// WebhooksForListID returns Webhooks for the provided listID.
func (c oauthClient) WebhooksForListID(listID uint) ([]wundergo.Webhook, error) {
if listID == 0 {
return nil, errors.New("listID must be > 0")
}
url := fmt.Sprintf(
"%s/webhooks?list_id=%d",
c.apiURL,
listID,
)
req, err := c.newGetRequest(url)
if err != nil {
return nil, err
}
resp, err := c.do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Unexpected response code %d - expected %d", resp.StatusCode, http.StatusOK)
}
webhooks := []wundergo.Webhook{}
err = json.NewDecoder(resp.Body).Decode(&webhooks)
if err != nil {
return nil, err
}
return webhooks, nil
}
// Webhook returns the Webhook for the corresponding webhookID.
func (c oauthClient) Webhook(webhookID uint) (wundergo.Webhook, error) {
allWebhooks, err := c.Webhooks()
for _, w := range allWebhooks {
if w.ID == webhookID {
return w, err
}
}
return wundergo.Webhook{}, fmt.Errorf("webhook not found")
}
// CreateWebhook creates a new webhook with the provided parameters.
// listID must be non-zero; the remaining parameters are not validated.
func (c oauthClient) CreateWebhook(
listID uint,
url string,
processorType string,
configuration string,
) (wundergo.Webhook, error) {
if listID == 0 {
return wundergo.Webhook{}, errors.New("listID must be > 0")
}
body := []byte(fmt.Sprintf(`{
"list_id":%d,
"url":"%s",
"processor_type":"%s",
"configuration":"%s"
}`,
listID,
url,
processorType,
configuration,
))
reqURL := fmt.Sprintf("%s/webhooks", c.apiURL)
req, err := c.newPostRequest(reqURL, body)
if err != nil {
return wundergo.Webhook{}, err
}
resp, err := c.do(req)
if err != nil {
return wundergo.Webhook{}, err
}
if resp.StatusCode != http.StatusCreated {
return wundergo.Webhook{}, fmt.Errorf("Unexpected response code %d - expected %d", resp.StatusCode, http.StatusCreated)
}
webhook := wundergo.Webhook{}
err = json.NewDecoder(resp.Body).Decode(&webhook)
if err != nil {
return wundergo.Webhook{}, err
}
return webhook, nil
}
// DeleteNote deletes the provided webhook.
func (c oauthClient) DeleteWebhook(webhook wundergo.Webhook) error {
url := fmt.Sprintf(
"%s/webhooks/%d",
c.apiURL,
webhook.ID,
)
req, err := c.newDeleteRequest(url)
if err != nil {
return err
}
resp, err := c.do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("Unexpected response code %d - expected %d", resp.StatusCode, http.StatusNoContent)
}
return nil
}