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

Commit 303710b

Browse files
authored
Fixed folder pagination (#134)
1 parent 5c6d8c2 commit 303710b

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

folder.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"net/url"
78
)
89

910
// Folder represents a Grafana folder.
@@ -22,13 +23,28 @@ type FolderPayload struct {
2223

2324
// Folders fetches and returns Grafana folders.
2425
func (c *Client) Folders() ([]Folder, error) {
25-
folders := make([]Folder, 0)
26-
err := c.request("GET", "/api/folders/", nil, nil, &folders)
27-
if err != nil {
28-
return folders, err
26+
const limit = 1000
27+
var (
28+
page = 0
29+
newFolders []Folder
30+
folders []Folder
31+
query = make(url.Values)
32+
)
33+
query.Set("limit", fmt.Sprint(limit))
34+
for {
35+
page++
36+
query.Set("page", fmt.Sprint(page))
37+
38+
if err := c.request("GET", "/api/folders/", query, nil, &newFolders); err != nil {
39+
return nil, err
40+
}
41+
42+
folders = append(folders, newFolders...)
43+
44+
if len(newFolders) < limit {
45+
return folders, nil
46+
}
2947
}
30-
31-
return folders, err
3248
}
3349

3450
// Folder fetches and returns the Grafana folder whose ID it's passed.

folder_test.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package gapi
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/gobs/pretty"
78
)
89

910
const (
10-
getFoldersJSON = `
11-
[
12-
{
11+
getFoldersJSON = `{
1312
"id":1,
1413
"uid": "nErXDvCkzz",
1514
"title": "Departmenet ABC",
@@ -23,9 +22,7 @@ const (
2322
"updatedBy": "admin",
2423
"updated": "2018-01-31T17:43:12+01:00",
2524
"version": 1
26-
}
27-
]
28-
`
25+
}`
2926
getFolderJSON = `
3027
{
3128
"id":1,
@@ -85,7 +82,17 @@ const (
8582
)
8683

8784
func TestFolders(t *testing.T) {
88-
client := gapiTestTools(t, 200, getFoldersJSON)
85+
mockData := strings.Repeat(getFoldersJSON+",", 1000) // make 1000 folders.
86+
mockData = "[" + mockData[:len(mockData)-1] + "]" // remove trailing comma; make a json list.
87+
88+
// This creates 1000 + 1000 + 1 (2001, 3 calls) worth of folders.
89+
client := gapiTestToolsFromCalls(t, []mockServerCall{
90+
{200, mockData},
91+
{200, mockData},
92+
{200, "[" + getFolderJSON + "]"},
93+
})
94+
95+
const dashCount = 2001
8996

9097
folders, err := client.Folders()
9198
if err != nil {
@@ -94,12 +101,15 @@ func TestFolders(t *testing.T) {
94101

95102
t.Log(pretty.PrettyFormat(folders))
96103

97-
if len(folders) != 1 {
98-
t.Error("Length of returned folders should be 1")
104+
if len(folders) != dashCount {
105+
t.Errorf("Length of returned folders should be %d", dashCount)
99106
}
100107
if folders[0].ID != 1 || folders[0].Title != "Departmenet ABC" {
101108
t.Error("Not correctly parsing returned folders.")
102109
}
110+
if folders[dashCount-1].ID != 1 || folders[dashCount-1].Title != "Departmenet ABC" {
111+
t.Error("Not correctly parsing returned folders.")
112+
}
103113
}
104114

105115
func TestFolder(t *testing.T) {

0 commit comments

Comments
 (0)