-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathsearch.go
77 lines (65 loc) · 1.86 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 lib
import (
"context"
"github.com/qri-io/qri/base/params"
qhttp "github.com/qri-io/qri/lib/http"
"github.com/qri-io/qri/registry"
"github.com/qri-io/qri/registry/regclient"
"github.com/qri-io/qri/repo"
)
// SearchMethods groups together methods for search
type SearchMethods struct {
d dispatcher
}
// Name returns the name of this method group
func (m SearchMethods) Name() string {
return "search"
}
// Attributes defines attributes for each method
func (m SearchMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"search": {Endpoint: qhttp.AESearch, HTTPVerb: "POST"},
}
}
// SearchParams defines paremeters for the search Method
type SearchParams struct {
params.List
Query string `json:"q"`
}
// SetNonZeroDefaults sets a default limit and offset
func (p *SearchParams) SetNonZeroDefaults() {
if p.Offset < 0 {
p.Offset = 0
}
if p.Limit <= 0 {
p.Limit = params.DefaultListLimit
}
}
// Search queries for items on qri related to given parameters
func (m SearchMethods) Search(ctx context.Context, p *SearchParams) ([]registry.SearchResult, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "search"), p)
if res, ok := got.([]registry.SearchResult); ok {
return res, err
}
return nil, dispatchReturnError(got, err)
}
// Implementations for FSI methods follow
// searchImpl holds the method implementations for search
type searchImpl struct{}
// Search queries for items on qri related to given parameters
func (searchImpl) Search(scope scope, p *SearchParams) ([]registry.SearchResult, error) {
client := scope.RegistryClient()
if client == nil {
return nil, repo.ErrNoRegistry
}
params := ®client.SearchParams{
Query: p.Query,
Limit: p.Limit,
Offset: p.Offset,
}
regResults, err := client.Search(scope.Context(), params)
if err != nil {
return nil, err
}
return regResults, nil
}