Giter Club home page Giter Club logo

brewerydb's Introduction

brewerydb

brewerydb is a Go library for accessing the BreweryDB API

GoDoc Build StatusCoverage Status

usage

import "github.com/naegelejd/brewerydb"

Construct a new Client using your BreweryDB API key:

client := brewerydb.NewClient("<your API key>")

Then use the available services to access the API. For example:

// Get any random beer
beer, _ := client.Beer.Random(&brewerydb.RandomBeerRequest{ABV: "8"})
fmt.Println(beer.Name, beer.Style.Name)

or

// Get all breweries established in 1983
bs, err := client.Brewery.List(&brewerydb.BreweryListRequest{Established: "1983"})
if err != nil {
    panic(err)
}
for _, b := range bs {
    fmt.Println(b.Name, b.Website)
}

or

// "What is in Dragon's Milk?"
bl, _ := client.Search.Beer("Dragon's Milk", nil)

var beerID string
for _, beer := range bl.Beers {
    if beer.Name == "Dragon's Milk" {
        beerID = beer.ID
    }
}
if beerID == "" {
    panic("Dragon's Milk not found")
}

ingredients, _ := client.Beer.ListIngredients(beerID)
adjuncts, _ := client.Beer.ListAdjuncts(beerID)
fermentables, _ := client.Beer.ListFermentables(beerID)
hops, _ := client.Beer.ListHops(beerID)
yeasts, _ := client.Beer.ListYeasts(beerID)

fmt.Println("Dragon's Milk:")
fmt.Println("  Ingredients:")
for _, ingredient := range ingredients {
    fmt.Println("    " + ingredient.Name)
}
fmt.Println("\n  Adjuncts:")
for _, adjunct := range adjuncts {
    fmt.Println("    " + adjunct.Name)
}
fmt.Println("  Fermentables:")
for _, fermentable := range fermentables {
    fmt.Println("    " + fermentable.Name)
}
fmt.Println("  Hops:")
for _, hop := range hops {
    fmt.Println("    " + hop.Name)
}
fmt.Println("  Yeasts:")
for _, yeast := range yeasts {
    fmt.Println("    " + yeast.Name)
}

status

This library is under development. Please feel free to suggest design changes or report issues.

license

This library is distributed under the BSD-style license found in the LICENSE file.

viewsviews 24h

brewerydb's People

Contributors

movingtomars avatar naegelejd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

brewerydb's Issues

Weak URL querystring encoding

The current support for encoding "request" structs as URL query strings is weak (because I hacked it out). The easiest way to solve this is to use github.com/google/go-querystring/query. I've evaluated it and decided that it would solve many problems at once:

  1. Proper support for encoding embedded structs
  2. Better struct tags for fields that should vs shouldn't be encoded (currently just using json, which is inaccurate)
  3. Unification of "query" structs. Some methods require a query struct like BeerChangeRequest for adding new beers or updating existing ones, when really they should just take a Beer struct. This requires omitting some fields in the Beer struct during encoding, e.g. ID, CreateDate, etc.

Service methods have confusing names

(For example) I don't think the EventService should allow retrieving Events using List and Get when adding new ones or updating/deleting existing ones requires AddEvent, UpdateEvent, or DeleteEvent, respectively. The EventService also has methods like ListBeers, AddBeer, UpdateBrewery, etc.

Should the methods called List and Get be renamed to include the type of object being retrieved, or should the Add..., Update..., and Delete... methods have the object type removed from their name for each service?

Option 1:

client.Event.List -> client.Event.ListEvents
client.Event.Get  -> client.Event.GetEvent

or Option 2:

client.Event.AddEvent    -> client.Event.Add
client.Event.UpdateEvent -> client.Event.Update
client.Event.DeleteEvent -> client.Event.Delete

For reference, the BeerService currently looks like option 2.

Inconsistent "List" types

Many endpoints return a "list" of objects, usually paginated. The current API has three different "list" types:

  • normal slice of objects
  • a thin struct wrapper containing a slice of objects, one for each "page"
  • a struct wrapper that supports iteration with built-in pagination using a First and Next method

Obviously the most convenient is a slice of all the objects. This is possible for all collections that are not paginated. For paginated collections, the current First, Next idiom, e.g.

c := NewCollection(...)
for v, err := c.First(); v != nil; v, err = c.Next() {
    if err != nil {
        // handle err and break
    }
    // do something with v
}

is convenient because it handles pagination under the hood and allows for clean iteration over every object. It has a few problems though:

  1. A user has no control over pagination (and therefore the number of API requests they make)
  2. If First returns a nil pointer to an object, and an error, say a critical error, the error isn't caught. Sure you could declare err before the loop and check it after the loop, but that's a terrible idea.
  3. Much of the API returns objects not pointers, where this iteration API requires pointers because it uses nil as the loop terminator.

The thin wrapper with manual pagination is simple. It'd be nice to have some optional API on top it to allow for iteration, perhaps using a channel (since they are iterable using range).

"Search" endpoint can't search all types of data

The "search" endpoint on BreweryDB allows for searching breweries, beers, guilds and events but since objects of each type are returned in one JSON response, the Go implementation just has 4 separate methods for searching each type. This makes the code simple, but it also means a user must make more requests to search for multiple types of objects, e.g. searching for both breweries and beers with a specific term requires two method calls and two HTTP requests.

"Extra" JSON fields are lost

In the case that there are JSON fields in a response that aren't defined in the corresponding Go struct, it would be nice if they were unmarshalled into some extra space. Apparently other JSON libraries are capable of doing this. At the very least it would help develop/debug this library as there are a few discrepancies in the BreweryDB API documentation.

HTTP error codes are swallowed

As of now, if there is an HTTP error during a request, the error code is caught and swallowed and a very useless message is returned in its place.

Google's Github Go API actually returns an entire Response object for each endpoint, along with the result "object" and an error. This makes sense, but I don't know that it's not overkill. Regardless, something must be done.

All "Y/N" string fields are not idomatic Go

Ideally, all "Y/N" fields should be of type bool in the Go API. I guess this would require special JSON decoding/encoding.

On this note, it would be nice if all the "year" and "postalCode" fields could be ints in Go but still encode to strings in JSON.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.