forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pager.go
408 lines (366 loc) · 9.16 KB
/
pager.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package recurly
import (
"context"
"encoding/xml"
"errors"
"fmt"
"net/url"
"strconv"
"time"
)
// Pager paginates records.
type Pager interface {
// Count returns a total count of the request. Calling this function
// more than once will return the value from the first call.
Count(ctx context.Context) (int, error)
// Next returns true if there is a next result expected, or false if
// there is no next result.
Next() bool
// Cursor returns the next cursor (if available).
Cursor() string
// Fetch fetches results of a single page and populates dst with the results.
// For use in a for loop with Next().
Fetch(ctx context.Context, dst interface{}) error
// FetchAll fetches all of the pages recurly has available for the result
// set and populates dst with the results.
FetchAll(ctx context.Context, dst interface{}) error
}
var _ Pager = &pager{}
// pager paginates API calls.
type pager struct {
client *Client
method string
path string
opts *PagerOptions
count *int
cursor string
expectResults bool
}
// returns a new pager and initializes params if nil. It ensures no cursor
// is set.
func (c *Client) newPager(method, path string, opts *PagerOptions) *pager {
if opts == nil {
opts = &PagerOptions{}
}
return &pager{
client: c,
method: method,
path: path,
opts: opts,
cursor: opts.Cursor,
expectResults: true,
}
}
func (p *pager) Count(ctx context.Context) (int, error) {
if p.count != nil {
return *p.count, nil
}
req, err := p.client.newPagerRequest("HEAD", p.path, p.opts, nil)
if err != nil {
return 0, err
}
resp, err := p.client.do(ctx, req, nil)
if err != nil {
return 0, err
}
if count := resp.Header.Get("X-Records"); count == "" {
return 0, nil
} else if i, err := strconv.Atoi(count); err != nil {
return 0, err
} else {
p.count = &i
return i, nil
}
}
func (p *pager) Next() bool { return p.expectResults }
// fetch retrieves the results and populates dst, setting the next
// cursor.
func (p *pager) Fetch(ctx context.Context, dst interface{}) error {
select {
default:
case <-ctx.Done():
return ctx.Err()
}
if !p.expectResults {
return errors.New("no more results")
}
p.opts.Cursor = p.cursor
req, err := p.client.newPagerRequest(p.method, p.path, p.opts, nil)
if err != nil {
return err
}
var unmarshaler struct {
XMLName xml.Name
Account []Account `xml:"account"`
Adjustment []Adjustment `xml:"adjustment"`
AddOn []AddOn `xml:"add_on"`
Coupon []Coupon `xml:"coupon"`
CreditPayment []CreditPayment `xml:"credit_payment"`
ExportDate []ExportDate `xml:"export_date"`
ExportFile []ExportFile `xml:"export_file"`
Invoice []Invoice `xml:"invoice"`
Note []Note `xml:"note"`
Plan []Plan `xml:"plan"`
Redemption []Redemption `xml:"redemption"`
ShippingAddress []ShippingAddress `xml:"shipping_address"`
ShippingMethod []ShippingMethod `xml:"shipping_method"`
Subscription []Subscription `xml:"subscription"`
Transaction []Transaction `xml:"transaction"`
}
resp, err := p.client.do(ctx, req, &unmarshaler)
if err != nil {
p.expectResults = false
return err
} else if p.cursor = resp.cursor; p.cursor == "" {
p.expectResults = false
}
// note: this may be a good candidate for a code generator.
switch v := dst.(type) {
case *[]Account:
*v = unmarshaler.Account
case *[]Adjustment:
*v = unmarshaler.Adjustment
case *[]AddOn:
*v = unmarshaler.AddOn
case *[]Coupon:
*v = unmarshaler.Coupon
case *[]CreditPayment:
*v = unmarshaler.CreditPayment
case *[]ExportDate:
*v = unmarshaler.ExportDate
case *[]ExportFile:
*v = unmarshaler.ExportFile
case *[]Invoice:
*v = unmarshaler.Invoice
case *[]Note:
*v = unmarshaler.Note
case *[]Plan:
*v = unmarshaler.Plan
case *[]Redemption:
*v = unmarshaler.Redemption
case *[]ShippingAddress:
*v = unmarshaler.ShippingAddress
case *[]ShippingMethod:
*v = unmarshaler.ShippingMethod
case *[]Subscription:
*v = unmarshaler.Subscription
case *[]Transaction:
*v = unmarshaler.Transaction
default:
return fmt.Errorf("unknown type used for pagination: %T", dst)
}
return nil
}
func (p *pager) FetchAll(ctx context.Context, dst interface{}) error {
// Reduce HTTP calls needed by setting pagination to Recurly's max of 200.
p.opts.PerPage = 200
// note: this may be a good candidate for a code generator.
switch v := dst.(type) {
case *[]Account:
var all []Account
for p.Next() {
var dst []Account
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]Adjustment:
var all []Adjustment
for p.Next() {
var dst []Adjustment
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]AddOn:
var all []AddOn
for p.Next() {
var dst []AddOn
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]Coupon:
var all []Coupon
for p.Next() {
var dst []Coupon
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]CreditPayment:
var all []CreditPayment
for p.Next() {
var dst []CreditPayment
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]Invoice:
var all []Invoice
for p.Next() {
var dst []Invoice
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]Note:
var all []Note
for p.Next() {
var dst []Note
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]Plan:
var all []Plan
for p.Next() {
var dst []Plan
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]Redemption:
var all []Redemption
for p.Next() {
var dst []Redemption
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]ShippingAddress:
var all []ShippingAddress
for p.Next() {
var dst []ShippingAddress
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]ShippingMethod:
var all []ShippingMethod
for p.Next() {
var dst []ShippingMethod
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]Subscription:
var all []Subscription
for p.Next() {
var dst []Subscription
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
case *[]Transaction:
var all []Transaction
for p.Next() {
var dst []Transaction
if err := p.Fetch(ctx, &dst); err != nil {
return err
}
all = append(all, dst...)
}
*v = all
default:
return fmt.Errorf("unknown type used for pagination: %T", dst)
}
return nil
}
func (p *pager) Cursor() string { return p.cursor }
// PagerOptions are used to send pagination parameters with paginated requests.
type PagerOptions struct {
// Results per page. If not provided, Recurly defaults to 50.
PerPage int
// The field to sort by (e.g. created_at). See Recurly's documentation.
Sort string
// asc or desc
Order string
// Returns records greater than or equal to BeginTime.
BeginTime NullTime
// Returns records less than or equal to EndTime.
EndTime NullTime
State string // supported by some endpoints. Check Recurly's documenation.
Type string // supported by some endpoints. Check Recurly's documentation.
// query is for any one-off URL params used by a specific endpoint.
// Values sent as time.Time or recurly.NullTime will be automatically
// converted to a valid datetime format for Recurly.
query query
// Cursor is set internally by the library. If you are paginating
// records non-consecutively and obtained the next cursor, you can set it
// as the starting cursor here to continue where you left off.
//
// Use Pager.Cursor() to obtain the next cursor.
Cursor string
}
type query map[string]interface{}
func (q query) append(u *url.URL) {
if len(q) == 0 {
return
}
vals := u.Query()
for key, val := range q {
switch v := val.(type) {
case string:
if v != "" {
vals.Add(key, v)
}
case time.Time:
if !v.IsZero() {
vals.Add(key, v.UTC().Format(DateTimeFormat))
}
case NullTime:
if _, ok := v.Value(); ok {
vals.Add(key, v.String())
}
case bool:
vals.Add(key, fmt.Sprintf("%t", v))
case int, int64, uint, uint64:
vals.Add(key, fmt.Sprintf("%d", v))
default:
vals.Add(key, fmt.Sprintf("%v", val))
}
}
u.RawQuery = vals.Encode()
}
// append appends params to a URL.
func (p PagerOptions) append(u *url.URL) {
if p.query == nil {
p.query = map[string]interface{}{}
}
if p.PerPage > 0 {
p.query["per_page"] = p.PerPage
}
p.query["begin_time"] = p.BeginTime.String()
p.query["end_time"] = p.EndTime.String()
p.query["sort"] = p.Sort
p.query["order"] = p.Order
p.query["state"] = p.State
p.query["type"] = p.Type
p.query["cursor"] = p.Cursor
p.query.append(u)
}