Skip to content

Commit

Permalink
Add POC for a proposed ListAll func for paginated resources
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Aug 10, 2023
1 parent 1f5853c commit 51a69a9
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions management/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,68 @@ func (m *ClientManager) List(ctx context.Context, opts ...RequestOption) (c *Cli
return
}

// ListAll retrieves all the clients using offset pagination.
// If limit is 0 it will retrieve all the clients, otherwise it will retrieve all the clients until the limit is reached.
//
// See: https://auth0.com/docs/api/management/v2#!/Clients/get_clients
func (m *ClientManager) ListAll(ctx context.Context, limit int, opts ...RequestOption) (clients []*Client, err error) {
clients, err = getWithPagination(ctx, limit, opts, func(ctx context.Context, opts ...RequestOption) ([]*Client, bool, error) {
clientList, err := m.List(ctx, opts...)
if err != nil {
return nil, false, err
}

return clientList.Clients, clientList.HasNext(), nil
})

return
}

type paginatedResource interface {
*Client | *Connection | *Organization
}

func getWithPagination[Resource paginatedResource](
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
var list []Resource
var pageSize, 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...)
if err != nil {
return list, err
}

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

pageNumber++
}

return list, nil
}

// Update a client.
//
// See: https://auth0.com/docs/api/management/v2#!/Clients/patch_clients_by_id
Expand Down

0 comments on commit 51a69a9

Please sign in to comment.