-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendgrid.go
149 lines (138 loc) · 4.06 KB
/
sendgrid.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
package sgew
import (
"encoding/json"
"errors"
"fmt"
"github.com/sendgrid/sendgrid-go"
)
type sendGrid struct {
apiKey string
host string
}
func newSendGrid(apiKey string, host string) *sendGrid {
return &sendGrid{
apiKey: apiKey,
host: host,
}
}
type GetAllEventWebhooksOutput struct {
Webhooks []struct {
ID string `json:"id"`
CreatedDate string `json:"created_date"`
Enabled bool `json:"enabled"`
FriendlyName string `json:"friendly_name"`
URL string `json:"url"`
} `json:"webhooks"`
}
func (s *sendGrid) getAllEventWebhooks() (*GetAllEventWebhooksOutput, error) {
endpoint := "/v3/user/webhooks/event/settings/all"
request := sendgrid.GetRequest(s.apiKey, endpoint, s.host)
request.Method = "GET"
response, err := sendgrid.API(request)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, errors.New(endpoint + ": expected status is 200, got status is " + fmt.Sprint(response.StatusCode))
}
output := &GetAllEventWebhooksOutput{}
err = json.Unmarshal([]byte(response.Body), output)
if err != nil {
return nil, err
}
return output, nil
}
// ref. https://docs.sendgrid.com/api-reference/webhooks/test-event-notification-settings
func (s *sendGrid) sendEventNotificationForTest(id string, url string) error {
endpoint := "/v3/user/webhooks/event/test"
request := sendgrid.GetRequest(s.apiKey, endpoint, s.host)
request.Method = "POST"
b, err := json.Marshal(&triggerInput{
ID: id,
URL: url,
})
if err != nil {
return err
}
request.Body = b
response, err := sendgrid.API(request)
if err != nil {
return err
}
if response.StatusCode != 204 {
return errors.New(endpoint + ": expected status is 204, got status is " + fmt.Sprint(response.StatusCode))
}
if err != nil {
return err
}
return nil
}
type createWebhookInput struct {
Enabled bool `json:"enabled"`
URL string `json:"url"`
GroupReSubscribe bool `json:"group_resubscribe"`
Delivered bool `json:"delivered"`
GroupUnsubscribe bool `json:"group_unsubscribe"`
SpamReport bool `json:"spam_report"`
Bounce bool `json:"bounce"`
Deferred bool `json:"deferred"`
Unsubscribe bool `json:"unsubscribe"`
Processed bool `json:"processed"`
Open bool `json:"open"`
Click bool `json:"click"`
Dropped bool `json:"dropped"`
FriendlyName string `json:"friendly_name"`
}
type createWebhookOutput struct {
ID string `json:"id"`
}
// ref. https://docs.sendgrid.com/api-reference/webhooks/create-an-event-webhook
func (s *sendGrid) CreateEventWebhook(url string) (*createWebhookOutput, error) {
endpoint := "/v3/user/webhooks/event/settings"
request := sendgrid.GetRequest(s.apiKey, endpoint, s.host)
b, err := json.Marshal(&createWebhookInput{
Enabled: true,
URL: url,
GroupReSubscribe: true,
Delivered: true,
GroupUnsubscribe: true,
SpamReport: true,
Bounce: true,
Deferred: true,
Unsubscribe: true,
Processed: true,
Open: true,
Click: true,
Dropped: true,
FriendlyName: "created by sgew",
})
if err != nil {
return nil, err
}
request.Method = "POST"
request.Body = b
response, err := sendgrid.API(request)
if err != nil {
return nil, err
}
if response.StatusCode != 201 {
return nil, errors.New(endpoint + ": expected status is 201, got status is " + fmt.Sprint(response.StatusCode))
}
output := &createWebhookOutput{}
json.Unmarshal([]byte(response.Body), output)
return output, nil
}
// ref. https://docs.sendgrid.com/api-reference/webhooks/delete-an-event-webhook
func (s *sendGrid) deleteEventWebhook(id string) error {
endpoint := "/v3/user/webhooks/event/settings/" + id
request := sendgrid.GetRequest(s.apiKey, endpoint, s.host)
request.Method = "DELETE"
response, err := sendgrid.API(request)
if err != nil {
return err
}
if response.StatusCode != 204 {
return errors.New(endpoint + ": expected status is 204, got status is " + fmt.Sprint(response.StatusCode))
}
return nil
}