-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathsubaccounts.go
249 lines (212 loc) · 6.64 KB
/
subaccounts.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
package mailgun
import (
"context"
"fmt"
"strconv"
)
type ListSubaccountsOptions struct {
Limit int
Skip int
SortArray string
Enabled bool
}
type SubaccountsIterator struct {
subaccountsListResponse
mg Mailgun
limit int
offset int
skip int
sortArray string
enabled bool
url string
err error
}
// A Subaccount structure holds information about a subaccount.
type Subaccount struct {
Id string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
}
type SubaccountResponse struct {
Item Subaccount `json:"subaccount"`
}
type subaccountsListResponse struct {
Items []Subaccount `json:"subaccounts"`
Total int `json:"total"`
}
// ListSubaccounts retrieves a set of subaccount linked to the primary Mailgun account.
func (mg *MailgunImpl) ListSubaccounts(opts *ListSubaccountsOptions) *SubaccountsIterator {
r := newHTTPRequest(generateSubaccountsApiUrl(mg))
r.setClient(mg.client)
r.setBasicAuth(basicAuthUser, mg.APIKey())
var limit, skip int
var sortArray string
var enabled bool
if opts != nil {
limit = opts.Limit
skip = opts.Skip
sortArray = opts.SortArray
enabled = opts.Enabled
}
if limit == 0 {
limit = 10
}
return &SubaccountsIterator{
mg: mg,
url: generateSubaccountsApiUrl(mg),
subaccountsListResponse: subaccountsListResponse{Total: -1},
limit: limit,
skip: skip,
sortArray: sortArray,
enabled: enabled,
}
}
// If an error occurred during iteration `Err()` will return non nil
func (ri *SubaccountsIterator) Err() error {
return ri.err
}
// Offset returns the current offset of the iterator
func (ri *SubaccountsIterator) 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 *SubaccountsIterator) Next(ctx context.Context, items *[]Subaccount) bool {
if ri.err != nil {
return false
}
ri.err = ri.fetch(ctx, ri.offset, ri.limit)
if ri.err != nil {
return false
}
cpy := make([]Subaccount, len(ri.Items))
copy(cpy, ri.Items)
*items = cpy
if len(ri.Items) == 0 {
return false
}
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 *SubaccountsIterator) First(ctx context.Context, items *[]Subaccount) bool {
if ri.err != nil {
return false
}
ri.err = ri.fetch(ctx, 0, ri.limit)
if ri.err != nil {
return false
}
cpy := make([]Subaccount, 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 *SubaccountsIterator) Last(ctx context.Context, items *[]Subaccount) bool {
if ri.err != nil {
return false
}
if ri.Total == -1 {
return false
}
ri.offset = ri.Total - 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([]Subaccount, 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 *SubaccountsIterator) Previous(ctx context.Context, items *[]Subaccount) bool {
if ri.err != nil {
return false
}
if ri.Total == -1 {
return false
}
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([]Subaccount, len(ri.Items))
copy(cpy, ri.Items)
*items = cpy
return len(ri.Items) != 0
}
func (ri *SubaccountsIterator) 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.subaccountsListResponse)
}
// CreateSubaccount instructs Mailgun to create a new account (Subaccount) that is linked to the primary account.
// Subaccounts are child accounts that share the same plan and usage allocations as the primary, but have their own
// assets (sending domains, unique users, API key, SMTP credentials, settings, statistics and site login).
// All you need is the name of the subaccount.
func (mg *MailgunImpl) CreateSubaccount(ctx context.Context, subaccountName string) (SubaccountResponse, error) {
r := newHTTPRequest(generateSubaccountsApiUrl(mg))
r.setClient(mg.client)
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
payload.addValue("name", subaccountName)
resp := SubaccountResponse{}
err := postResponseFromJSON(ctx, r, payload, &resp)
return resp, err
}
// SubaccountDetails retrieves detailed information about subaccount using subaccountId.
func (mg *MailgunImpl) SubaccountDetails(ctx context.Context, subaccountId string) (SubaccountResponse, error) {
r := newHTTPRequest(generateSubaccountsApiUrl(mg) + "/" + subaccountId)
r.setClient(mg.client)
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp SubaccountResponse
err := getResponseFromJSON(ctx, r, &resp)
return resp, err
}
// EnableSubaccount instructs Mailgun to enable subaccount.
func (mg *MailgunImpl) EnableSubaccount(ctx context.Context, subaccountId string) (SubaccountResponse, error) {
r := newHTTPRequest(generateSubaccountsApiUrl(mg) + "/" + subaccountId + "/" + "enable")
r.setClient(mg.client)
r.setBasicAuth(basicAuthUser, mg.APIKey())
resp := SubaccountResponse{}
err := postResponseFromJSON(ctx, r, nil, &resp)
return resp, err
}
// DisableSubaccount instructs Mailgun to disable subaccount.
func (mg *MailgunImpl) DisableSubaccount(ctx context.Context, subaccountId string) (SubaccountResponse, error) {
r := newHTTPRequest(generateSubaccountsApiUrl(mg) + "/" + subaccountId + "/" + "disable")
r.setClient(mg.client)
r.setBasicAuth(basicAuthUser, mg.APIKey())
resp := SubaccountResponse{}
err := postResponseFromJSON(ctx, r, nil, &resp)
return resp, err
}
func generateSubaccountsApiUrl(m Mailgun) string {
return fmt.Sprintf("%s/%s/%s", m.APIBase(), accountsEndpoint, subaccountsEndpoint)
}