-
Notifications
You must be signed in to change notification settings - Fork 1
/
brewery.go
461 lines (410 loc) · 13.4 KB
/
brewery.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package brewerydb
import (
"fmt"
"net/http"
)
// BreweryService provides access to the BreweryDB Brewery API. Use Client.Brewery.
type BreweryService struct {
c *Client
}
// BreweryOrder represents the ordering of a list of Breweries.
type BreweryOrder string
// BreweryList ordering options.
const (
BreweryOrderName BreweryOrder = "name"
BreweryOrderDescription = "description"
BreweryOrderWebsite = "website"
BreweryOrderEstablished = "established"
BreweryOrderMailingListURL = "mailingListUrl"
BreweryOrderIsOrganic = "isOrganic"
BreweryOrderStatus = "status"
BreweryOrderCreateDate = "createDate"
BreweryOrderUpdateDate = "updateDate"
BreweryOrderRandom = "random"
)
// BreweryList represents a "page" containing one slice of Breweries.
type BreweryList struct {
CurrentPage int
NumberOfPages int
TotalResults int
Breweries []Brewery `json:"data"`
}
// Brewery contains all relevant information for a single Brewery.
type Brewery struct {
ID string `url:"-"`
Name string `url:"name"`
Description string `url:"description,omitempty"`
MailingListURL string `url:"mailingListUrl,omitempty"`
Images Images `url:"-"`
Image string `url:"image,omitempty"` // only used for adding/update Breweries
Established string `url:"established,omitempty"`
IsOrganic YesNo `url:"isOrganic,omitempty"`
Website string `url:"website,omitempty"`
Status string `url:"-"`
StatusDisplay string `url:"-"`
CreateDate string `url:"-"`
UpdateDate string `url:"-"`
}
// BreweryListRequest contains all the required and optional fields
// used for querying for a list of Breweries.
type BreweryListRequest struct {
Page int `url:"p"`
Name string `url:"name,omitempty"`
IDs string `url:"ids,omitempty"`
Established string `url:"established,omitempty"`
IsOrganic YesNo `url:"isOrganic,omitempty"`
HasImages YesNo `url:"hasImages,omitempty"`
Since string `url:"since,omitempty"`
Status string `url:"status,omitempty"`
Order BreweryOrder `url:"order,omitempty"` // TODO: enumerate
Sort string `url:"sort,omitempty"` // TODO: enumerate
RandomCount string `url:"randomCount,omitempty"`
WithSocialAccounts YesNo `url:"withSocialAccounts,omitempty"`
WithGuilds YesNo `url:"withGuilds,omitempty"`
WithLocations YesNo `url:"withLocations,omitempty"`
WithAlternateNames YesNo `url:"withAlternateNames,omitempty"`
}
// List returns all Breweries on the page specified in the given BreweryListRequest.
// For non-premium members, one of Name or Established must be set.
func (bs *BreweryService) List(q *BreweryListRequest) (bl BreweryList, err error) {
// GET: /breweries
var req *http.Request
req, err = bs.c.NewRequest("GET", "/breweries", q)
if err != nil {
return
}
err = bs.c.Do(req, &bl)
return
}
// Get queries for a single Brewery with the given Brewery ID.
func (bs *BreweryService) Get(id string) (brewery Brewery, err error) {
// GET: /brewery/:breweryId
var req *http.Request
req, err = bs.c.NewRequest("GET", "/brewery/"+id, nil)
if err != nil {
return
}
resp := struct {
Message string
Data Brewery
Status string
}{}
err = bs.c.Do(req, &resp)
return resp.Data, err
}
// Add adds a new Brewery to the BreweryDB and returns its new ID.
func (bs *BreweryService) Add(b *Brewery) (id string, err error) {
// POST: /breweries
if b == nil {
err = fmt.Errorf("nil Brewery")
return
}
var req *http.Request
req, err = bs.c.NewRequest("POST", "/breweries", b)
if err != nil {
return
}
resp := struct {
Status string
Data struct {
ID string
}
Message string
}{}
err = bs.c.Do(req, &resp)
return resp.Data.ID, err
}
// Update changes an existing Brewery in the BreweryDB.
func (bs *BreweryService) Update(breweryID string, b *Brewery) error {
// PUT: /brewery/:breweryId
if b == nil {
return fmt.Errorf("nil Brewery")
}
req, err := bs.c.NewRequest("PUT", "/brewery/"+breweryID, b)
if err != nil {
return err
}
// TODO: extract and return response message?
return bs.c.Do(req, nil)
}
// Delete removes the Brewery with the given ID from the BreweryDB.
func (bs *BreweryService) Delete(id string) error {
// DELETE: /brewery/:breweryId
req, err := bs.c.NewRequest("DELETE", "/brewery/"+id, nil)
if err != nil {
return err
}
// TODO: extract and return response message?
return bs.c.Do(req, nil)
}
// AlternateName represents an alternate name for a Brewery.
// TODO: the actual response object contains the entire Brewery object as well.
// see: http://www.brewerydb.com/developers/docs-endpoint/brewery_alternatename
type AlternateName struct {
ID int
Name string
BreweryID string
CreateDate string
UpdateDate string
}
// ListAlternateNames returns a slice of all the AlternateNames for the Brewery with the given ID.
func (bs *BreweryService) ListAlternateNames(breweryID string) (al []AlternateName, err error) {
// GET: /brewery/:breweryId/alternatenames
var req *http.Request
req, err = bs.c.NewRequest("GET", "/brewery/"+breweryID+"/alternatenames", nil)
if err != nil {
return
}
resp := struct {
Status string
Data []AlternateName
Message string
}{}
err = bs.c.Do(req, &resp)
return resp.Data, err
}
// AddAlternateName adds an alternate name to the Brewery with the given ID
// and returns the alternate name's new ID.
func (bs *BreweryService) AddAlternateName(breweryID, name string) (id int, err error) {
// POST: /brewery/:breweryId/alternatenames
q := struct {
Name string `url:"name"`
}{name}
var req *http.Request
req, err = bs.c.NewRequest("POST", "/brewery/"+breweryID+"/alternatenames", &q)
if err != nil {
return
}
resp := struct {
Status string
Data struct {
ID int
}
Message string
}{}
err = bs.c.Do(req, &resp)
return resp.Data.ID, err
}
// DeleteAlternateName removes the AlternateName with the given ID from the Brewery with the given ID.
func (bs *BreweryService) DeleteAlternateName(breweryID string, alternateNameID int) error {
// DELETE: /brewery/:breweryId/alternatename/:alternatenameId
req, err := bs.c.NewRequest("DELETE", fmt.Sprintf("/brewery/%s/alternatename/%d", breweryID, alternateNameID), nil)
if err != nil {
return err
}
return bs.c.Do(req, nil)
}
// BreweryBeersRequest contains options for querying for all Beers from a Brewery.
type BreweryBeersRequest struct {
WithBreweries YesNo `url:"withBreweries,omitempty"`
WithSocialAccounts YesNo `url:"withSocialAccounts,omitempty"`
WithIngredients YesNo `url:"withIngredients,omitempty"`
}
// ListBeers returns a slice of all Beers offered by the Brewery with the given ID.
func (bs *BreweryService) ListBeers(breweryID string, q *BreweryBeersRequest) (bl []Beer, err error) {
// GET: /brewery/:breweryId/beers
var req *http.Request
req, err = bs.c.NewRequest("GET", "/brewery/"+breweryID+"/beers", q)
if err != nil {
return
}
resp := struct {
Status string
Data []Beer
Message string
}{}
err = bs.c.Do(req, &resp)
return resp.Data, err
}
// ListEvents returns a slice of Events where the given Brewery is/was present
// or has won awards.
func (bs *BreweryService) ListEvents(breweryID string, onlyWinners bool) (el []Event, err error) {
// GET: /brewery/:breweryId/events
q := struct {
OnlyWinners YesNo `url:"onlyWinners,omitempty"`
}{YesNo(onlyWinners)}
var req *http.Request
req, err = bs.c.NewRequest("GET", "/brewery/"+breweryID+"/events", &q)
if err != nil {
return
}
resp := struct {
Status string
Data []Event
Message string
}{}
err = bs.c.Do(req, &resp)
return resp.Data, err
}
// ListGuilds returns a slice of all Guilds the Brewery with the given ID belongs to.
func (bs *BreweryService) ListGuilds(breweryID string) (al []Guild, err error) {
// GET: /brewery/:breweryId/guilds
var req *http.Request
req, err = bs.c.NewRequest("GET", "/brewery/"+breweryID+"/guilds", nil)
if err != nil {
return
}
resp := struct {
Status string
Data []Guild
Message string
}{}
err = bs.c.Do(req, &resp)
return resp.Data, err
}
// AddGuild adds the Guild with the given ID to the Brewery with the given ID.
// discount is optional (value of discount offered to guild members).
func (bs *BreweryService) AddGuild(breweryID string, guildID string, discount *string) error {
// POST: /brewery/:breweryId/guilds
q := struct {
ID string `url:"guildId"`
Discount string `url:"discount"`
}{ID: guildID}
if discount != nil {
q.Discount = *discount
}
req, err := bs.c.NewRequest("POST", "/brewery/"+breweryID+"/guilds", &q)
if err != nil {
return err
}
return bs.c.Do(req, nil)
}
// DeleteGuild removes the Guild with the given ID from the Brewery with the given ID.
func (bs *BreweryService) DeleteGuild(breweryID string, guildID string) error {
// DELETE: /brewery/:breweryId/guild/:guildId
req, err := bs.c.NewRequest("DELETE", "/brewery/"+breweryID+"/guild/"+guildID, nil)
if err != nil {
return err
}
return bs.c.Do(req, nil)
}
// ListLocations returns a slice of all locations for the Brewery with the given ID.
func (bs *BreweryService) ListLocations(breweryID string) (ll []Location, err error) {
// GET: /brewery/:breweryId/locations
var req *http.Request
req, err = bs.c.NewRequest("GET", "/brewery/"+breweryID+"/locations", nil)
if err != nil {
return
}
resp := struct {
Status string
Data []Location
Message string
}{}
err = bs.c.Do(req, &resp)
return resp.Data, err
}
// AddLocation adds a new location for the Brewery with the given ID
// and returns the new location's ID.
// TODO: verify that the location ID in the response is, in fact, called "guid".
// see: http://www.brewerydb.com/developers/docs-endpoint/brewery_location#2
func (bs *BreweryService) AddLocation(breweryID string, loc *Location) (id string, err error) {
// POST: /brewery/:breweryId/locations
if loc == nil {
return "", fmt.Errorf("nil Location")
}
var req *http.Request
req, err = bs.c.NewRequest("POST", "/brewery/"+breweryID+"/locations", loc)
if err != nil {
return
}
resp := struct {
Status string
Data struct {
ID string `json:"guid"`
}
Message string
}{}
err = bs.c.Do(req, &resp)
return resp.Data.ID, err
}
// RandomBreweryRequest contains options for retrieving a random Brewery.
type RandomBreweryRequest struct {
Established string `url:"established"` // YYYY
IsOrganic YesNo `url:"isOrganic"`
WithSocialAccounts YesNo `url:"withSocialAccounts"`
WithGuilds YesNo `url:"withGuilds"`
WithLocations YesNo `url:"withLocations"`
WithAlternateNames YesNo `url:"withAlternateNames"`
}
// GetRandom returns a random active Brewery.
func (bs *BreweryService) GetRandom(q *RandomBreweryRequest) (b Brewery, err error) {
// GET: /brewery/random
var req *http.Request
req, err = bs.c.NewRequest("GET", "/brewery/random", q)
if err != nil {
return
}
resp := struct {
Status string
Data Brewery
Message string
}{}
err = bs.c.Do(req, &resp)
return resp.Data, err
}
// ListSocialAccounts returns a slice of all social media accounts associated with the given Brewery.
func (bs *BreweryService) ListSocialAccounts(breweryID string) (sl []SocialAccount, err error) {
// GET: /brewery/:breweryId/socialaccounts
var req *http.Request
req, err = bs.c.NewRequest("GET", "/brewery/"+breweryID+"/socialaccounts", nil)
if err != nil {
return
}
resp := struct {
Status string
Data []SocialAccount
Message string
}{}
err = bs.c.Do(req, &resp)
return resp.Data, err
}
// GetSocialAccount retrieves the SocialAccount with the given ID for the given Brewery.
func (bs *BreweryService) GetSocialAccount(breweryID string, socialAccountID int) (s SocialAccount, err error) {
// GET: /brewery/:breweryId/socialaccount/:socialaccountId
var req *http.Request
req, err = bs.c.NewRequest("GET", fmt.Sprintf("/brewery/%s/socialaccount/%d", breweryID, socialAccountID), nil)
if err != nil {
return
}
resp := struct {
Status string
Data SocialAccount
Message string
}{}
err = bs.c.Do(req, &resp)
return resp.Data, err
}
// AddSocialAccount adds a new SocialAccount to the given Brewery.
func (bs *BreweryService) AddSocialAccount(breweryID string, s *SocialAccount) error {
// POST: /brewery/:breweryId/socialaccounts
if s == nil {
return fmt.Errorf("nil SocialAccount")
}
req, err := bs.c.NewRequest("POST", "/brewery/"+breweryID+"/socialaccounts", s)
if err != nil {
return err
}
return bs.c.Do(req, nil)
}
// UpdateSocialAccount updates a SocialAccount for the given Brewery.
func (bs *BreweryService) UpdateSocialAccount(breweryID string, s *SocialAccount) error {
// PUT: /brewery/:breweryId/socialaccount/:socialaccountId
if s == nil {
return fmt.Errorf("nil SocialAccount")
}
req, err := bs.c.NewRequest("PUT", fmt.Sprintf("/brewery/%s/socialaccount/%d", breweryID, s.ID), s)
if err != nil {
return err
}
return bs.c.Do(req, nil)
}
// DeleteSocialAccount removes a SocialAccount from the given Brewery.
func (bs *BreweryService) DeleteSocialAccount(breweryID string, socialAccountID int) error {
// DELETE: /brewery/:breweryId/socialaccount/:socialaccountId
req, err := bs.c.NewRequest("DELETE", fmt.Sprintf("/brewery/%s/socialaccount/%d", breweryID, socialAccountID), nil)
if err != nil {
return err
}
return bs.c.Do(req, nil)
}