forked from seppestas/go-confluence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
73 lines (63 loc) · 1.76 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
68
69
70
71
72
73
package confluence
import (
"encoding/json"
"net/http"
"net/url"
"strings"
)
type ContentResult struct {
Content Content `json:"content"`
Title string `json:"title"`
Excerpt string `json:"excerpt"`
URL string `json:"url"`
ResultGlobalContainer struct {
Title string `json:"title"`
DisplayURL string `json:"displayUrl"`
}
// breadcrumbs is ignored
EntityType string `json:"entityType"`
IconCSSClass string `json:"iconCssClass"`
LastModified string `json:"lastModified"`
FriendlyLastModified string `json:"friendlyLastModified"`
}
type GenericResults struct {
Start int `json:"size"`
Limit int `json:"limit"`
Size int `json:"size"`
}
type SearchResults struct {
GenericResults
Results []ContentResult `json:"results"`
TotalSize int `json:"totalSize"`
CqlQuery string `json:"cqlQuery"`
SearchDuration int `json:"SearchDuration"`
// links are ignored
}
func (w *Wiki) searchEndpoint() (*url.URL, error) {
return url.ParseRequestURI(w.endPoint.String() + "/search")
}
func (w *Wiki) Search(cql, cqlContext string, expand []string, limit int) (*SearchResults, error) {
searchEndPoint, err := w.searchEndpoint()
if err != nil {
return nil, err
}
data := url.Values{}
data.Set("expand", strings.Join(expand, ","))
data.Set("cqlcontext", cqlContext)
data.Set("cql", cql)
searchEndPoint.RawQuery = data.Encode()
req, err := http.NewRequest("GET", searchEndPoint.String(), nil)
if err != nil {
return nil, err
}
res, err := w.sendRequest(req)
if err != nil {
return nil, err
}
var results SearchResults
err = json.Unmarshal(res, &results)
if err != nil {
return nil, err
}
return &results, nil
}