From ea85393b13bc8d57966ccb52684ff371acd6159c Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Sun, 23 Sep 2018 16:23:33 +0300 Subject: [PATCH] app.py: Use parameterized URI instead of query for /item Falcon's get_param_as_int() is really nice in that it gets a query parameter and does validation for you, but I really wanted to have cleaner URIs for API routes so I am now using a route URI template with a field converter. This is cleaner, but means that parameters not matching the template will return HTTP 404. See: https://falcon.readthedocs.io/en/stable/api/routing.html#field-converters --- app.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 5f9745e..75b7e90 100644 --- a/app.py +++ b/app.py @@ -8,10 +8,8 @@ from solr import solr_connection class ItemResource: - def on_get(self, req, resp): + def on_get(self, req, resp, item_id): """Handles GET requests""" - # Return HTTPBadRequest if id parameter is not present and valid - item_id = req.get_param_as_int("id", required=True, min=0) # Get views res = solr.query(SOLR_CORE, { @@ -38,6 +36,6 @@ class ItemResource: resp.media = statistics api = falcon.API() -api.add_route('/item', ItemResource()) +api.add_route('/item/{item_id:int}', ItemResource()) # vim: set sw=4 ts=4 expandtab: