forked from digitalocean/doctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagination.go
139 lines (113 loc) · 2.82 KB
/
pagination.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
/*
Copyright 2018 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package do
import (
"fmt"
"net/url"
"strconv"
"sync"
"github.com/digitalocean/godo"
)
const maxFetchPages = 5
var perPage = 200
var fetchFn = fetchPage
type paginatedList struct {
list [][]any
total int
mu sync.Mutex
}
func (pl *paginatedList) set(page int, items []any) {
pl.mu.Lock()
defer pl.mu.Unlock()
pl.total += len(items)
pl.list[page-1] = items
}
// Generator is a function that generates the list to be paginated.
type Generator func(*godo.ListOptions) ([]any, *godo.Response, error)
// PaginateResp paginates a Response.
func PaginateResp(gen Generator) ([]any, error) {
opt := &godo.ListOptions{Page: 1, PerPage: perPage}
// fetch first page to get page count (x)
firstPage, resp, err := gen(opt)
if err != nil {
return nil, err
}
// find last page
lp, err := lastPage(resp)
if err != nil {
return nil, err
}
l := paginatedList{
list: make([][]any, lp),
}
// set results from the first page
l.set(1, firstPage)
fetchChan := make(chan int, maxFetchPages)
var wg sync.WaitGroup
for i := 0; i < maxFetchPages-1; i++ {
wg.Add(1)
go func() {
for page := range fetchChan {
items, err := fetchFn(gen, page)
if err == nil {
l.set(page, items)
}
}
wg.Done()
}()
}
// start with second page
opt.Page++
for ; opt.Page <= lp; opt.Page++ {
fetchChan <- opt.Page
}
close(fetchChan)
wg.Wait()
// flatten paginated list
items := make([]any, l.total)[:0]
for _, page := range l.list {
if page == nil {
// must have been an error getting page results
continue
}
for _, item := range page {
items = append(items, item)
}
}
return items, nil
}
func fetchPage(gen Generator, page int) ([]any, error) {
opt := &godo.ListOptions{Page: page, PerPage: perPage}
items, _, err := gen(opt)
return items, err
}
func lastPage(resp *godo.Response) (int, error) {
if resp.Links == nil || resp.Links.Pages == nil {
// no other pages
return 1, nil
}
uStr := resp.Links.Pages.Last
if uStr == "" {
return 1, nil
}
u, err := url.Parse(uStr)
if err != nil {
return 0, fmt.Errorf("could not parse last page: %v", err)
}
pageStr := u.Query().Get("page")
page, err := strconv.Atoi(pageStr)
if err != nil {
return 0, fmt.Errorf("could not find page param: %v", err)
}
return page, err
}