-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.go
More file actions
140 lines (129 loc) · 4.44 KB
/
Copy pathiterator.go
File metadata and controls
140 lines (129 loc) · 4.44 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
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
package streaming
// Iterator is an interface for iterating over a collection of items.
// It handles pagination automatically behind the scenes.
type Iterator[T any] struct {
index int
items []T
hasMorePages bool
nextPageCursor *string
fetcher func(string) ([]T, bool, *string, error)
err error
maxPage int
currentPage int
}
// Next returns true if there is a next item.
// It returns false when there are no more items, page limit is reached, or an error occurred.
// Call Err() to check if an error occurred.
// An iteration starts with a call to Next() and then a call to Get().
func (p *Iterator[T]) Next() bool {
if p.index < len(p.items)-1 {
p.index++
return true
}
if !p.hasMorePages || (p.maxPage > 0 && p.currentPage >= p.maxPage) {
return false
}
items, hasMorePages, nextPageCursor, err := p.fetcher(*p.nextPageCursor)
if err != nil {
p.err = err
return false
}
p.items = items
p.hasMorePages = hasMorePages
p.nextPageCursor = nextPageCursor
p.index = 0
p.currentPage++
if len(p.items) == 0 {
return false
}
return true
}
// Get returns the current item.
// Call Next() to move to the next item.
// You should not call Get() before calling Next() at least once.
func (p *Iterator[T]) Get() T {
return p.items[p.index]
}
// Err returns the error if an error occurred during iteration.
func (p *Iterator[T]) Err() error {
return p.err
}
// SearchShowsByFiltersExecuteWithAutoPagination executes the request and returns a paginator to iterate over the results page by page.
// If maxPages is 0 or less, all pages will be fetched.
func (a *ShowsAPIService) SearchShowsByFiltersExecuteWithAutoPagination(r ApiSearchShowsByFiltersRequest, maxPages int) (*Iterator[Show], error) {
res, _, err := a.SearchShowsByFiltersExecute(r)
if err != nil {
return nil, err
}
return &Iterator[Show]{
items: res.Shows,
hasMorePages: res.HasMore,
nextPageCursor: res.NextCursor,
fetcher: func(cursor string) ([]Show, bool, *string, error) {
r := r.Cursor(cursor)
res, _, err := a.SearchShowsByFiltersExecute(r)
if err != nil {
return nil, false, nil, err
}
return res.Shows, res.HasMore, res.NextCursor, nil
},
maxPage: maxPages,
currentPage: 1,
index: -1,
}, nil
}
// ExecuteWithAutoPagination executes the request and returns a paginator to iterate over the results page by page.
// If maxPages is 0 or less, all pages will be fetched.
func (r ApiSearchShowsByFiltersRequest) ExecuteWithAutoPagination(maxPages int) (*Iterator[Show], error) {
return r.ApiService.SearchShowsByFiltersExecuteWithAutoPagination(r, maxPages)
}
// ChangeWithShow represents a Change with the corresponding Show
type ChangeWithShow struct {
Change
// Show that the change is related to
Show Show
}
// GetChangesExecuteWithAutoPagination executes the request and returns a paginator to iterate over the results page by page.
// Unlike Execute, type of the returned struct is ChangeWithShow instead of Change.
// If maxPages is 0 or less, all pages will be fetched.
func (a *ChangesAPIService) GetChangesExecuteWithAutoPagination(r ApiGetChangesRequest, maxPages int) (*Iterator[ChangeWithShow], error) {
res, _, err := a.GetChangesExecute(r)
if err != nil {
return nil, err
}
return &Iterator[ChangeWithShow]{
items: getChangeWithShows(res),
hasMorePages: res.HasMore,
nextPageCursor: res.NextCursor,
fetcher: func(cursor string) ([]ChangeWithShow, bool, *string, error) {
r := r.Cursor(cursor)
res, _, err := a.GetChangesExecute(r)
if err != nil {
return nil, false, nil, err
}
return getChangeWithShows(res), res.HasMore, res.NextCursor, nil
},
maxPage: maxPages,
currentPage: 1,
index: -1,
}, nil
}
// ExecuteWithAutoPagination executes the request and returns a paginator to iterate over the results page by page.
// Unlike Execute, type of the returned struct is ChangeWithShow instead of Change.
// If maxPages is 0 or less, all pages will be fetched.
func (r ApiGetChangesRequest) ExecuteWithAutoPagination(maxPages int) (*Iterator[ChangeWithShow], error) {
return r.ApiService.GetChangesExecuteWithAutoPagination(r, maxPages)
}
func getChangeWithShows(result *ChangesResult) []ChangeWithShow {
if result == nil {
return nil
}
res := make([]ChangeWithShow, 0, len(result.Changes))
for _, change := range result.Changes {
res = append(res, ChangeWithShow{
Change: change,
Show: result.Shows[change.ShowId],
})
}
return res
}