-
Notifications
You must be signed in to change notification settings - Fork 40
/
suppression_list.go
241 lines (194 loc) · 8.15 KB
/
suppression_list.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package gosparkpost
import (
"context"
"encoding/json"
"fmt"
"net/url"
)
// SuppressionListsPathFormat https://developers.sparkpost.com/api/#/reference/suppression-list
var SuppressionListsPathFormat = "/api/v%d/suppression-list"
// SuppressionEntry stores a recipient’s opt-out preferences. It is a list of recipient email addresses to which you do NOT want to send email.
// https://developers.sparkpost.com/api/suppression-list.html#header-list-entry-attributes
type SuppressionEntry struct {
// Email is used when list is stored
Email string `json:"email,omitempty"`
// Recipient is used when a list is returned
Recipient string `json:"recipient,omitempty"`
Transactional bool `json:"transactional,omitempty"`
NonTransactional bool `json:"non_transactional,omitempty"`
Source string `json:"source,omitempty"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Updated string `json:"updated,omitempty"`
Created string `json:"created,omitempty"`
SubAccountID string `json:"subaccount_id,omitempty"`
}
// WritableSuppressionEntry stores a recipient’s opt-out preferences. It is a list of recipient email addresses to which you do NOT want to send email.
// https://developers.sparkpost.com/api/suppression-list.html#suppression-list-bulk-insert-update-put
type WritableSuppressionEntry struct {
// Recipient is used when a list is returned
Recipient string `json:"recipient,omitempty"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
}
// SuppressionPage wraps suppression entries and response meta information
type SuppressionPage struct {
client *Client
Results []*SuppressionEntry `json:"results,omitempty"`
Recipients []SuppressionEntry `json:"recipients,omitempty"`
Errors []struct {
Message string `json:"message,omitempty"`
} `json:"errors,omitempty"`
TotalCount int `json:"total_count,omitempty"`
NextPage string
PrevPage string
FirstPage string
LastPage string
Links []struct {
Href string `json:"href"`
Rel string `json:"rel"`
} `json:"links,omitempty"`
Params map[string]string `json:"-"`
}
// SuppressionList retrieves the account's suppression list.
// Suppression lists larger than 10,000 entries will need to use cursor to retrieve more results.
// See https://developers.sparkpost.com/api/suppression-list.html#suppression-list-search-get
func (c *Client) SuppressionList(sp *SuppressionPage) (*Response, error) {
return c.SuppressionListContext(context.Background(), sp)
}
// SuppressionListContext retrieves the account's suppression list
func (c *Client) SuppressionListContext(ctx context.Context, sp *SuppressionPage) (*Response, error) {
path := fmt.Sprintf(SuppressionListsPathFormat, c.Config.ApiVersion)
return c.suppressionGet(ctx, c.Config.BaseUrl+path, sp)
}
// SuppressionRetrieve retrieves the suppression status for a specific recipient by specifying the recipient’s email address
// // https://developers.sparkpost.com/api/suppression-list.html#suppression-list-retrieve,-delete,-insert-or-update-get
func (c *Client) SuppressionRetrieve(email string, sp *SuppressionPage) (*Response, error) {
return c.SuppressionRetrieveContext(context.Background(), email, sp)
}
//SuppressionRetrieveContext retrieves the suppression status for a specific recipient by specifying the recipient’s email address
// // https://developers.sparkpost.com/api/suppression-list.html#suppression-list-retrieve,-delete,-insert-or-update-get
func (c *Client) SuppressionRetrieveContext(ctx context.Context, email string, sp *SuppressionPage) (*Response, error) {
path := fmt.Sprintf(SuppressionListsPathFormat, c.Config.ApiVersion)
finalURL := fmt.Sprintf("%s%s/%s", c.Config.BaseUrl, path, email)
return c.suppressionGet(ctx, finalURL, sp)
}
// SuppressionSearch search for suppression entries. For a list of parameters see
// https://developers.sparkpost.com/api/suppression-list.html#suppression-list-search-get
func (c *Client) SuppressionSearch(sp *SuppressionPage) (*Response, error) {
return c.SuppressionSearchContext(context.Background(), sp)
}
// SuppressionSearchContext search for suppression entries. For a list of parameters see
// https://developers.sparkpost.com/api/suppression-list.html#suppression-list-search-get
func (c *Client) SuppressionSearchContext(ctx context.Context, sp *SuppressionPage) (*Response, error) {
var finalURL string
path := fmt.Sprintf(SuppressionListsPathFormat, c.Config.ApiVersion)
if sp.Params == nil || len(sp.Params) == 0 {
finalURL = fmt.Sprintf("%s%s", c.Config.BaseUrl, path)
} else {
args := url.Values{}
for k, v := range sp.Params {
args.Add(k, v)
}
finalURL = fmt.Sprintf("%s%s?%s", c.Config.BaseUrl, path, args.Encode())
}
return c.suppressionGet(ctx, finalURL, sp)
}
// Next returns the next page of results from a previous MessageEventsSearch call
func (sp *SuppressionPage) Next() (*SuppressionPage, *Response, error) {
return sp.NextContext(context.Background())
}
// NextContext is the same as Next, and it accepts a context.Context
func (sp *SuppressionPage) NextContext(ctx context.Context) (*SuppressionPage, *Response, error) {
if sp.NextPage == "" {
return nil, nil, nil
}
suppressionPage := &SuppressionPage{}
suppressionPage.client = sp.client
finalURL := fmt.Sprintf("%s", sp.client.Config.BaseUrl+sp.NextPage)
res, err := sp.client.suppressionGet(ctx, finalURL, suppressionPage)
return suppressionPage, res, err
}
// SuppressionDelete deletes an entry from the suppression list
func (c *Client) SuppressionDelete(email string) (res *Response, err error) {
return c.SuppressionDeleteContext(context.Background(), email)
}
// SuppressionDeleteContext deletes an entry from the suppression list
func (c *Client) SuppressionDeleteContext(ctx context.Context, email string) (res *Response, err error) {
if email == "" {
err = fmt.Errorf("Deleting a suppression entry requires an email address")
return nil, err
}
path := fmt.Sprintf(SuppressionListsPathFormat, c.Config.ApiVersion)
finalURL := fmt.Sprintf("%s%s/%s", c.Config.BaseUrl, path, email)
res, err = c.HttpDelete(ctx, finalURL)
if err != nil {
return res, err
}
// We get an empty response on success. If there are errors we get JSON.
if _, err = res.AssertJson(); err == nil {
err = res.ParseResponse()
if err != nil {
return res, err
}
}
return res, res.HTTPError()
}
// SuppressionUpsert adds an entry to the suppression, or updates the existing entry
func (c *Client) SuppressionUpsert(entries []WritableSuppressionEntry) (*Response, error) {
return c.SuppressionUpsertContext(context.Background(), entries)
}
// SuppressionUpsertContext is the same as SuppressionUpsert, and it accepts a context.Context
func (c *Client) SuppressionUpsertContext(ctx context.Context, entries []WritableSuppressionEntry) (*Response, error) {
if entries == nil {
return nil, fmt.Errorf("`entries` cannot be nil")
}
path := fmt.Sprintf(SuppressionListsPathFormat, c.Config.ApiVersion)
type EntriesWrapper struct {
Recipients []WritableSuppressionEntry `json:"recipients,omitempty"`
}
entriesWrapper := EntriesWrapper{entries}
// Marshaling a static type won't fail
jsonBytes, _ := json.Marshal(entriesWrapper)
finalURL := c.Config.BaseUrl + path
return c.HttpPutJson(ctx, finalURL, jsonBytes)
}
// Wraps call to server and unmarshals response
func (c *Client) suppressionGet(ctx context.Context, finalURL string, sp *SuppressionPage) (*Response, error) {
// Send off our request
res, err := c.HttpGet(ctx, finalURL)
if err != nil {
return res, err
}
var body []byte
// Assert that we got a JSON Content-Type back
if body, err = res.AssertJson(); err != nil {
return res, err
}
err = res.ParseResponse()
if err != nil {
return res, err
}
// Parse expected response structure
err = json.Unmarshal(body, sp)
if err != nil {
return res, err
}
// For usage convenience parse out common links
for _, link := range sp.Links {
switch link.Rel {
case "next":
sp.NextPage = link.Href
case "previous":
sp.PrevPage = link.Href
case "first":
sp.FirstPage = link.Href
case "last":
sp.LastPage = link.Href
}
}
if sp.client == nil {
sp.client = c
}
return res, nil
}