mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2024-11-25 23:58:18 +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:
|
class ItemResource:
|
||||||
def on_get(self, req, resp):
|
def on_get(self, req, resp, item_id):
|
||||||
"""Handles GET requests"""
|
"""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
|
# Get views
|
||||||
res = solr.query(SOLR_CORE, {
|
res = solr.query(SOLR_CORE, {
|
||||||
@ -38,6 +36,6 @@ class ItemResource:
|
|||||||
resp.media = statistics
|
resp.media = statistics
|
||||||
|
|
||||||
api = falcon.API()
|
api = falcon.API()
|
||||||
api.add_route('/item', ItemResource())
|
api.add_route('/item/{item_id:int}', ItemResource())
|
||||||
|
|
||||||
# vim: set sw=4 ts=4 expandtab:
|
# vim: set sw=4 ts=4 expandtab:
|
||||||
|
Loading…
Reference in New Issue
Block a user