-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspellcheck.go
53 lines (40 loc) · 1.16 KB
/
spellcheck.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
package brave
import (
"context"
"net/http"
"github.com/google/go-querystring/query"
)
func (b *brave) Spellcheck(ctx context.Context, term string, options ...SearchOption) (*SpellcheckResult, error) {
u := *b.baseURL
u.Path = u.Path + spellcheckPath
var opts searchOptions
applyOpts(&opts, options, nil)
var params spellcheckParams
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[SpellcheckResult](b.client, req)
}
type SpellcheckResult struct {
Type string `json:"type"`
Query *Query `json:"query"`
Results []SpellcheckResultItem `json:"results"`
}
type spellcheckParams struct {
Term string `url:"q"`
Country string `url:"country,omitempty"`
Lang string `url:"lang,omitempty"`
}
func (s *spellcheckParams) fromSearchOptions(term string, options searchOptions) {
s.Term = term
s.Country = options.country
s.Lang = options.lang
}