Skip to content

Commit 0263574

Browse files
add pagination to all users query - without pagination query doesn't return it time
1 parent 55e3ef2 commit 0263574

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

codefresh/cfclient/user.go

+32-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cfclient
22

33
import (
44
"fmt"
5-
"log"
5+
//"log"
66
"strings"
77
)
88

@@ -222,25 +222,41 @@ func (client *Client) DeleteUserAsAccountAdmin(accountId, userId string) error {
222222

223223
func (client *Client) GetAllUsers() (*[]User, error) {
224224

225-
opts := RequestOptions{
226-
Path: "/admin/user",
227-
Method: "GET",
228-
}
225+
limitPerQuery := 100
226+
bIsDone := false
227+
nPageIndex := 1
229228

230-
resp, err := client.RequestAPI(&opts)
231-
if err != nil {
232-
return nil, err
233-
}
229+
var allUsers []User
234230

235-
var users []User
236-
respStr := string(resp)
237-
log.Printf("[INFO] GetAllUsers resp: %s", respStr)
238-
err = DecodeResponseInto(resp, &users)
239-
if err != nil {
240-
return nil, err
231+
for !bIsDone {
232+
var userPaginatedResp struct{Docs []User `json:"docs"`}
233+
234+
opts := RequestOptions{
235+
Path: fmt.Sprintf("/admin/user?limit=%d&page=%d", limitPerQuery, nPageIndex),
236+
Method: "GET",
237+
}
238+
239+
resp, err := client.RequestAPI(&opts)
240+
241+
if err != nil {
242+
return nil, err
243+
}
244+
245+
err = DecodeResponseInto(resp, &userPaginatedResp)
246+
247+
if err != nil {
248+
return nil, err
249+
}
250+
251+
if len(userPaginatedResp.Docs) > 0 {
252+
allUsers = append(allUsers,userPaginatedResp.Docs...)
253+
nPageIndex++
254+
} else {
255+
bIsDone = true
256+
}
241257
}
242258

243-
return &users, nil
259+
return &allUsers, nil
244260
}
245261

246262
func (client *Client) GetUserByID(userId string) (*User, error) {

0 commit comments

Comments
 (0)