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
This commit is contained in:
Alan Orth 2018-09-23 16:23:33 +03:00
parent cbeb7c89a7
commit ea85393b13
Signed by: alanorth
GPG Key ID: 0FB860CC9C45B1B9
1 changed files with 2 additions and 4 deletions

6
app.py
View File

@ -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: