forked from glycerine/sshego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailing_lists.go
317 lines (299 loc) · 10.7 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package mailgun
import (
"encoding/json"
"fmt"
"strconv"
)
// A mailing list may have one of three membership modes.
// ReadOnly specifies that nobody, including Members,
// may send messages to the mailing list.
// Messages distributed on such lists come from list administrator accounts only.
// Members specifies that only those who subscribe to the mailing list may send messages.
// Everyone specifies that anyone and everyone may both read and submit messages
// to the mailing list, including non-subscribers.
const (
ReadOnly = "readonly"
Members = "members"
Everyone = "everyone"
)
// Mailing list members have an attribute that determines if they've subscribed to the mailing list or not.
// This attribute may be used to filter the results returned by GetSubscribers().
// All, Subscribed, and Unsubscribed provides a convenient and readable syntax for specifying the scope of the search.
var (
All *bool = nil
Subscribed *bool = &yes
Unsubscribed *bool = &no
)
// yes and no are variables which provide us the ability to take their addresses.
// Subscribed and Unsubscribed are pointers to these booleans.
//
// We use a pointer to boolean as a kind of trinary data type:
// if nil, the relevant data type remains unspecified.
// Otherwise, its value is either true or false.
var (
yes bool = true
no bool = false
)
// A List structure provides information for a mailing list.
//
// AccessLevel may be one of ReadOnly, Members, or Everyone.
type List struct {
Address string `json:"address",omitempty"`
Name string `json:"name",omitempty"`
Description string `json:"description",omitempty"`
AccessLevel string `json:"access_level",omitempty"`
CreatedAt string `json:"created_at",omitempty"`
MembersCount int `json:"members_count",omitempty"`
}
// A Member structure represents a member of the mailing list.
// The Vars field can represent any JSON-encodable data.
type Member struct {
Address string `json:"address,omitempty"`
Name string `json:"name,omitempty"`
Subscribed *bool `json:"subscribed,omitempty"`
Vars map[string]interface{} `json:"vars,omitempty"`
}
// GetLists returns the specified set of mailing lists administered by your account.
func (mg *MailgunImpl) GetLists(limit, skip int, filter string) (int, []List, error) {
r := newHTTPRequest(generatePublicApiUrl(listsEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
p := newUrlEncodedPayload()
if limit != DefaultLimit {
p.addValue("limit", strconv.Itoa(limit))
}
if skip != DefaultSkip {
p.addValue("skip", strconv.Itoa(skip))
}
if filter != "" {
p.addValue("address", filter)
}
var envelope struct {
Items []List `json:"items"`
TotalCount int `json:"total_count"`
}
response, err := makeRequest(r, "GET", p)
if err != nil {
return -1, nil, err
}
err = response.parseFromJSON(&envelope)
return envelope.TotalCount, envelope.Items, err
}
// CreateList creates a new mailing list under your Mailgun account.
// You need specify only the Address and Name members of the prototype;
// Description, and AccessLevel are optional.
// If unspecified, Description remains blank,
// while AccessLevel defaults to Everyone.
func (mg *MailgunImpl) CreateList(prototype List) (List, error) {
r := newHTTPRequest(generatePublicApiUrl(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", prototype.AccessLevel)
}
response, err := makePostRequest(r, p)
if err != nil {
return List{}, err
}
var l List
err = response.parseFromJSON(&l)
return l, err
}
// DeleteList 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) DeleteList(addr string) error {
r := newHTTPRequest(generatePublicApiUrl(listsEndpoint) + "/" + addr)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
_, err := makeDeleteRequest(r)
return err
}
// GetListByAddress 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) GetListByAddress(addr string) (List, error) {
r := newHTTPRequest(generatePublicApiUrl(listsEndpoint) + "/" + addr)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
response, err := makeGetRequest(r)
var envelope struct {
List `json:"list"`
}
err = response.parseFromJSON(&envelope)
return envelope.List, err
}
// UpdateList allows you to change various attributes of a list.
// Address, Name, Description, and AccessLevel 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) UpdateList(addr string, prototype List) (List, error) {
r := newHTTPRequest(generatePublicApiUrl(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", prototype.AccessLevel)
}
var l List
response, err := makePutRequest(r, p)
if err != nil {
return l, err
}
err = response.parseFromJSON(&l)
return l, err
}
// GetMembers returns the list of members belonging to the indicated mailing list.
// The s parameter can be set to one of three settings to help narrow the returned data set:
// All indicates that you want both Members and unsubscribed members alike, while
// Subscribed and Unsubscribed indicate you want only those eponymous subsets.
func (mg *MailgunImpl) GetMembers(limit, skip int, s *bool, addr string) (int, []Member, error) {
r := newHTTPRequest(generateMemberApiUrl(listsEndpoint, addr))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
p := newUrlEncodedPayload()
if limit != DefaultLimit {
p.addValue("limit", strconv.Itoa(limit))
}
if skip != DefaultSkip {
p.addValue("skip", strconv.Itoa(skip))
}
if s != nil {
p.addValue("subscribed", yesNo(*s))
}
var envelope struct {
TotalCount int `json:"total_count"`
Items []Member `json:"items"`
}
response, err := makeRequest(r, "GET", p)
if err != nil {
return -1, nil, err
}
err = response.parseFromJSON(&envelope)
return envelope.TotalCount, envelope.Items, err
}
// GetMemberByAddress returns a complete Member structure for a member of a mailing list,
// given only their subscription e-mail address.
func (mg *MailgunImpl) GetMemberByAddress(s, l string) (Member, error) {
r := newHTTPRequest(generateMemberApiUrl(listsEndpoint, l) + "/" + s)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
response, err := makeGetRequest(r)
if err != nil {
return Member{}, err
}
var envelope struct {
Member Member `json:"member"`
}
err = response.parseFromJSON(&envelope)
return envelope.Member, err
}
// CreateMember registers a new member of the indicated mailing list.
// If merge is set to true, then the registration may update an existing Member's settings.
// Otherwise, an error will occur if you attempt to add a member with a duplicate e-mail address.
func (mg *MailgunImpl) CreateMember(merge bool, addr string, prototype Member) error {
vs, err := json.Marshal(prototype.Vars)
if err != nil {
return err
}
r := newHTTPRequest(generateMemberApiUrl(listsEndpoint, addr))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
p := newFormDataPayload()
p.addValue("upsert", yesNo(merge))
p.addValue("address", prototype.Address)
p.addValue("name", prototype.Name)
p.addValue("vars", string(vs))
if prototype.Subscribed != nil {
p.addValue("subscribed", yesNo(*prototype.Subscribed))
}
_, err = makePostRequest(r, p)
return err
}
// UpdateMember lets you change certain details about the indicated mailing list member.
// Address, Name, Vars, and Subscribed fields may be changed.
func (mg *MailgunImpl) UpdateMember(s, l string, prototype Member) (Member, error) {
r := newHTTPRequest(generateMemberApiUrl(listsEndpoint, l) + "/" + s)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
p := newFormDataPayload()
if prototype.Address != "" {
p.addValue("address", prototype.Address)
}
if prototype.Name != "" {
p.addValue("name", prototype.Name)
}
if prototype.Vars != nil {
vs, err := json.Marshal(prototype.Vars)
if err != nil {
return Member{}, err
}
p.addValue("vars", string(vs))
}
if prototype.Subscribed != nil {
p.addValue("subscribed", yesNo(*prototype.Subscribed))
}
response, err := makePutRequest(r, p)
if err != nil {
return Member{}, err
}
var envelope struct {
Member Member `json:"member"`
}
err = response.parseFromJSON(&envelope)
return envelope.Member, err
}
// DeleteMember removes the member from the list.
func (mg *MailgunImpl) DeleteMember(member, addr string) error {
r := newHTTPRequest(generateMemberApiUrl(listsEndpoint, addr) + "/" + member)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
_, err := makeDeleteRequest(r)
return err
}
// CreateMemberList registers multiple Members and non-Member members to a single mailing list
// in a single round-trip.
// u indicates if the existing members should be updated or duplicates should be updated.
// Use All to elect not to provide a default.
// The newMembers list can take one of two JSON-encodable forms: an slice of strings, or
// a slice of Member structures.
// If a simple slice of strings is passed, each string refers to the member's e-mail address.
// Otherwise, each Member needs to have at least the Address field filled out.
// Other fields are optional, but may be set according to your needs.
func (mg *MailgunImpl) CreateMemberList(u *bool, addr string, newMembers []interface{}) error {
r := newHTTPRequest(generateMemberApiUrl(listsEndpoint, addr) + ".json")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
p := newFormDataPayload()
if u != nil {
p.addValue("upsert", yesNo(*u))
}
bs, err := json.Marshal(newMembers)
if err != nil {
return err
}
fmt.Println(string(bs))
p.addValue("members", string(bs))
_, err = makePostRequest(r, p)
return err
}