-
Notifications
You must be signed in to change notification settings - Fork 57
/
search.go
67 lines (60 loc) · 1.7 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package goconfluence
import (
"net/url"
"strconv"
"strings"
)
// 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"`
}
// SearchQuery defines query parameters used for searchng
// Query parameter values https://developer.atlassian.com/cloud/confluence/rest/#api-search-get
type SearchQuery struct {
CQL string
CQLContext string
IncludeArchivedSpaces bool
Limit int
Start int
Expand []string
}
// getContentEndpoint creates the correct api endpoint by given id
func (a *API) getSearchEndpoint() (*url.URL, error) {
return url.ParseRequestURI(a.endPoint.String() + "/search")
}
// Search querys confluence using CQL
func (a *API) Search(query SearchQuery) (*Search, error) {
ep, err := a.getSearchEndpoint()
if err != nil {
return nil, err
}
ep.RawQuery = addSearchQueryParams(query).Encode()
return a.SendSearchRequest(ep, "GET")
}
// addSearchQueryParams adds the defined query parameters
func addSearchQueryParams(query SearchQuery) *url.Values {
data := url.Values{}
if query.CQL != "" {
data.Set("cql", query.CQL)
}
if query.CQLContext != "" {
data.Set("cqlcontext", query.CQLContext)
}
if query.IncludeArchivedSpaces {
data.Set("includeArchivedSpaces", "true")
}
if query.Limit != 0 {
data.Set("limit", strconv.Itoa(query.Limit))
}
if query.Start != 0 {
data.Set("start", strconv.Itoa(query.Start))
}
if len(query.Expand) != 0 {
data.Set("expand", strings.Join(query.Expand, ","))
}
return &data
}