From 76022a4a38dca8648574ab18a0685bdc7df8769f Mon Sep 17 00:00:00 2001 From: Josh Caldwell Date: Tue, 27 Jan 2015 22:53:36 -0800 Subject: [PATCH 1/4] Add multipart endpoints and post functions Some endpoints (event in particular) require /endpoint/{id}/endpoints or /endpoint/{id}/endpoint/{id2} structure - this update allows for that. Additionally there is a first attempt at allowing post data (currently erroring about endpoint) --- brewerydb.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/brewerydb.py b/brewerydb.py index c9b09cf..efa40a7 100755 --- a/brewerydb.py +++ b/brewerydb.py @@ -13,20 +13,51 @@ "feature", "glass", "guild", "ingredient", "location", "socialsite", "style", "menu"] +complex_endpoints = ["event:breweries", "event:beers", "beer;events", + "brewery:events"] + +double_param_endpoints = ["event:brewery", "event:beer"] + class BreweryDb: @staticmethod - def __make_simple_endpoint_fun(name): + def __make_simple_endpoint_fun(name, method = 'get'): @staticmethod def _function(options={}): - return BreweryDb._get("/" + name, options) + if method == 'post': + return BreweryDb._post("/" + name, options) + else: + return BreweryDb._get("/" + name, options) + return _function + + @staticmethod + def __make_singlearg_endpoint_fun(name, method = 'get'): + @staticmethod + def _function(id, options={}): + if method == 'post': + return BreweryDb._post("/" + name + "/" + id, options) + else: + return BreweryDb._get("/" + name + "/" + id, options) return _function @staticmethod - def __make_singlearg_endpoint_fun(name): + def __make_complex_endpoint_fun(name, method = 'get'): @staticmethod def _function(id, options={}): - return BreweryDb._get("/" + name + "/" + id, options) + if method == 'post': + return BreweryDb._post("/" + name[0] + "/" + id + "/" + name[1], options) + else: + return BreweryDb._get("/" + name[0] + "/" + id + "/" + name[1], options) + return _function + + @staticmethod + def __make_doublearg_endpoint_fun(name, method = 'get'): + @staticmethod + def _function(base, id, options={}): + if method == 'post': + return BreweryDb._post("/" + name[0] + "/" + base + "/" + name[1] + "/" + id, options) + else: + return BreweryDb._get("/" + name[0] + "/" + base + "/" + name[1] + "/" + id, options) return _function @staticmethod @@ -35,12 +66,35 @@ def _get(request, options): return requests.get(BreweryDb.BASE_URI + request, params=options).json() @staticmethod - def configure(apikey, baseuri=DEFAULT_BASE_URI): + def _post(request, options): + options.update({"key" : BreweryDb.API_KEY}) + return requests.post(BreweryDb.BASE_URI + request, params=options).json() + + @staticmethod + def configure(apikey=API_KEY, 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) + fun_post = BreweryDb.__make_simple_endpoint_fun(endpoint) + setattr(BreweryDb, endpoint.replace('/', '_') + '_post', fun_post) + for endpoint in single_param_endpoints: fun = BreweryDb.__make_singlearg_endpoint_fun(endpoint) setattr(BreweryDb, endpoint.replace('/', '_'), fun) + fun_post = BreweryDb.__make_singlearg_endpoint_fun(endpoint) + setattr(BreweryDb, endpoint.replace('/', '_') + '_post', fun_post) + + for endpoint in complex_endpoints: + fun = BreweryDb.__make_complex_endpoint_fun(endpoint.split(':')) + setattr(BreweryDb, endpoint.replace(':', '_'), fun) + fun_post = BreweryDb.__make_complex_endpoint_fun(endpoint) + setattr(BreweryDb, endpoint.replace(':', '_') + '_post', fun_post) + + for endpoint in double_param_endpoints: + fun = BreweryDb.__make_doublearg_endpoint_fun(endpoint.split(':')) + setattr(BreweryDb, endpoint.replace(':', '_'), fun) + fun_post = BreweryDb.__make_doublearg_endpoint_fun(endpoint) + setattr(BreweryDb, endpoint.replace(':', '_') + '_post', fun_post) From bd1065bc1304bec30cd09047ababc9269330c852 Mon Sep 17 00:00:00 2001 From: Josh Caldwell Date: Wed, 28 Jan 2015 08:46:32 -0800 Subject: [PATCH 2/4] post functions Post functions work, though currently it generates a post for every endpoint, whether or not it needs one. --- brewerydb.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/brewerydb.py b/brewerydb.py index efa40a7..be820ca 100755 --- a/brewerydb.py +++ b/brewerydb.py @@ -44,10 +44,11 @@ def _function(id, options={}): def __make_complex_endpoint_fun(name, method = 'get'): @staticmethod def _function(id, options={}): + endpoint = "/" + name[0] + "/" + id + "/" + name[1] if method == 'post': - return BreweryDb._post("/" + name[0] + "/" + id + "/" + name[1], options) + return BreweryDb._post(endpoint, options) else: - return BreweryDb._get("/" + name[0] + "/" + id + "/" + name[1], options) + return BreweryDb._get(endpoint, options) return _function @staticmethod @@ -90,11 +91,11 @@ def configure(apikey=API_KEY, baseuri=DEFAULT_BASE_URI): for endpoint in complex_endpoints: fun = BreweryDb.__make_complex_endpoint_fun(endpoint.split(':')) setattr(BreweryDb, endpoint.replace(':', '_'), fun) - fun_post = BreweryDb.__make_complex_endpoint_fun(endpoint) + fun_post = BreweryDb.__make_complex_endpoint_fun(endpoint.split(':'), 'post') setattr(BreweryDb, endpoint.replace(':', '_') + '_post', fun_post) for endpoint in double_param_endpoints: fun = BreweryDb.__make_doublearg_endpoint_fun(endpoint.split(':')) setattr(BreweryDb, endpoint.replace(':', '_'), fun) - fun_post = BreweryDb.__make_doublearg_endpoint_fun(endpoint) + fun_post = BreweryDb.__make_doublearg_endpoint_fun(endpoint.split(':'), 'post') setattr(BreweryDb, endpoint.replace(':', '_') + '_post', fun_post) From 2b1e81e982fd3d646390436232b2ed0451c1cdcc Mon Sep 17 00:00:00 2001 From: Josh Caldwell Date: Wed, 28 Jan 2015 12:30:09 -0800 Subject: [PATCH 3/4] Enable Put and Delete Simplified overall structure of function generation, added ability to Put and Delete records. Should be backwards compatible (no method arg assumes GET) --- brewerydb.py | 65 ++++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/brewerydb.py b/brewerydb.py index be820ca..92f0929 100755 --- a/brewerydb.py +++ b/brewerydb.py @@ -4,7 +4,7 @@ BASE_URI = "" API_KEY = "" -simple_endpoints = ["beers", "breweries", "categories", "events", +simple_endpoints = ["beers", "beer/random", "breweries", "categories", "events", "featured", "features", "fluidsizes", "glassware", "locations", "guilds", "heartbeat", "ingredients", "search", "search/upc", "socialsites", "styles"] @@ -21,55 +21,48 @@ class BreweryDb: @staticmethod - def __make_simple_endpoint_fun(name, method = 'get'): + def __make_simple_endpoint_fun(name): @staticmethod - def _function(options={}): - if method == 'post': - return BreweryDb._post("/" + name, options) - else: - return BreweryDb._get("/" + name, options) + def _function(options={}, method = 'get'): + endpoint = "/" + name + return BreweryDb._request(endpoint, options, method) return _function @staticmethod - def __make_singlearg_endpoint_fun(name, method = 'get'): + def __make_singlearg_endpoint_fun(name): @staticmethod - def _function(id, options={}): - if method == 'post': - return BreweryDb._post("/" + name + "/" + id, options) - else: - return BreweryDb._get("/" + name + "/" + id, options) + def _function(id, options={}, method = 'get'): + endpoint = "/" + name + "/" + id + return BreweryDb._request(endpoint, options, method) return _function @staticmethod - def __make_complex_endpoint_fun(name, method = 'get'): + def __make_complex_endpoint_fun(name): @staticmethod - def _function(id, options={}): + def _function(id, options={}, method = 'get'): endpoint = "/" + name[0] + "/" + id + "/" + name[1] - if method == 'post': - return BreweryDb._post(endpoint, options) - else: - return BreweryDb._get(endpoint, options) + return BreweryDb._request(endpoint, options, method) return _function @staticmethod - def __make_doublearg_endpoint_fun(name, method = 'get'): + def __make_doublearg_endpoint_fun(name): @staticmethod - def _function(base, id, options={}): - if method == 'post': - return BreweryDb._post("/" + name[0] + "/" + base + "/" + name[1] + "/" + id, options) - else: - return BreweryDb._get("/" + name[0] + "/" + base + "/" + name[1] + "/" + id, options) + def _function(base, id, options={}, method = 'get'): + endpoint = "/" + name[0] + "/" + base + "/" + name[1] + "/" + id + return BreweryDb._request(endpoint, options, method) return _function @staticmethod - def _get(request, options): + def _request(request, options, method): options.update({"key" : BreweryDb.API_KEY}) - return requests.get(BreweryDb.BASE_URI + request, params=options).json() - - @staticmethod - def _post(request, options): - options.update({"key" : BreweryDb.API_KEY}) - return requests.post(BreweryDb.BASE_URI + request, params=options).json() + if method == 'post': + return requests.post(BreweryDb.BASE_URI + request, params=options).json() + elif method == 'put': + return requests.put(BreweryDb.BASE_URI + request, params=options).json() + elif method == 'delete': + return requests.delete(BreweryDb.BASE_URI + request, params=options).json() + else: + return requests.get(BreweryDb.BASE_URI + request, params=options).json() @staticmethod def configure(apikey=API_KEY, baseuri=DEFAULT_BASE_URI): @@ -79,23 +72,15 @@ def configure(apikey=API_KEY, baseuri=DEFAULT_BASE_URI): for endpoint in simple_endpoints: fun = BreweryDb.__make_simple_endpoint_fun(endpoint) setattr(BreweryDb, endpoint.replace('/', '_'), fun) - fun_post = BreweryDb.__make_simple_endpoint_fun(endpoint) - setattr(BreweryDb, endpoint.replace('/', '_') + '_post', fun_post) for endpoint in single_param_endpoints: fun = BreweryDb.__make_singlearg_endpoint_fun(endpoint) setattr(BreweryDb, endpoint.replace('/', '_'), fun) - fun_post = BreweryDb.__make_singlearg_endpoint_fun(endpoint) - setattr(BreweryDb, endpoint.replace('/', '_') + '_post', fun_post) for endpoint in complex_endpoints: fun = BreweryDb.__make_complex_endpoint_fun(endpoint.split(':')) setattr(BreweryDb, endpoint.replace(':', '_'), fun) - fun_post = BreweryDb.__make_complex_endpoint_fun(endpoint.split(':'), 'post') - setattr(BreweryDb, endpoint.replace(':', '_') + '_post', fun_post) for endpoint in double_param_endpoints: fun = BreweryDb.__make_doublearg_endpoint_fun(endpoint.split(':')) setattr(BreweryDb, endpoint.replace(':', '_'), fun) - fun_post = BreweryDb.__make_doublearg_endpoint_fun(endpoint.split(':'), 'post') - setattr(BreweryDb, endpoint.replace(':', '_') + '_post', fun_post) From 8497dd7472e3533e74938217a6e31012148f2628 Mon Sep 17 00:00:00 2001 From: Josh Caldwell Date: Wed, 28 Jan 2015 12:41:38 -0800 Subject: [PATCH 4/4] Add menu endpoint --- brewerydb.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/brewerydb.py b/brewerydb.py index 92f0929..f5919c4 100755 --- a/brewerydb.py +++ b/brewerydb.py @@ -6,14 +6,14 @@ simple_endpoints = ["beers", "beer/random", "breweries", "categories", "events", "featured", "features", "fluidsizes", "glassware", - "locations", "guilds", "heartbeat", "ingredients", + "locations", "guilds", "heartbeat", "ingredients", "menu", "search", "search/upc", "socialsites", "styles"] single_param_endpoints = ["beer", "brewery", "category", "event", "feature", "glass", "guild", "ingredient", "location", "socialsite", "style", "menu"] -complex_endpoints = ["event:breweries", "event:beers", "beer;events", +complex_endpoints = ["event:breweries", "event:beers", "beer:events", "brewery:events"] double_param_endpoints = ["event:brewery", "event:beer"]