-
Notifications
You must be signed in to change notification settings - Fork 143
/
events.go
293 lines (262 loc) · 7.81 KB
/
events.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
package mailgun
import (
"context"
"fmt"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/mailgun/mailgun-go/v4/events"
)
// ListEventOptions{} modifies the behavior of ListEvents()
type ListEventOptions struct {
// Limits the results to a specific start and end time
Begin, End time.Time
// ForceAscending and ForceDescending are used to force Mailgun to use a given
// traversal order of the events. If both ForceAscending and ForceDescending are
// true, an error will result. If none, the default will be inferred from the Begin
// and End parameters.
ForceAscending, ForceDescending bool
// Compact, if true, compacts the returned JSON to minimize transmission bandwidth.
Compact bool
// Limit caps the number of results returned. If left unspecified, MailGun assumes 100.
Limit int
// Filter allows the caller to provide more specialized filters on the query.
// Consult the Mailgun documentation for more details.
Filter map[string]string
PollInterval time.Duration
}
// EventIterator maintains the state necessary for paging though small parcels of a larger set of events.
type EventIterator struct {
events.Response
mg Mailgun
err error
}
// Create an new iterator to fetch a page of events from the events api with a specific domain
func (mg *MailgunImpl) ListEventsWithDomain(opts *ListEventOptions, domain string) *EventIterator {
url := generateApiUrlWithDomain(mg, eventsEndpoint, domain)
return mg.listEvents(url, opts)
}
// Create an new iterator to fetch a page of events from the events api
func (mg *MailgunImpl) ListEvents(opts *ListEventOptions) *EventIterator {
url := generateApiUrl(mg, eventsEndpoint)
return mg.listEvents(url, opts)
}
func (mg *MailgunImpl) listEvents(url string, opts *ListEventOptions) *EventIterator {
req := newHTTPRequest(url)
if opts != nil {
if opts.Limit > 0 {
req.addParameter("limit", fmt.Sprintf("%d", opts.Limit))
}
if opts.Compact {
req.addParameter("pretty", "no")
}
if opts.ForceAscending {
req.addParameter("ascending", "yes")
} else if opts.ForceDescending {
req.addParameter("ascending", "no")
}
if !opts.Begin.IsZero() {
req.addParameter("begin", formatMailgunTime(opts.Begin))
}
if !opts.End.IsZero() {
req.addParameter("end", formatMailgunTime(opts.End))
}
if opts.Filter != nil {
for k, v := range opts.Filter {
req.addParameter(k, v)
}
}
}
url, err := req.generateUrlWithParameters()
return &EventIterator{
mg: mg,
Response: events.Response{Paging: events.Paging{Next: url, First: url}},
err: err,
}
}
// If an error occurred during iteration `Err()` will return non nil
func (ei *EventIterator) Err() error {
return ei.err
}
// Next retrieves the next page of events 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 (ei *EventIterator) Next(ctx context.Context, events *[]Event) bool {
if ei.err != nil {
return false
}
ei.err = ei.fetch(ctx, ei.Paging.Next)
if ei.err != nil {
return false
}
*events, ei.err = ParseEvents(ei.Items)
if ei.err != nil {
return false
}
if len(ei.Items) == 0 {
return false
}
return true
}
// First retrieves the first page of events 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 (ei *EventIterator) First(ctx context.Context, events *[]Event) bool {
if ei.err != nil {
return false
}
ei.err = ei.fetch(ctx, ei.Paging.First)
if ei.err != nil {
return false
}
*events, ei.err = ParseEvents(ei.Items)
return true
}
// Last retrieves the last page of events 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 (ei *EventIterator) Last(ctx context.Context, events *[]Event) bool {
if ei.err != nil {
return false
}
ei.err = ei.fetch(ctx, ei.Paging.Last)
if ei.err != nil {
return false
}
*events, ei.err = ParseEvents(ei.Items)
return true
}
// Previous retrieves the previous page of events 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 (ei *EventIterator) Previous(ctx context.Context, events *[]Event) bool {
if ei.err != nil {
return false
}
if ei.Paging.Previous == "" {
return false
}
ei.err = ei.fetch(ctx, ei.Paging.Previous)
if ei.err != nil {
return false
}
*events, ei.err = ParseEvents(ei.Items)
if len(ei.Items) == 0 {
return false
}
return true
}
func (ei *EventIterator) fetch(ctx context.Context, url string) error {
ei.Items = nil
r := newHTTPRequest(url)
r.setClient(ei.mg.Client())
r.setBasicAuth(basicAuthUser, ei.mg.APIKey())
resp, err := makeRequest(ctx, r, "GET", nil)
if err != nil {
return err
}
if err := jsoniter.Unmarshal(resp.Data, &ei.Response); err != nil {
return fmt.Errorf("failed to un-marshall event.Response: %s", err)
}
return nil
}
// EventPoller maintains the state necessary for polling events
type EventPoller struct {
it *EventIterator
opts ListEventOptions
thresholdTime time.Time
beginTime time.Time
sleepUntil time.Time
mg Mailgun
err error
}
// Poll the events api and return new events as they occur
// it = mg.PollEvents(&ListEventOptions{
// // Only events with a timestamp after this date/time will be returned
// Begin: time.Now().Add(time.Second * -3),
// // How often we poll the api for new events
// PollInterval: time.Second * 4
// })
//
// var events []Event
// ctx, cancel := context.WithCancel(context.Background())
//
// // Blocks until new events appear or context is cancelled
// for it.Poll(ctx, &events) {
// for _, event := range(events) {
// fmt.Printf("Event %+v\n", event)
// }
// }
// if it.Err() != nil {
// log.Fatal(it.Err())
// }
func (mg *MailgunImpl) PollEvents(opts *ListEventOptions) *EventPoller {
now := time.Now()
// ForceAscending must be set
opts.ForceAscending = true
// Default begin time is 30 minutes ago
if opts.Begin.IsZero() {
opts.Begin = now.Add(time.Minute * -30)
}
// Set a 15 second poll interval if none set
if opts.PollInterval.Nanoseconds() == 0 {
opts.PollInterval = time.Duration(time.Second * 15)
}
return &EventPoller{
it: mg.ListEvents(opts),
opts: *opts,
mg: mg,
}
}
// If an error occurred during polling `Err()` will return non nil
func (ep *EventPoller) Err() error {
return ep.err
}
func (ep *EventPoller) Poll(ctx context.Context, events *[]Event) bool {
var currentPage string
var results []Event
if ep.opts.Begin.IsZero() {
ep.beginTime = time.Now().UTC()
}
for {
// Remember our current page url
currentPage = ep.it.Paging.Next
// Attempt to get a page of events
var page []Event
if ep.it.Next(ctx, &page) == false {
if ep.it.Err() == nil && len(page) == 0 {
// No events, sleep for our poll interval
goto SLEEP
}
ep.err = ep.it.Err()
return false
}
for _, e := range page {
// If any events on the page are older than our being time
if e.GetTimestamp().After(ep.beginTime) {
results = append(results, e)
}
}
// If we have events to return
if len(results) != 0 {
*events = results
return true
}
SLEEP:
// Since we didn't find an event older than our
// threshold, fetch this same page again
ep.it.Paging.Next = currentPage
// Sleep the rest of our duration
timer := time.NewTimer(ep.opts.PollInterval)
select {
case <-ctx.Done():
timer.Stop()
return false
case <-timer.C:
}
}
}
// Given time.Time{} return a float64 as given in mailgun event timestamps
func TimeToFloat(t time.Time) float64 {
return float64(t.Unix()) + (float64(t.Nanosecond()/int(time.Microsecond)) / float64(1000000))
}