Skip to content

Commit

Permalink
Remove limit argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris committed Aug 22, 2023
1 parent 24c1d37 commit ce5a22a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 25 deletions.
9 changes: 1 addition & 8 deletions management/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,17 +541,10 @@ func (m *ClientManager) List(ctx context.Context, opts ...RequestOption) (c *Cli
}

// ListAll goes through all available pages and retrieves all the clients using offset pagination.
// A limit can be provided through a context value named "paginationLimit".
//
// See: https://auth0.com/docs/api/management/v2#!/Clients/get_clients
func (m *ClientManager) ListAll(ctx context.Context, opts ...RequestOption) (clients []*Client, err error) {
paginationLimit := 0
limit, ok := ctx.Value("paginationLimit").(int)
if ok {
paginationLimit = limit
}

clients, err = getWithPagination(ctx, paginationLimit, opts, func(ctx context.Context, opts ...RequestOption) ([]*Client, bool, error) {
clients, err = getWithPagination(ctx, opts, func(ctx context.Context, opts ...RequestOption) ([]*Client, bool, error) {
clientList, err := m.List(ctx, opts...)
if err != nil {
return nil, false, err
Expand Down
20 changes: 3 additions & 17 deletions management/management_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,27 +118,13 @@ func (m *Management) Request(ctx context.Context, method, uri string, payload in

func getWithPagination[Resource interface{}](
ctx context.Context,
limit int,
opts []RequestOption,
api func(ctx context.Context, opts ...RequestOption) (result []Resource, hasNext bool, err error),
) ([]Resource, error) {
const defaultPageSize = 100
const pageSize = 100
var list []Resource
var pageSize, pageNumber int
var pageNumber int
for {
if limit > 0 {
wanted := limit - len(list)
if wanted == 0 {
return list, nil
}

if wanted < pageSize {
pageSize = wanted
} else {
pageSize = defaultPageSize
}
}

opts = append(opts, PerPage(pageSize), Page(pageNumber))

result, hasNext, err := api(ctx, opts...)
Expand All @@ -147,7 +133,7 @@ func getWithPagination[Resource interface{}](
}

list = append(list, result...)
if len(list) == limit || !hasNext {
if !hasNext {
break
}

Expand Down

0 comments on commit ce5a22a

Please sign in to comment.