-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
77 lines (65 loc) · 1.21 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
74
75
76
77
package yuque
import (
"encoding/json"
"fmt"
"github.com/my-Sakura/go-yuque-api/internal"
)
type Searcher struct {
*Client
}
type SearcherOption struct {
// split page example: 1, 2, 3...
Offset int
/*
"topic"
"repo"
"doc"
"artboard"
"group"
*/
Kind string
// TODO Search path, The path on the team or knowledge base URL
// Scope string
}
func newSearcher(c *Client) *Searcher {
return &Searcher{
c,
}
}
// Get get user info
func (s *Searcher) Work(keyword string, options ...SearcherOption) (*SearchSerializer, error) {
var opt SearcherOption
if len(options) > 1 {
return nil, ErrTooManyOptions
} else if len(options) == 1 {
opt = options[0]
}
if opt.Offset == 0 {
opt.Offset = 1
}
if opt.Kind == "" {
opt.Kind = "doc"
}
var (
url = fmt.Sprintf(s.baseURL + internal.SearchPath)
search = SearchSerializer{}
body = struct {
Type string `json:"type"`
Q string `json:"q"`
Offset int `json:"offset"`
}{
Type: opt.Kind,
Q: keyword,
Offset: opt.Offset,
}
)
respBody, err := s.do(url, option{Body: body})
if err != nil {
return nil, err
}
err = json.Unmarshal(respBody, &search)
if err != nil {
return nil, err
}
return &search, nil
}