-
Notifications
You must be signed in to change notification settings - Fork 143
/
mailing_lists.go
269 lines (247 loc) · 8.31 KB
/
mailing_lists.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package mailgun
import (
"context"
"strconv"
)
// A mailing list may have one of three membership modes.
const (
// ReadOnly specifies that nobody, including Members, may send messages to
// the mailing list. Messages distributed on such lists come from list
// administrator accounts only.
AccessLevelReadOnly = "readonly"
// Members specifies that only those who subscribe to the mailing list may
// send messages.
AccessLevelMembers = "members"
// Everyone specifies that anyone and everyone may both read and submit
// messages to the mailing list, including non-subscribers.
AccessLevelEveryone = "everyone"
)
// Specify the access of a mailing list member
type AccessLevel string
// Replies to a mailing list should go to one of two preferred destinations.
const (
// List specifies that replies should be sent to the mailing list address.
ReplyPreferenceList = "list"
// Sender specifies that replies should be sent to the sender (FROM) address.
ReplyPreferenceSender = "sender"
)
// Set where replies should go
type ReplyPreference string
// A List structure provides information for a mailing list.
//
// AccessLevel may be one of ReadOnly, Members, or Everyone.
type MailingList struct {
Address string `json:"address,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
AccessLevel AccessLevel `json:"access_level,omitempty"`
ReplyPreference ReplyPreference `json:"reply_preference,omitempty"`
CreatedAt RFC2822Time `json:"created_at,omitempty"`
MembersCount int `json:"members_count,omitempty"`
}
type listsResponse struct {
Items []MailingList `json:"items"`
Paging Paging `json:"paging"`
}
type mailingListResponse struct {
MailingList MailingList `json:"list"`
}
type ListsIterator struct {
listsResponse
mg Mailgun
err error
}
// ListMailingLists returns the specified set of mailing lists administered by your account.
func (mg *MailgunImpl) ListMailingLists(opts *ListOptions) *ListsIterator {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/pages")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
if opts != nil {
if opts.Limit != 0 {
r.addParameter("limit", strconv.Itoa(opts.Limit))
}
}
url, err := r.generateUrlWithParameters()
return &ListsIterator{
mg: mg,
listsResponse: listsResponse{Paging: Paging{Next: url, First: url}},
err: err,
}
}
// If an error occurred during iteration `Err()` will return non nil
func (li *ListsIterator) Err() error {
return li.err
}
// Next retrieves the next page of items from the api. Returns false when there
// no more pages to retrieve or if there was an error. Use `.Err()` to retrieve
// the error
func (li *ListsIterator) Next(ctx context.Context, items *[]MailingList) bool {
if li.err != nil {
return false
}
li.err = li.fetch(ctx, li.Paging.Next)
if li.err != nil {
return false
}
cpy := make([]MailingList, len(li.Items))
copy(cpy, li.Items)
*items = cpy
if len(li.Items) == 0 {
return false
}
return true
}
// First retrieves the first page of items from the api. Returns false if there
// was an error. It also sets the iterator object to the first page.
// Use `.Err()` to retrieve the error.
func (li *ListsIterator) First(ctx context.Context, items *[]MailingList) bool {
if li.err != nil {
return false
}
li.err = li.fetch(ctx, li.Paging.First)
if li.err != nil {
return false
}
cpy := make([]MailingList, len(li.Items))
copy(cpy, li.Items)
*items = cpy
return true
}
// Last retrieves the last page of items from the api.
// Calling Last() is invalid unless you first call First() or Next()
// Returns false if there was an error. It also sets the iterator object
// to the last page. Use `.Err()` to retrieve the error.
func (li *ListsIterator) Last(ctx context.Context, items *[]MailingList) bool {
if li.err != nil {
return false
}
li.err = li.fetch(ctx, li.Paging.Last)
if li.err != nil {
return false
}
cpy := make([]MailingList, len(li.Items))
copy(cpy, li.Items)
*items = cpy
return true
}
// Previous retrieves the previous page of items from the api. Returns false when there
// no more pages to retrieve or if there was an error. Use `.Err()` to retrieve
// the error if any
func (li *ListsIterator) Previous(ctx context.Context, items *[]MailingList) bool {
if li.err != nil {
return false
}
if li.Paging.Previous == "" {
return false
}
li.err = li.fetch(ctx, li.Paging.Previous)
if li.err != nil {
return false
}
cpy := make([]MailingList, len(li.Items))
copy(cpy, li.Items)
*items = cpy
if len(li.Items) == 0 {
return false
}
return true
}
func (li *ListsIterator) fetch(ctx context.Context, url string) error {
li.Items = nil
r := newHTTPRequest(url)
r.setClient(li.mg.Client())
r.setBasicAuth(basicAuthUser, li.mg.APIKey())
return getResponseFromJSON(ctx, r, &li.listsResponse)
}
// CreateMailingList creates a new mailing list under your Mailgun account.
// You need specify only the Address and Name members of the prototype;
// Description, AccessLevel and ReplyPreference are optional.
// If unspecified, Description remains blank,
// while AccessLevel defaults to Everyone
// and ReplyPreference defaults to List.
func (mg *MailgunImpl) CreateMailingList(ctx context.Context, prototype MailingList) (MailingList, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
if prototype.Address != "" {
p.addValue("address", prototype.Address)
}
if prototype.Name != "" {
p.addValue("name", prototype.Name)
}
if prototype.Description != "" {
p.addValue("description", prototype.Description)
}
if prototype.AccessLevel != "" {
p.addValue("access_level", string(prototype.AccessLevel))
}
if prototype.ReplyPreference != "" {
p.addValue("reply_preference", string(prototype.ReplyPreference))
}
response, err := makePostRequest(ctx, r, p)
if err != nil {
return MailingList{}, err
}
var l MailingList
err = response.parseFromJSON(&l)
return l, err
}
// DeleteMailingList removes all current members of the list, then removes the list itself.
// Attempts to send e-mail to the list will fail subsequent to this call.
func (mg *MailgunImpl) DeleteMailingList(ctx context.Context, addr string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/" + addr)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
}
// GetMailingList allows your application to recover the complete List structure
// representing a mailing list, so long as you have its e-mail address.
func (mg *MailgunImpl) GetMailingList(ctx context.Context, addr string) (MailingList, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/" + addr)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
response, err := makeGetRequest(ctx, r)
if err != nil {
return MailingList{}, err
}
var resp mailingListResponse
err = response.parseFromJSON(&resp)
return resp.MailingList, err
}
// UpdateMailingList allows you to change various attributes of a list.
// Address, Name, Description, AccessLevel and ReplyPreference are all optional;
// only those fields which are set in the prototype will change.
//
// Be careful! If changing the address of a mailing list,
// e-mail sent to the old address will not succeed.
// Make sure you account for the change accordingly.
func (mg *MailgunImpl) UpdateMailingList(ctx context.Context, addr string, prototype MailingList) (MailingList, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/" + addr)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
if prototype.Address != "" {
p.addValue("address", prototype.Address)
}
if prototype.Name != "" {
p.addValue("name", prototype.Name)
}
if prototype.Description != "" {
p.addValue("description", prototype.Description)
}
if prototype.AccessLevel != "" {
p.addValue("access_level", string(prototype.AccessLevel))
}
if prototype.ReplyPreference != "" {
p.addValue("reply_preference", string(prototype.ReplyPreference))
}
var l MailingList
response, err := makePutRequest(ctx, r, p)
if err != nil {
return l, err
}
err = response.parseFromJSON(&l)
return l, err
}