Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,20 @@ func main() {
fmt.Printf("%+v\n", v)
}

// search example with paging using SearchWithNext and Links.Next
next := ""
for {
resp, err := api.SearchWithNext(query, next)
if err != nil {
log.Fatal(err)
}
for _, v := range result.Results {
fmt.Printf("%+v\n", v)
}
next = resp.Links.Next
if next == "" {
break
}
log.Printf("Using next page: %s", next)
}
}
2 changes: 2 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ func confluenceRestAPIStub() *httptest.Server {
resp = User{}
case "/wiki/rest/api/search":
resp = Search{}
case "/wiki/rest/api/search?next=true&cursor=abc123":
resp = Search{}
case "/wiki/rest/api/content/42/history":
resp = History{}
case "/wiki/rest/api/content/42/label":
Expand Down
32 changes: 27 additions & 5 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ import (

// Search results
type Search struct {
Results []Results `json:"results"`
Start int `json:"start,omitempty"`
Limit int `json:"limit,omitempty"`
Size int `json:"size,omitempty"`
TotalSize int `json:"totalSize,omitempty"`
Results []Results `json:"results"`
Start int `json:"start,omitempty"`
Limit int `json:"limit,omitempty"`
Size int `json:"size,omitempty"`
TotalSize int `json:"totalSize,omitempty"`
Links SearchLinks `json:"_links,omitempty"`
}

// Parsing out the _links section to allow paging etc.
type SearchLinks struct {
Base string `json:"base,omitempty"`
Context string `json:"content,omitempty"`
Next string `json:"next,omitempty"`
Self string `json:"self,omitempty"`
}

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

// Search querys confluence using CQL, with the ability to pass in the Next header
// Empty Next header will run search like normal
func (a *API) SearchWithNext(query SearchQuery, next string) (*Search, error) {
if next == "" {
return a.Search(query)
}
ep, err := url.ParseRequestURI(a.endPoint.String() + strings.TrimPrefix(next, "/rest/api"))
if err != nil {
return nil, err
}
return a.SendSearchRequest(ep, "GET")
}

// addSearchQueryParams adds the defined query parameters
func addSearchQueryParams(query SearchQuery) *url.Values {

Expand Down
20 changes: 18 additions & 2 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func TestSearchQueryParams(t *testing.T) {
CQL: "test",
CQLContext: "test",
IncludeArchivedSpaces: true,
Limit: 1,
Start: 1,
Limit: 1,
Start: 1,
}
p := addSearchQueryParams(query)
assert.Equal(t, p.Get("cql"), "test")
Expand All @@ -34,3 +34,19 @@ func TestSearch(t *testing.T) {
assert.Equal(t, &Search{}, s)

}

func TestSearchWithNext(t *testing.T) {
server := confluenceRestAPIStub()
defer server.Close()

api, err := NewAPI(server.URL+"/wiki/rest/api", "userame", "token")
assert.Nil(t, err)

s, err := api.SearchWithNext(SearchQuery{}, "")
assert.Nil(t, err)
assert.Equal(t, &Search{}, s)

s, err = api.SearchWithNext(SearchQuery{}, "/rest/api/search?next=true&cursor=abc123")
assert.Nil(t, err)
assert.Equal(t, &Search{}, s)
}
Loading