-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggest.go
57 lines (44 loc) · 1.27 KB
/
suggest.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
package brave
import (
"context"
"net/http"
"github.com/google/go-querystring/query"
)
func (b *brave) SuggestSearch(ctx context.Context, term string, options ...SearchOption) (*SuggestSearchResult, error) {
u := *b.baseURL
u.Path = u.Path + suggestSearchPath
var opts searchOptions
applyOpts(&opts, options, nil)
var params suggestParams
params.fromSearchOptions(term, opts)
values, err := query.Values(params)
if err != nil {
return nil, err
}
u.RawQuery = values.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
opts.applyRequestHeaders(b.subscriptionToken, req)
return handleRequest[SuggestSearchResult](b.client, req)
}
type SuggestSearchResult struct {
Type string `json:"type"`
Query *Query `json:"query"`
Results []SuggestResult `json:"results"`
}
type suggestParams struct {
Term string `url:"q"`
Country string `url:"country,omitempty"`
Lang string `url:"lang,omitempty"`
Count int `url:"count,omitempty"`
Rich bool `url:"rich,omitempty"`
}
func (s *suggestParams) fromSearchOptions(term string, options searchOptions) {
s.Term = term
s.Country = options.country
s.Lang = options.lang
s.Count = options.count
s.Rich = options.rich
}