Skip to content

Commit

Permalink
fix: ListLimit const
Browse files Browse the repository at this point in the history
  • Loading branch information
tenstad committed Jan 15, 2025
1 parent 11bd231 commit 25feaff
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 5 additions & 4 deletions internal/sdk/cloudian/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/go-resty/resty/v2"
)

const ListLimit = 100

type Client struct {
client *resty.Client
}
Expand Down Expand Up @@ -135,12 +137,11 @@ func NewClient(baseURL string, authHeader string, opts ...func(*Client)) *Client
func (client Client) ListUsers(ctx context.Context, groupId string, offsetUserId *string) ([]User, error) {
var retVal, users []User

limit := 100
params := map[string]string{
"groupId": groupId,
"userType": "all",
"userStatus": "all",
"limit": strconv.Itoa(limit),
"limit": strconv.Itoa(ListLimit),
}
if offsetUserId != nil {
params["offset"] = *offsetUserId
Expand All @@ -157,12 +158,12 @@ func (client Client) ListUsers(ctx context.Context, groupId string, offsetUserId
retVal = append(retVal, users...)

// list users is a paginated API endpoint, so we need to check the limit and use an offset to fetch more
if len(users) > limit {
if len(users) > ListLimit {
retVal = retVal[0 : len(retVal)-1] // Remove the last element, which is the offset
// There is some ambiguity in the GET /user/list endpoint documentation, but it seems
// that UserId is the correct key for this parameter
// Fetch more results
moreUsers, err := client.ListUsers(ctx, groupId, &users[limit].UserID)
moreUsers, err := client.ListUsers(ctx, groupId, &users[ListLimit].UserID)
if err != nil {
return nil, fmt.Errorf("GET list users failed: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/sdk/cloudian/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ func TestListUsers(t *testing.T) {
}
}

end := index + 101
// return one more than limit to indicate more pages
end := index + ListLimit + 1
if end > len(expected) {
end = len(expected)
}
Expand Down

0 comments on commit 25feaff

Please sign in to comment.