-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
203 lines (177 loc) · 5.26 KB
/
find.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmhub
import (
"context"
"net/http"
"strings"
"github.com/juju/errors"
"github.com/juju/juju/charmhub/path"
"github.com/juju/juju/charmhub/transport"
)
// FindOption to be passed to Find to customize the resulting request.
type FindOption func(*findOptions)
type findOptions struct {
category *string
channel *string
charmType *string
platforms *string
publisher *string
relationRequires *string
relationProvides *string
}
// WithFindCategory sets the category on the option.
func WithFindCategory(category string) FindOption {
return func(findOptions *findOptions) {
findOptions.category = &category
}
}
// WithFindChannel sets the channel on the option.
func WithFindChannel(channel string) FindOption {
return func(findOptions *findOptions) {
findOptions.channel = &channel
}
}
// WithFindType sets the charmType on the option.
func WithFindType(charmType string) FindOption {
return func(findOptions *findOptions) {
findOptions.charmType = &charmType
}
}
// WithFindPlatforms sets the charmPlatforms on the option.
func WithFindPlatforms(platforms string) FindOption {
return func(findOptions *findOptions) {
findOptions.platforms = &platforms
}
}
// WithFindPublisher sets the publisher on the option.
func WithFindPublisher(publisher string) FindOption {
return func(findOptions *findOptions) {
findOptions.publisher = &publisher
}
}
// WithFindRelationRequires sets the relationRequires on the option.
func WithFindRelationRequires(relationRequires string) FindOption {
return func(findOptions *findOptions) {
findOptions.relationRequires = &relationRequires
}
}
// WithFindRelationProvides sets the relationProvides on the option.
func WithFindRelationProvides(relationProvides string) FindOption {
return func(findOptions *findOptions) {
findOptions.relationProvides = &relationProvides
}
}
// Create a findOptions instance with default values.
func newFindOptions() *findOptions {
return &findOptions{}
}
// FindClient defines a client for querying information about a given charm or
// bundle for a given CharmHub store.
type FindClient struct {
path path.Path
client RESTClient
logger Logger
}
// NewFindClient creates a FindClient for querying charm or bundle information.
func NewFindClient(path path.Path, client RESTClient, logger Logger) *FindClient {
return &FindClient{
path: path,
client: client,
logger: logger,
}
}
// Find searches Charm Hub and provides results matching a string.
func (c *FindClient) Find(ctx context.Context, query string, options ...FindOption) ([]transport.FindResponse, error) {
opts := newFindOptions()
for _, option := range options {
option(opts)
}
c.logger.Tracef("Find(%s)", query)
path, err := c.path.Query("q", query)
if err != nil {
return nil, errors.Trace(err)
}
path, err = path.Query("fields", defaultFindFilter())
if err != nil {
return nil, errors.Trace(err)
}
if err := walkFindOptions(opts, func(name, value string) error {
path, err = path.Query(name, value)
return errors.Trace(err)
}); err != nil {
return nil, errors.Trace(err)
}
var resp transport.FindResponses
restResp, err := c.client.Get(ctx, path, &resp)
if err != nil {
return nil, errors.Trace(err)
}
if restResp.StatusCode == http.StatusNotFound {
return nil, errors.NotFoundf(query)
}
if err := handleBasicAPIErrors(resp.ErrorList, c.logger); err != nil {
return nil, errors.Trace(err)
}
return resp.Results, nil
}
func walkFindOptions(opts *findOptions, fn func(string, string) error) error {
// We could use reflect here, but it might be easier to just list out what
// we want to walk over.
// See: https://gist.github.com/SimonRichardson/7c9243d71551cad4af7661128add93b5
if opts.category != nil {
if err := fn("category", *opts.category); err != nil {
return errors.Trace(err)
}
}
if opts.channel != nil {
if err := fn("channel", *opts.channel); err != nil {
return errors.Trace(err)
}
}
if opts.charmType != nil {
if err := fn("type", *opts.charmType); err != nil {
return errors.Trace(err)
}
}
if opts.platforms != nil {
if err := fn("platforms", *opts.platforms); err != nil {
return errors.Trace(err)
}
}
if opts.publisher != nil {
if err := fn("publisher", *opts.publisher); err != nil {
return errors.Trace(err)
}
}
if opts.relationRequires != nil {
if err := fn("relation-requires", *opts.relationRequires); err != nil {
return errors.Trace(err)
}
}
if opts.relationProvides != nil {
if err := fn("relation-provides", *opts.relationProvides); err != nil {
return errors.Trace(err)
}
}
return nil
}
// defaultFindFilter returns a filter string to retrieve all data
// necessary to fill the transport.FindResponse. Without it, we'd
// receive the Name, ID and Type.
func defaultFindFilter() string {
filter := defaultFindResultFilter
filter = append(filter, appendFilterList("default-release", defaultRevisionFilter)...)
return strings.Join(filter, ",")
}
var defaultFindResultFilter = []string{
"result.publisher.display-name",
"result.summary",
"result.store-url",
}
var defaultRevisionFilter = []string{
"revision.bases.architecture",
"revision.bases.name",
"revision.bases.channel",
"revision.version",
}