1
0
mirror of https://github.com/ilri/dspace-statistics-api.git synced 2024-06-26 16:13:47 +02:00
dspace-statistics-api/app.py
Alan Orth ea85393b13
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
2018-09-23 16:23:33 +03:00

42 lines
1.0 KiB
Python

# Tested with Python 3.6
# See DSpace Solr docs for tips about parameters
# https://wiki.duraspace.org/display/DSPACE/Solr
from config import SOLR_CORE
import falcon
from solr import solr_connection
class ItemResource:
def on_get(self, req, resp, item_id):
"""Handles GET requests"""
# Get views
res = solr.query(SOLR_CORE, {
'q':'type:2 AND id:{0}'.format(item_id),
'fq':'isBot:false AND statistics_type:view'
}, rows=0)
views = res.get_num_found()
# Get downloads
res = solr.query(SOLR_CORE, {
'q':'type:0 AND owningItem:{0}'.format(item_id),
'fq':'isBot:false AND statistics_type:view AND bundleName:ORIGINAL'
}, rows=0)
downloads = res.get_num_found()
statistics = {
'id': item_id,
'views': views,
'downloads': downloads
}
resp.media = statistics
api = falcon.API()
api.add_route('/item/{item_id:int}', ItemResource())
# vim: set sw=4 ts=4 expandtab: