mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2024-11-22 06:15:02 +01:00
Return HTTP 404 when an item id is not found
This commit is contained in:
parent
c0b550109a
commit
2850035a4c
@ -15,7 +15,7 @@ Create a virtual environment and run it:
|
|||||||
The API exposes the following endpoints:
|
The API exposes the following endpoints:
|
||||||
|
|
||||||
- GET `/items` — return views and downloads for all items that Solr knows about¹. Accepts `limit` and `page` query parameters for pagination of results.
|
- GET `/items` — return views and downloads for all items that Solr knows about¹. Accepts `limit` and `page` query parameters for pagination of results.
|
||||||
- GET `/item/id` — return views and downloads for a single item (*id* must be a positive integer).
|
- GET `/item/id` — return views and downloads for a single item (*id* must be a positive integer). Returns HTTP 404 if an item id is not found.
|
||||||
|
|
||||||
¹ We are querying the Solr statistics core, which technically only knows about items that have either views or downloads.
|
¹ We are querying the Solr statistics core, which technically only knows about items that have either views or downloads.
|
||||||
|
|
||||||
@ -24,7 +24,6 @@ The API exposes the following endpoints:
|
|||||||
- Add API documentation
|
- Add API documentation
|
||||||
- Close up DB connection when gunicorn shuts down gracefully
|
- Close up DB connection when gunicorn shuts down gracefully
|
||||||
- Better logging
|
- Better logging
|
||||||
- Return HTTP 404 when item_id is nonexistent
|
|
||||||
- Tests
|
- Tests
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
9
app.py
9
app.py
@ -47,8 +47,13 @@ class ItemResource:
|
|||||||
|
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
cursor.execute('SELECT views, downloads FROM items WHERE id={}'.format(item_id))
|
cursor.execute('SELECT views, downloads FROM items WHERE id={}'.format(item_id))
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
raise falcon.HTTPNotFound(
|
||||||
|
title='Item not found',
|
||||||
|
description='The item with id "{}" was not found.'.format(item_id)
|
||||||
|
)
|
||||||
|
else:
|
||||||
results = cursor.fetchone()
|
results = cursor.fetchone()
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
statistics = {
|
statistics = {
|
||||||
'id': item_id,
|
'id': item_id,
|
||||||
@ -58,6 +63,8 @@ class ItemResource:
|
|||||||
|
|
||||||
resp.media = statistics
|
resp.media = statistics
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
api = falcon.API()
|
api = falcon.API()
|
||||||
api.add_route('/items', AllItemsResource())
|
api.add_route('/items', AllItemsResource())
|
||||||
api.add_route('/item/{item_id:int}', ItemResource())
|
api.add_route('/item/{item_id:int}', ItemResource())
|
||||||
|
Loading…
Reference in New Issue
Block a user