mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2024-11-22 14:25:01 +01:00
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:
parent
cbeb7c89a7
commit
ea85393b13
6
app.py
6
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:
|
||||
|
Loading…
Reference in New Issue
Block a user