-
Notifications
You must be signed in to change notification settings - Fork 143
/
domains.go
453 lines (389 loc) · 13.5 KB
/
domains.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package mailgun
import (
"context"
"strconv"
"strings"
)
// Use these to specify a spam action when creating a new domain.
const (
// Tag the received message with headers providing a measure of its spamness.
SpamActionTag = SpamAction("tag")
// Prevents Mailgun from taking any action on what it perceives to be spam.
SpamActionDisabled = SpamAction("disabled")
// instructs Mailgun to just block or delete the message all-together.
SpamActionDelete = SpamAction("delete")
)
type SpamAction string
// A Domain structure holds information about a domain used when sending mail.
type Domain struct {
CreatedAt RFC2822Time `json:"created_at"`
SMTPLogin string `json:"smtp_login"`
Name string `json:"name"`
SMTPPassword string `json:"smtp_password"`
Wildcard bool `json:"wildcard"`
SpamAction SpamAction `json:"spam_action"`
State string `json:"state"`
WebScheme string `json:"web_scheme"`
}
// DNSRecord structures describe intended records to properly configure your domain for use with Mailgun.
// Note that Mailgun does not host DNS records.
type DNSRecord struct {
Priority string
RecordType string `json:"record_type"`
Valid string
Name string
Value string
}
type DomainResponse struct {
Domain Domain `json:"domain"`
ReceivingDNSRecords []DNSRecord `json:"receiving_dns_records"`
SendingDNSRecords []DNSRecord `json:"sending_dns_records"`
}
type domainConnectionResponse struct {
Connection DomainConnection `json:"connection"`
}
type domainsListResponse struct {
// is -1 if Next() or First() have not been called
TotalCount int `json:"total_count"`
Items []Domain `json:"items"`
}
// Specify the domain connection options
type DomainConnection struct {
RequireTLS bool `json:"require_tls"`
SkipVerification bool `json:"skip_verification"`
}
// Specify the domain tracking options
type DomainTracking struct {
Click TrackingStatus `json:"click"`
Open TrackingStatus `json:"open"`
Unsubscribe TrackingStatus `json:"unsubscribe"`
}
// The tracking status of a domain
type TrackingStatus struct {
Active bool `json:"active"`
HTMLFooter string `json:"html_footer"`
TextFooter string `json:"text_footer"`
}
type domainTrackingResponse struct {
Tracking DomainTracking `json:"tracking"`
}
// ListDomains retrieves a set of domains from Mailgun.
func (mg *MailgunImpl) ListDomains(opts *ListOptions) *DomainsIterator {
var limit int
if opts != nil {
limit = opts.Limit
}
if limit == 0 {
limit = 100
}
return &DomainsIterator{
mg: mg,
url: generatePublicApiUrl(mg, domainsEndpoint),
domainsListResponse: domainsListResponse{TotalCount: -1},
limit: limit,
}
}
type DomainsIterator struct {
domainsListResponse
limit int
mg Mailgun
offset int
url string
err error
}
// If an error occurred during iteration `Err()` will return non nil
func (ri *DomainsIterator) Err() error {
return ri.err
}
// Offset returns the current offset of the iterator
func (ri *DomainsIterator) Offset() int {
return ri.offset
}
// 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 (ri *DomainsIterator) Next(ctx context.Context, items *[]Domain) bool {
if ri.err != nil {
return false
}
ri.err = ri.fetch(ctx, ri.offset, ri.limit)
if ri.err != nil {
return false
}
cpy := make([]Domain, len(ri.Items))
copy(cpy, ri.Items)
*items = cpy
if len(ri.Items) == 0 {
return false
}
ri.offset = ri.offset + len(ri.Items)
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 (ri *DomainsIterator) First(ctx context.Context, items *[]Domain) bool {
if ri.err != nil {
return false
}
ri.err = ri.fetch(ctx, 0, ri.limit)
if ri.err != nil {
return false
}
cpy := make([]Domain, len(ri.Items))
copy(cpy, ri.Items)
*items = cpy
ri.offset = len(ri.Items)
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 (ri *DomainsIterator) Last(ctx context.Context, items *[]Domain) bool {
if ri.err != nil {
return false
}
if ri.TotalCount == -1 {
return false
}
ri.offset = ri.TotalCount - ri.limit
if ri.offset < 0 {
ri.offset = 0
}
ri.err = ri.fetch(ctx, ri.offset, ri.limit)
if ri.err != nil {
return false
}
cpy := make([]Domain, len(ri.Items))
copy(cpy, ri.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 (ri *DomainsIterator) Previous(ctx context.Context, items *[]Domain) bool {
if ri.err != nil {
return false
}
if ri.TotalCount == -1 {
return false
}
ri.offset = ri.offset - (ri.limit * 2)
if ri.offset < 0 {
ri.offset = 0
}
ri.err = ri.fetch(ctx, ri.offset, ri.limit)
if ri.err != nil {
return false
}
cpy := make([]Domain, len(ri.Items))
copy(cpy, ri.Items)
*items = cpy
if len(ri.Items) == 0 {
return false
}
return true
}
func (ri *DomainsIterator) fetch(ctx context.Context, skip, limit int) error {
ri.Items = nil
r := newHTTPRequest(ri.url)
r.setBasicAuth(basicAuthUser, ri.mg.APIKey())
r.setClient(ri.mg.Client())
if skip != 0 {
r.addParameter("skip", strconv.Itoa(skip))
}
if limit != 0 {
r.addParameter("limit", strconv.Itoa(limit))
}
return getResponseFromJSON(ctx, r, &ri.domainsListResponse)
}
// GetDomain retrieves detailed information about the named domain.
func (mg *MailgunImpl) GetDomain(ctx context.Context, domain string) (DomainResponse, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp DomainResponse
err := getResponseFromJSON(ctx, r, &resp)
return resp, err
}
func (mg *MailgunImpl) VerifyDomain(ctx context.Context, domain string) (string, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/verify")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
var resp DomainResponse
err := putResponseFromJSON(ctx, r, payload, &resp)
return resp.Domain.State, err
}
// VerifyAndReturnDomain verifies & retrieves detailed information about the named domain.
func (mg *MailgunImpl) VerifyAndReturnDomain(ctx context.Context, domain string) (DomainResponse, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/verify")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
var resp DomainResponse
err := putResponseFromJSON(ctx, r, payload, &resp)
return resp, err
}
// Optional parameters when creating a domain
type CreateDomainOptions struct {
Password string
SpamAction SpamAction
Wildcard bool
ForceDKIMAuthority bool
DKIMKeySize int
IPS []string
WebScheme string
}
// CreateDomain instructs Mailgun to create a new domain for your account.
// The name parameter identifies the domain.
// The smtpPassword parameter provides an access credential for the domain.
// The spamAction domain must be one of Delete, Tag, or Disabled.
// The wildcard parameter instructs Mailgun to treat all subdomains of this domain uniformly if true,
// and as different domains if false.
func (mg *MailgunImpl) CreateDomain(ctx context.Context, name string, opts *CreateDomainOptions) (DomainResponse, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
payload.addValue("name", name)
if opts != nil {
if opts.SpamAction != "" {
payload.addValue("spam_action", string(opts.SpamAction))
}
if opts.Wildcard {
payload.addValue("wildcard", boolToString(opts.Wildcard))
}
if opts.ForceDKIMAuthority {
payload.addValue("force_dkim_authority", boolToString(opts.ForceDKIMAuthority))
}
if opts.DKIMKeySize != 0 {
payload.addValue("dkim_key_size", strconv.Itoa(opts.DKIMKeySize))
}
if len(opts.IPS) != 0 {
payload.addValue("ips", strings.Join(opts.IPS, ","))
}
if len(opts.Password) != 0 {
payload.addValue("smtp_password", opts.Password)
}
if opts.WebScheme != "" {
payload.addValue("web_scheme", string(opts.WebScheme))
}
}
var resp DomainResponse
err := postResponseFromJSON(ctx, r, payload, &resp)
return resp, err
}
// GetDomainConnection returns delivery connection settings for the defined domain
func (mg *MailgunImpl) GetDomainConnection(ctx context.Context, domain string) (DomainConnection, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/connection")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp domainConnectionResponse
err := getResponseFromJSON(ctx, r, &resp)
return resp.Connection, err
}
// Updates the specified delivery connection settings for the defined domain
func (mg *MailgunImpl) UpdateDomainConnection(ctx context.Context, domain string, settings DomainConnection) error {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/connection")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
payload.addValue("require_tls", boolToString(settings.RequireTLS))
payload.addValue("skip_verification", boolToString(settings.SkipVerification))
_, err := makePutRequest(ctx, r, payload)
return err
}
// DeleteDomain instructs Mailgun to dispose of the named domain name
func (mg *MailgunImpl) DeleteDomain(ctx context.Context, name string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
}
// GetDomainTracking returns tracking settings for a domain
func (mg *MailgunImpl) GetDomainTracking(ctx context.Context, domain string) (DomainTracking, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/tracking")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp domainTrackingResponse
err := getResponseFromJSON(ctx, r, &resp)
return resp.Tracking, err
}
func (mg *MailgunImpl) UpdateClickTracking(ctx context.Context, domain, active string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/tracking/click")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
payload.addValue("active", active)
_, err := makePutRequest(ctx, r, payload)
return err
}
func (mg *MailgunImpl) UpdateUnsubscribeTracking(ctx context.Context, domain, active, htmlFooter, textFooter string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/tracking/unsubscribe")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
payload.addValue("active", active)
payload.addValue("html_footer", htmlFooter)
payload.addValue("text_footer", textFooter)
_, err := makePutRequest(ctx, r, payload)
return err
}
func (mg *MailgunImpl) UpdateOpenTracking(ctx context.Context, domain, active string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/tracking/open")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
payload.addValue("active", active)
_, err := makePutRequest(ctx, r, payload)
return err
}
// Update the DKIM selector for a domain
func (mg *MailgunImpl) UpdateDomainDkimSelector(ctx context.Context, domain, dkimSelector string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/dkim_selector")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
payload.addValue("dkim_selector", dkimSelector)
_, err := makePutRequest(ctx, r, payload)
return err
}
// Update the CNAME used for tracking opens and clicks
func (mg *MailgunImpl) UpdateDomainTrackingWebPrefix(ctx context.Context, domain, webPrefix string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + domain + "/web_prefix")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
payload.addValue("web_prefix", webPrefix)
_, err := makePutRequest(ctx, r, payload)
return err
}
// UpdateDomainOptions options for updating a domain
type UpdateDomainOptions struct {
WebScheme string
}
// UpdateDomain updates a domain's attributes.
// Currently only the web_scheme update is supported, spam_action and wildcard are to be added.
func (mg *MailgunImpl) UpdateDomain(ctx context.Context, name string, opts *UpdateDomainOptions) error {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
if opts != nil {
if opts.WebScheme != "" {
payload.addValue("web_scheme", string(opts.WebScheme))
}
}
_, err := makePutRequest(ctx, r, payload)
return err
}
func boolToString(b bool) string {
if b {
return "true"
}
return "false"
}