-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
178 lines (154 loc) · 5.13 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
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
package brewerydb
// SearchService provides access to the BreweryDB Search API.
// Use Client.Search.
type SearchService struct {
c *Client
}
type searchType string
const (
searchBeer searchType = "beer"
searchBrewery = "brewery"
searchEvent = "event"
searchGuild = "guild"
)
// SearchRequest contains options for narrowing a Search.
type SearchRequest struct {
Page int
WithBreweries bool
WithSocialAccounts bool
WithGuilds bool
WithLocations bool
WithAlternateNames bool
WithIngredients bool
}
type actualSearchRequest struct {
Page int `url:"p,omitempty"`
Query string `url:"q"` // required
Type searchType `url:"type"`
WithBreweries YesNo `url:"withBreweries,omitempty"`
WithSocialAccounts YesNo `url:"withSocialAccounts,omitempty"`
WithGuilds YesNo `url:"withGuilds,omitempty"`
WithLocations YesNo `url:"withLocations,omitempty"`
WithAlternateNames YesNo `url:"withAlternateNames,omitempty"`
WithIngredients YesNo `url:"withIngredients,omitempty"`
}
func makeActualSearchRequest(req *SearchRequest, query string, tp searchType) *actualSearchRequest {
if req == nil {
return &actualSearchRequest{Query: query, Type: tp}
}
return &actualSearchRequest{
Page: req.Page,
Query: query,
Type: tp,
WithBreweries: YesNo(req.WithBreweries),
WithGuilds: YesNo(req.WithGuilds),
WithLocations: YesNo(req.WithLocations),
WithAlternateNames: YesNo(req.WithAlternateNames),
WithIngredients: YesNo(req.WithIngredients),
}
}
// Beer searches for Beers matching the given query.
func (ss *SearchService) Beer(query string, q *SearchRequest) (bl BeerList, err error) {
err = ss.search(query, q, searchBeer, &bl)
return
}
// Brewery searches for Breweries matching the given query.
func (ss *SearchService) Brewery(query string, q *SearchRequest) (bl BreweryList, err error) {
err = ss.search(query, q, searchBrewery, &bl)
return
}
// Event searches for Events matching the given query.
func (ss *SearchService) Event(query string, q *SearchRequest) (el EventList, err error) {
err = ss.search(query, q, searchEvent, &el)
return
}
// Guild searches for Guilds matching the given query.
func (ss *SearchService) Guild(query string, q *SearchRequest) (gl GuildList, err error) {
err = ss.search(query, q, searchGuild, &gl)
return
}
func (ss *SearchService) search(query string, q *SearchRequest, t searchType, data interface{}) error {
asr := makeActualSearchRequest(q, query, t)
req, err := ss.c.NewRequest("GET", "/search", asr)
if err != nil {
return err
}
return ss.c.Do(req, data)
}
// GeoPointUnit differentiates between miles and kilometers.
type GeoPointUnit string
// Units of measurement.
const (
Miles GeoPointUnit = "mi"
Kilometers GeoPointUnit = "km"
)
// GeoPointRequest contains options for specifying a geographic coordinate.
type GeoPointRequest struct {
Latitude float64 `url:"lat"` // Required
Longitude float64 `url:"lng"` // Required
Radius float64 `url:"radius,omitempty"`
Unit GeoPointUnit `url:"unit,omitempty"` // Default: mi
WithSocialAccounts YesNo `url:"withSocialAccounts,omitempty"`
WithGuilds YesNo `url:"withGuilds,omitempty"`
WithAlternateNames YesNo `url:"withAlternateNames,omitempty"`
}
// GeoPoint searches for Locations near the geographic coordinate specified in the GeoPointRequest.
// TODO: pagination??
func (ss *SearchService) GeoPoint(q *GeoPointRequest) ([]Location, error) {
req, err := ss.c.NewRequest("GET", "/search/geo/point", q)
if err != nil {
return nil, err
}
geoPointResult := struct {
NumberOfPages int
CurrentPage int
TotalResults int
Data []Location
}{}
err = ss.c.Do(req, &geoPointResult)
return geoPointResult.Data, err
}
// Style retrieves one or more Styles matching the given query string.
// TODO: pagination??
func (ss *SearchService) Style(query string, withDescriptions bool) ([]Style, error) {
q := struct {
Query string `url:"q"`
WithDescriptions YesNo `url:"withDescriptions,omitempty"`
}{query, YesNo(withDescriptions)}
req, err := ss.c.NewRequest("GET", "/search/style", &q)
if err != nil {
return nil, err
}
resp := struct {
NumberOfPages int
CurrentPage int
TotalResults int
Data []Style
}{}
err = ss.c.Do(req, &resp)
return resp.Data, err
}
// UPC retrieves one or more Beers matching the given Universal Product Code.
// TODO: pagination??
// TODO: the API doc example shows "data" as being an array of arrays,
// see: http://www.brewerydb.com/developers/docs-endpoint/search_upc
func (ss *SearchService) UPC(code uint64) ([]Beer, error) {
q := struct {
Code uint64 `url:"code"`
}{code}
req, err := ss.c.NewRequest("GET", "/search/upc", &q)
if err != nil {
return nil, err
}
resp := struct {
NumberOfPages int
CurrentPage int
TotalResults int
Data [][]Beer
}{}
err = ss.c.Do(req, &resp)
if len(resp.Data) <= 0 {
return nil, nil
}
return resp.Data[0], err
}