mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2025-12-09 05:36:27 +08:00
28 lines
837 B
Go
28 lines
837 B
Go
package alidns
|
|
|
|
// paginateAll is a small generic paginator helper. The caller provides a
|
|
// fetch function that requests a single page (pageNumber,pageSize) and
|
|
// returns the items for that page, the total number of items available,
|
|
// and an error if any. paginateAll will iterate pages until it has
|
|
// collected all items or an error occurs.
|
|
func paginateAll[T any](fetch func(pageNumber, pageSize int) ([]T, int, error)) ([]T, error) {
|
|
const maxPageSize = 500 // Alibaba API max for many endpoints
|
|
page := 1
|
|
pageSize := maxPageSize
|
|
var out []T
|
|
|
|
for {
|
|
items, total, err := fetch(page, pageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, items...)
|
|
|
|
// If we've collected all items, or the page returned nothing, stop.
|
|
if len(out) >= total || len(items) == 0 {
|
|
break
|
|
}
|
|
page++
|
|
}
|
|
return out, nil
|
|
}
|