From 0263574b9264789a8997ae406fa703a3274c945a Mon Sep 17 00:00:00 2001 From: Ilia Medvedev Date: Mon, 27 May 2024 08:43:47 +0300 Subject: [PATCH 1/2] add pagination to all users query - without pagination query doesn't return it time --- codefresh/cfclient/user.go | 48 +++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/codefresh/cfclient/user.go b/codefresh/cfclient/user.go index 0a3c593..3e1cba7 100644 --- a/codefresh/cfclient/user.go +++ b/codefresh/cfclient/user.go @@ -2,7 +2,7 @@ package cfclient import ( "fmt" - "log" + //"log" "strings" ) @@ -222,25 +222,41 @@ func (client *Client) DeleteUserAsAccountAdmin(accountId, userId string) error { func (client *Client) GetAllUsers() (*[]User, error) { - opts := RequestOptions{ - Path: "/admin/user", - Method: "GET", - } + limitPerQuery := 100 + bIsDone := false + nPageIndex := 1 - resp, err := client.RequestAPI(&opts) - if err != nil { - return nil, err - } + var allUsers []User - var users []User - respStr := string(resp) - log.Printf("[INFO] GetAllUsers resp: %s", respStr) - err = DecodeResponseInto(resp, &users) - if err != nil { - return nil, err + for !bIsDone { + var userPaginatedResp struct{Docs []User `json:"docs"`} + + opts := RequestOptions{ + Path: fmt.Sprintf("/admin/user?limit=%d&page=%d", limitPerQuery, nPageIndex), + Method: "GET", + } + + resp, err := client.RequestAPI(&opts) + + if err != nil { + return nil, err + } + + err = DecodeResponseInto(resp, &userPaginatedResp) + + if err != nil { + return nil, err + } + + if len(userPaginatedResp.Docs) > 0 { + allUsers = append(allUsers,userPaginatedResp.Docs...) + nPageIndex++ + } else { + bIsDone = true + } } - return &users, nil + return &allUsers, nil } func (client *Client) GetUserByID(userId string) (*User, error) { From 2301b6919fa77b9f68790b497ed8ca98f27dd1f8 Mon Sep 17 00:00:00 2001 From: Ilia Medvedev Date: Mon, 27 May 2024 09:31:18 +0300 Subject: [PATCH 2/2] cleanup --- codefresh/cfclient/user.go | 1 - 1 file changed, 1 deletion(-) diff --git a/codefresh/cfclient/user.go b/codefresh/cfclient/user.go index 3e1cba7..f4b4d36 100644 --- a/codefresh/cfclient/user.go +++ b/codefresh/cfclient/user.go @@ -2,7 +2,6 @@ package cfclient import ( "fmt" - //"log" "strings" )