Skip to content

Commit 9e2a852

Browse files
authored
Merge pull request #69 from jdolitsky/searchwithnext
Add SearchWithNext method to allow paging
2 parents 2facb0a + 48bdb8a commit 9e2a852

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

examples/search/search.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,20 @@ func main() {
2929
fmt.Printf("%+v\n", v)
3030
}
3131

32+
// search example with paging using SearchWithNext and Links.Next
33+
next := ""
34+
for {
35+
resp, err := api.SearchWithNext(query, next)
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
for _, v := range result.Results {
40+
fmt.Printf("%+v\n", v)
41+
}
42+
next = resp.Links.Next
43+
if next == "" {
44+
break
45+
}
46+
log.Printf("Using next page: %s", next)
47+
}
3248
}

request_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ func confluenceRestAPIStub() *httptest.Server {
343343
resp = User{}
344344
case "/wiki/rest/api/search":
345345
resp = Search{}
346+
case "/wiki/rest/api/search?next=true&cursor=abc123":
347+
resp = Search{}
346348
case "/wiki/rest/api/content/42/history":
347349
resp = History{}
348350
case "/wiki/rest/api/content/42/label":

search.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@ import (
88

99
// Search results
1010
type Search struct {
11-
Results []Results `json:"results"`
12-
Start int `json:"start,omitempty"`
13-
Limit int `json:"limit,omitempty"`
14-
Size int `json:"size,omitempty"`
15-
TotalSize int `json:"totalSize,omitempty"`
11+
Results []Results `json:"results"`
12+
Start int `json:"start,omitempty"`
13+
Limit int `json:"limit,omitempty"`
14+
Size int `json:"size,omitempty"`
15+
TotalSize int `json:"totalSize,omitempty"`
16+
Links SearchLinks `json:"_links,omitempty"`
17+
}
18+
19+
// Parsing out the _links section to allow paging etc.
20+
type SearchLinks struct {
21+
Base string `json:"base,omitempty"`
22+
Context string `json:"content,omitempty"`
23+
Next string `json:"next,omitempty"`
24+
Self string `json:"self,omitempty"`
1625
}
1726

1827
// SearchQuery defines query parameters used for searchng
@@ -41,6 +50,19 @@ func (a *API) Search(query SearchQuery) (*Search, error) {
4150
return a.SendSearchRequest(ep, "GET")
4251
}
4352

53+
// Search querys confluence using CQL, with the ability to pass in the Next header
54+
// Empty Next header will run search like normal
55+
func (a *API) SearchWithNext(query SearchQuery, next string) (*Search, error) {
56+
if next == "" {
57+
return a.Search(query)
58+
}
59+
ep, err := url.ParseRequestURI(a.endPoint.String() + strings.TrimPrefix(next, "/rest/api"))
60+
if err != nil {
61+
return nil, err
62+
}
63+
return a.SendSearchRequest(ep, "GET")
64+
}
65+
4466
// addSearchQueryParams adds the defined query parameters
4567
func addSearchQueryParams(query SearchQuery) *url.Values {
4668

search_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ func TestSearchQueryParams(t *testing.T) {
1111
CQL: "test",
1212
CQLContext: "test",
1313
IncludeArchivedSpaces: true,
14-
Limit: 1,
15-
Start: 1,
14+
Limit: 1,
15+
Start: 1,
1616
}
1717
p := addSearchQueryParams(query)
1818
assert.Equal(t, p.Get("cql"), "test")
@@ -34,3 +34,19 @@ func TestSearch(t *testing.T) {
3434
assert.Equal(t, &Search{}, s)
3535

3636
}
37+
38+
func TestSearchWithNext(t *testing.T) {
39+
server := confluenceRestAPIStub()
40+
defer server.Close()
41+
42+
api, err := NewAPI(server.URL+"/wiki/rest/api", "userame", "token")
43+
assert.Nil(t, err)
44+
45+
s, err := api.SearchWithNext(SearchQuery{}, "")
46+
assert.Nil(t, err)
47+
assert.Equal(t, &Search{}, s)
48+
49+
s, err = api.SearchWithNext(SearchQuery{}, "/rest/api/search?next=true&cursor=abc123")
50+
assert.Nil(t, err)
51+
assert.Equal(t, &Search{}, s)
52+
}

0 commit comments

Comments
 (0)