-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_search.go
55 lines (48 loc) · 1.13 KB
/
api_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
package redmine
import (
"encoding/json"
"fmt"
"strings"
)
type Result struct {
ID int `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
URL string `json:"url"`
Description string `json:"description"`
Datetime string `json:"datetime"`
}
func (c *Client) Search(query []string, opts ...ReqOption) ([]*Result, error) {
endpoint := "/search.json"
if query == nil {
return nil, fmt.Errorf("query is nil")
}
o := reqOptions(opts...)
o.query.Set("q", strings.Join(query, " "))
items := []*Result{}
filter := &listFilter{query: o.query}
for ; ; filter.nextPage() {
respBodyBytes, err := c.getRequest(endpoint, filter.encode())
if err != nil {
return nil, err
}
resp := struct {
listResponseAttrs
Results []*Result `json:"results"`
}{}
if err := json.Unmarshal(respBodyBytes, &resp); err != nil {
return nil, fmt.Errorf("json unmarshal error: %v", err)
}
if len(resp.Results) == 0 {
goto end
}
for _, item := range resp.Results {
items = append(items, item)
if o.limit > 0 && len(items) >= o.limit {
goto end
}
}
}
end:
return items, nil
}