Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 635d1c3

Browse files
Paginate user calls (#119)
* Paginate user calls `Users` should get the full list of users, but it currently only returns a single page (1000 items by default) * Start at page 1 * oops * oops again * Fix the test
1 parent 4ad8cdf commit 635d1c3

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

user.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@ type UserSearch struct {
4646

4747
// Users fetches and returns Grafana users.
4848
func (c *Client) Users() (users []UserSearch, err error) {
49-
err = c.request("GET", "/api/users", nil, nil, &users)
49+
var (
50+
page = 1
51+
newUsers []UserSearch
52+
)
53+
for len(newUsers) > 0 || page == 1 {
54+
query := url.Values{}
55+
query.Add("page", fmt.Sprintf("%d", page))
56+
if err = c.request("GET", "/api/users", query, nil, &newUsers); err != nil {
57+
return
58+
}
59+
users = append(users, newUsers...)
60+
page++
61+
}
62+
5063
return
5164
}
5265

user_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const (
1414
)
1515

1616
func TestUsers(t *testing.T) {
17-
client := gapiTestTools(t, 200, getUsersJSON)
17+
client := gapiTestToolsFromCalls(t, []mockServerCall{
18+
{200, getUsersJSON},
19+
{200, "null"},
20+
})
1821

1922
resp, err := client.Users()
2023
if err != nil {

0 commit comments

Comments
 (0)