forked from hanzoai/gochimp3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhooks.go
96 lines (74 loc) · 2.29 KB
/
webhooks.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
package gochimp3
import "fmt"
const (
webhooks_path = "/lists/%s/webhooks"
single_webhook_path = webhooks_path + "/%s"
)
type ListOfWebHooks struct {
baseList
ListID string `json:"list_id"`
WebHooks []WebHook `json:"webhooks"`
}
type WebHookRequest struct {
URL string `json:"url"`
Events HookEvents `json:"events"`
Sources HookSources `json:"sources"`
}
type WebHook struct {
WebHookRequest
ListID string `json:"list_id"`
withLinks
}
type HookSources struct {
User bool `json:"user"`
Admin bool `json:"admin"`
API bool `json:"api"`
}
type HookEvents struct {
Subscribe bool `json:"subscribe"`
Unsubscribe bool `json:"unsubscribe"`
Profile bool `json:"profile"`
Cleaned bool `json:"cleaned"`
Upemail bool `json:"upemail"`
Campaign bool `json:"campaign"`
}
func (list ListResponse) CreateWebHooks(body *WebHookRequest) (*WebHook, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(webhooks_path, list.ID)
response := new(WebHook)
return response, list.api.Request("POST", endpoint, nil, &body, response)
}
func (list ListResponse) UpdateWebHook(id string, body *WebHookRequest) (*WebHook, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(single_webhook_path, list.ID, id)
response := new(WebHook)
return response, list.api.Request("PATCH", endpoint, nil, &body, response)
}
// TODO - does this take filters? undocumented
func (list ListResponse) GetWebHooks() (*ListOfWebHooks, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(webhooks_path, list.ID)
response := new(ListOfWebHooks)
return response, list.api.Request("GET", endpoint, nil, nil, response)
}
func (list ListResponse) GetWebHook(id string) (*WebHook, error) {
if err := list.CanMakeRequest(); err != nil {
return nil, err
}
endpoint := fmt.Sprintf(single_webhook_path, list.ID, id)
response := new(WebHook)
return response, list.api.Request("GET", endpoint, nil, nil, response)
}
func (list ListResponse) DeleteWebHook(id string) (bool, error) {
if err := list.CanMakeRequest(); err != nil {
return false, err
}
endpoint := fmt.Sprintf(single_webhook_path, list.ID, id)
return list.api.RequestOk("DELETE", endpoint)
}