-
Notifications
You must be signed in to change notification settings - Fork 952
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add AllPages pagination helper
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package gitlab | ||
|
||
// AllPages can be used to fetch all pages of a paginated resource, e.g.: (assuming gl is a gitlab client instance) | ||
// | ||
// allUsers, err := gitlab.AllPages(gl.Users.List, nil) | ||
// | ||
// It is also possible to specify additional pagination parameters: | ||
// | ||
// mrs, err := gitlab.AllPages(gl.MergeRequests.ListMergeRequests, &gitlab.ListMergeRequestsOptions{ | ||
// ListOptions: gitlab.ListOptions{ | ||
// PerPage: 100, | ||
// Pagination: "keyset", | ||
// OrderBy: "created_at", | ||
// }, | ||
// }) | ||
func AllPages[O, T any](f Paginatable[O, T], opt *O) ([]*T, error) { | ||
all := make([]*T, 0) | ||
nextLink := "" | ||
for { | ||
page, resp, err := f(opt, WithKeysetPaginationParameters(nextLink)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
all = append(all, page...) | ||
if resp.NextLink == "" { | ||
break | ||
} | ||
nextLink = resp.NextLink | ||
} | ||
return all, nil | ||
} | ||
|
||
// Paginatable is the type that is implemented by all functions used to paginated content. | ||
type Paginatable[O, T any] func(*O, ...RequestOptionFunc) ([]*T, *Response, error) |