-
Notifications
You must be signed in to change notification settings - Fork 6
/
beer_api.py
49 lines (38 loc) · 1.65 KB
/
beer_api.py
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
import requests
DEFAULT_BASE_URI = "http://api.brewerydb.com/v2"
BASE_URI = ""
API_KEY = ""
simple_endpoints = ["beers", "breweries", "categories", "events",
"featured", "features", "fluidsizes", "glassware",
"locations", "guilds", "heartbeat", "ingredients",
"search", "search/upc", "socialsites", "styles"]
single_param_endpoints = ["beer", "brewery", "category", "event",
"feature", "glass", "guild", "ingredient",
"location", "socialsite", "style", "menu"]
class BreweryDb:
@staticmethod
def __make_simple_endpoint_fun(name):
@staticmethod
def _function(options={}):
return BreweryDb._get("/" + name, options)
return _function
@staticmethod
def __make_singlearg_endpoint_fun(name):
@staticmethod
def _function(id, options={}):
return BreweryDb._get("/" + name + "/" + id, options)
return _function
@staticmethod
def _get(request, options):
options.update({"key": BreweryDb.API_KEY})
return requests.get(BreweryDb.BASE_URI + request, params=options).json()
@staticmethod
def configure(apikey, baseuri=DEFAULT_BASE_URI):
BreweryDb.API_KEY = apikey
BreweryDb.BASE_URI = baseuri
for endpoint in simple_endpoints:
fun = BreweryDb.__make_simple_endpoint_fun(endpoint)
setattr(BreweryDb, endpoint.replace('/', '_'), fun)
for endpoint in single_param_endpoints:
fun = BreweryDb.__make_singlearg_endpoint_fun(endpoint)
setattr(BreweryDb, endpoint.replace('/', '_'), fun)