-
Notifications
You must be signed in to change notification settings - Fork 145
/
unsubscribes.go
189 lines (167 loc) · 5.85 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
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
package mailgun
import (
"context"
"strconv"
)
type Unsubscribe struct {
CreatedAt RFC2822Time `json:"created_at,omitempty"`
Tags []string `json:"tags,omitempty"`
ID string `json:"id,omitempty"`
Address string `json:"address"`
}
type unsubscribesResponse struct {
Paging Paging `json:"paging"`
Items []Unsubscribe `json:"items"`
}
// Fetches the list of unsubscribes
func (mg *MailgunImpl) ListUnsubscribes(opts *ListOptions) *UnsubscribesIterator {
r := newHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint))
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 &UnsubscribesIterator{
mg: mg,
unsubscribesResponse: unsubscribesResponse{Paging: Paging{Next: url, First: url}},
err: err,
}
}
type UnsubscribesIterator struct {
unsubscribesResponse
mg Mailgun
err error
}
// If an error occurred during iteration `Err()` will return non nil
func (ci *UnsubscribesIterator) Err() error {
return ci.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 (ci *UnsubscribesIterator) Next(ctx context.Context, items *[]Unsubscribe) bool {
if ci.err != nil {
return false
}
ci.err = ci.fetch(ctx, ci.Paging.Next)
if ci.err != nil {
return false
}
cpy := make([]Unsubscribe, len(ci.Items))
copy(cpy, ci.Items)
*items = cpy
return len(ci.Items) != 0
}
// 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 (ci *UnsubscribesIterator) First(ctx context.Context, items *[]Unsubscribe) bool {
if ci.err != nil {
return false
}
ci.err = ci.fetch(ctx, ci.Paging.First)
if ci.err != nil {
return false
}
cpy := make([]Unsubscribe, len(ci.Items))
copy(cpy, ci.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 (ci *UnsubscribesIterator) Last(ctx context.Context, items *[]Unsubscribe) bool {
if ci.err != nil {
return false
}
ci.err = ci.fetch(ctx, ci.Paging.Last)
if ci.err != nil {
return false
}
cpy := make([]Unsubscribe, len(ci.Items))
copy(cpy, ci.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 (ci *UnsubscribesIterator) Previous(ctx context.Context, items *[]Unsubscribe) bool {
if ci.err != nil {
return false
}
if ci.Paging.Previous == "" {
return false
}
ci.err = ci.fetch(ctx, ci.Paging.Previous)
if ci.err != nil {
return false
}
cpy := make([]Unsubscribe, len(ci.Items))
copy(cpy, ci.Items)
*items = cpy
return len(ci.Items) != 0
}
func (ci *UnsubscribesIterator) fetch(ctx context.Context, url string) error {
ci.Items = nil
r := newHTTPRequest(url)
r.setClient(ci.mg.Client())
r.setBasicAuth(basicAuthUser, ci.mg.APIKey())
return getResponseFromJSON(ctx, r, &ci.unsubscribesResponse)
}
// Retreives a single unsubscribe record. Can be used to check if a given address is present in the list of unsubscribed users.
func (mg *MailgunImpl) GetUnsubscribe(ctx context.Context, address string) (Unsubscribe, error) {
r := newHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, address))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
envelope := Unsubscribe{}
err := getResponseFromJSON(ctx, r, &envelope)
return envelope, err
}
// Unsubscribe adds an e-mail address to the domain's unsubscription table.
func (mg *MailgunImpl) CreateUnsubscribe(ctx context.Context, address, tag string) error {
r := newHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
p.addValue("address", address)
p.addValue("tag", tag)
_, err := makePostRequest(ctx, r, p)
return err
}
// CreateUnsubscribes adds multiple e-mail addresses to the domain's unsubscription table.
func (mg *MailgunImpl) CreateUnsubscribes(ctx context.Context, unsubscribes []Unsubscribe) error {
r := newHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
r.addHeader("Content-Type", "application/json")
p := newJSONEncodedPayload(unsubscribes)
_, err := makePostRequest(ctx, r, p)
return err
}
// DeleteUnsubscribe removes the e-mail address given from the domain's unsubscription table.
// If passing in an ID (discoverable from, e.g., ListUnsubscribes()), the e-mail address associated
// with the given ID will be removed.
func (mg *MailgunImpl) DeleteUnsubscribe(ctx context.Context, address string) error {
r := newHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, address))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
}
// DeleteUnsubscribeWithTag 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., ListUnsubscribes()), the e-mail address associated
// with the given ID will be removed.
func (mg *MailgunImpl) DeleteUnsubscribeWithTag(ctx context.Context, 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(ctx, r)
return err
}