forked from mailgun/mailgun-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
272 lines (240 loc) · 7.91 KB
/
routes.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
package mailgun
import (
"context"
"strconv"
)
// A Route structure contains information on a configured or to-be-configured route.
// When creating a new route, the SDK only uses a subset of the fields of this structure.
// In particular, CreatedAt and ID are meaningless in this context, and will be ignored.
// Only Priority, Description, Expression, and Actions need be provided.
type Route struct {
// The Priority field indicates how soon the route works relative to other configured routes.
// Routes of equal priority are consulted in chronological order.
Priority int `json:"priority,omitempty"`
// The Description field provides a human-readable description for the route.
// Mailgun ignores this field except to provide the description when viewing the Mailgun web control panel.
Description string `json:"description,omitempty"`
// The Expression field lets you specify a pattern to match incoming messages against.
Expression string `json:"expression,omitempty"`
// The Actions field contains strings specifying what to do
// with any message which matches the provided expression.
Actions []string `json:"actions,omitempty"`
// The CreatedAt field provides a time-stamp for when the route came into existence.
CreatedAt RFC2822Time `json:"created_at,omitempty"`
// ID field provides a unique identifier for this route.
Id string `json:"id,omitempty"`
}
type routesListResponse struct {
// is -1 if Next() or First() have not been called
TotalCount int `json:"total_count"`
Items []Route `json:"items"`
}
type createRouteResp struct {
Message string `json:"message"`
Route `json:"route"`
}
// ListRoutes allows you to iterate through a list of routes returned by the API
func (mg *MailgunImpl) ListRoutes(opts *ListOptions) *RoutesIterator {
var limit int
if opts != nil {
limit = opts.Limit
}
if limit == 0 {
limit = 100
}
return &RoutesIterator{
mg: mg,
url: generatePublicApiUrl(mg, routesEndpoint),
routesListResponse: routesListResponse{TotalCount: -1},
limit: limit,
}
}
type RoutesIterator struct {
routesListResponse
limit int
mg Mailgun
offset int
url string
err error
}
// If an error occurred during iteration `Err()` will return non nil
func (ri *RoutesIterator) Err() error {
return ri.err
}
// Offset returns the current offset of the iterator
func (ri *RoutesIterator) 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 *RoutesIterator) Next(ctx context.Context, items *[]Route) bool {
if ri.err != nil {
return false
}
ri.err = ri.fetch(ctx, ri.offset, ri.limit)
if ri.err != nil {
return false
}
cpy := make([]Route, 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 *RoutesIterator) First(ctx context.Context, items *[]Route) bool {
if ri.err != nil {
return false
}
ri.err = ri.fetch(ctx, 0, ri.limit)
if ri.err != nil {
return false
}
cpy := make([]Route, 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 *RoutesIterator) Last(ctx context.Context, items *[]Route) 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([]Route, 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 *RoutesIterator) Previous(ctx context.Context, items *[]Route) 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([]Route, len(ri.Items))
copy(cpy, ri.Items)
*items = cpy
if len(ri.Items) == 0 {
return false
}
return true
}
func (ri *RoutesIterator) fetch(ctx context.Context, skip, limit int) error {
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.routesListResponse)
}
// CreateRoute installs a new route for your domain.
// The route structure you provide serves as a template, and
// only a subset of the fields influence the operation.
// See the Route structure definition for more details.
func (mg *MailgunImpl) CreateRoute(ctx context.Context, prototype Route) (_ignored Route, err error) {
r := newHTTPRequest(generatePublicApiUrl(mg, routesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
p.addValue("priority", strconv.Itoa(prototype.Priority))
p.addValue("description", prototype.Description)
p.addValue("expression", prototype.Expression)
for _, action := range prototype.Actions {
p.addValue("action", action)
}
var resp createRouteResp
if err = postResponseFromJSON(ctx, r, p, &resp); err != nil {
return _ignored, err
}
return resp.Route, err
}
// DeleteRoute removes the specified route from your domain's configuration.
// To avoid ambiguity, Mailgun identifies the route by unique ID.
// See the Route structure definition and the Mailgun API documentation for more details.
func (mg *MailgunImpl) DeleteRoute(ctx context.Context, id string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, routesEndpoint) + "/" + id)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
}
// GetRoute retrieves the complete route definition associated with the unique route ID.
func (mg *MailgunImpl) GetRoute(ctx context.Context, id string) (Route, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, routesEndpoint) + "/" + id)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var envelope struct {
Message string `json:"message"`
*Route `json:"route"`
}
err := getResponseFromJSON(ctx, r, &envelope)
if err != nil {
return Route{}, err
}
return *envelope.Route, err
}
// UpdateRoute provides an "in-place" update of the specified route.
// Only those route fields which are non-zero or non-empty are updated.
// All other fields remain as-is.
func (mg *MailgunImpl) UpdateRoute(ctx context.Context, id string, route Route) (Route, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, routesEndpoint) + "/" + id)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
if route.Priority != 0 {
p.addValue("priority", strconv.Itoa(route.Priority))
}
if route.Description != "" {
p.addValue("description", route.Description)
}
if route.Expression != "" {
p.addValue("expression", route.Expression)
}
if route.Actions != nil {
for _, action := range route.Actions {
p.addValue("action", action)
}
}
// For some reason, this API function just returns a bare Route on success.
// Unsure why this is the case; it seems like it ought to be a bug.
var envelope Route
err := putResponseFromJSON(ctx, r, p, &envelope)
return envelope, err
}