forked from glycerine/sshego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsubscribes.go
81 lines (74 loc) · 2.84 KB
/
unsubscribes.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
package mailgun
import (
"strconv"
)
type Unsubscription struct {
CreatedAt string `json:"created_at"`
Tag string `json:"tag"`
ID string `json:"id"`
Address string `json:"address"`
}
// GetUnsubscribes retrieves a list of unsubscriptions issued by recipients of mail from your domain.
// Zero is a valid list length.
func (mg *MailgunImpl) GetUnsubscribes(limit, skip int) (int, []Unsubscription, error) {
r := newHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint))
if limit != DefaultLimit {
r.addParameter("limit", strconv.Itoa(limit))
}
if skip != DefaultSkip {
r.addParameter("skip", strconv.Itoa(skip))
}
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
var envelope struct {
TotalCount int `json:"total_count"`
Items []Unsubscription `json:"items"`
}
err := getResponseFromJSON(r, &envelope)
return envelope.TotalCount, envelope.Items, err
}
// GetUnsubscribesByAddress retrieves a list of unsubscriptions by recipient address.
// Zero is a valid list length.
func (mg *MailgunImpl) GetUnsubscribesByAddress(a string) (int, []Unsubscription, error) {
r := newHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, a))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
var envelope struct {
TotalCount int `json:"total_count"`
Items []Unsubscription `json:"items"`
}
err := getResponseFromJSON(r, &envelope)
return envelope.TotalCount, envelope.Items, err
}
// Unsubscribe adds an e-mail address to the domain's unsubscription table.
func (mg *MailgunImpl) Unsubscribe(a, t string) error {
r := newHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
p := newUrlEncodedPayload()
p.addValue("address", a)
p.addValue("tag", t)
_, err := makePostRequest(r, p)
return err
}
// RemoveUnsubscribe removes the e-mail address given from the domain's unsubscription table.
// If passing in an ID (discoverable from, e.g., GetUnsubscribes()), the e-mail address associated
// with the given ID will be removed.
func (mg *MailgunImpl) RemoveUnsubscribe(a string) error {
r := newHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, a))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
_, err := makeDeleteRequest(r)
return err
}
// RemoveUnsubscribe removes the e-mail address given from the domain's unsubscription table with a matching tag.
// If passing in an ID (discoverable from, e.g., GetUnsubscribes()), the e-mail address associated
// with the given ID will be removed.
func (mg *MailgunImpl) RemoveUnsubscribeWithTag(a, t string) error {
r := newHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, a))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
r.addParameter("tag", t)
_, err := makeDeleteRequest(r)
return err
}