Repository navigation
Expand file tree
/
Copy pathiterator.go
More file actions
61 lines (58 loc) · 1.91 KB
/
Copy pathiterator.go
File metadata and controls
61 lines (58 loc) · 1.91 KB
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
package ranjit
import (
"context"
"iter"
)
func iterate[T any](ctx context.Context, startCursor string, fetch func(string) (Page[T], error)) iter.Seq2[T, error] {
return func(yield func(T, error) bool) {
cursor := startCursor
var zero T
seen := map[string]bool{cursor: true}
for {
if err := ctx.Err(); err != nil {
yield(zero, err)
return
}
page, err := fetch(cursor)
if err != nil {
yield(zero, err)
return
}
if page.NextCursor != "" && seen[page.NextCursor] {
yield(zero, ErrInvalidResponse)
return
}
for _, item := range page.Data {
if err = ctx.Err(); err != nil {
yield(zero, err)
return
}
if !yield(item, nil) {
return
}
}
if page.NextCursor == "" {
return
}
cursor = page.NextCursor
seen[cursor] = true
}
}
}
// IterConnections fetches pages lazily. Breaking the loop stops further requests.
func (c *Client) IterConnections(ctx context.Context, customer string, limit int) iter.Seq2[Connection, error] {
return iterate(ctx, "", func(cursor string) (Page[Connection], error) { return c.Connections(ctx, customer, cursor, limit) })
}
func (c *Client) IterAccounts(ctx context.Context, connectionID string, limit int) iter.Seq2[Account, error] {
return iterate(ctx, "", func(cursor string) (Page[Account], error) { return c.Accounts(ctx, connectionID, cursor, limit) })
}
func (c *Client) IterTransactions(ctx context.Context, accountID string, limit int) iter.Seq2[Transaction, error] {
return iterate(ctx, "", func(cursor string) (Page[Transaction], error) { return c.Transactions(ctx, accountID, cursor, limit) })
}
func (c *Client) IterSearchTransactions(ctx context.Context, filter TransactionFilter) iter.Seq2[Transaction, error] {
return iterate(ctx, filter.Cursor, func(cursor string) (Page[Transaction], error) {
pageFilter := filter
pageFilter.Cursor = cursor
return c.SearchTransactions(ctx, pageFilter)
})
}