2018-10-26 18:14:27 +02:00
|
|
|
from .database import database_connection
|
2018-09-18 13:03:15 +02:00
|
|
|
import falcon
|
|
|
|
|
2018-09-24 23:49:47 +02:00
|
|
|
db = database_connection()
|
|
|
|
db.set_session(readonly=True)
|
2018-09-18 13:03:15 +02:00
|
|
|
|
2018-11-03 22:55:23 +01:00
|
|
|
|
2018-10-31 23:19:39 +01:00
|
|
|
class RootResource:
|
|
|
|
def on_get(self, req, resp):
|
|
|
|
resp.status = falcon.HTTP_200
|
|
|
|
resp.content_type = 'text/html'
|
|
|
|
with open('dspace_statistics_api/docs/index.html', 'r') as f:
|
|
|
|
resp.body = f.read()
|
|
|
|
|
2018-11-03 22:55:23 +01:00
|
|
|
|
2018-09-24 15:07:26 +02:00
|
|
|
class AllItemsResource:
|
|
|
|
def on_get(self, req, resp):
|
|
|
|
"""Handles GET requests"""
|
|
|
|
# Return HTTPBadRequest if id parameter is not present and valid
|
|
|
|
limit = req.get_param_as_int("limit", min=0, max=100) or 100
|
|
|
|
page = req.get_param_as_int("page", min=0) or 0
|
|
|
|
offset = limit * page
|
|
|
|
|
|
|
|
cursor = db.cursor()
|
|
|
|
|
|
|
|
# get total number of items so we can estimate the pages
|
|
|
|
cursor.execute('SELECT COUNT(id) FROM items')
|
|
|
|
pages = round(cursor.fetchone()[0] / limit)
|
|
|
|
|
|
|
|
# get statistics, ordered by id, and use limit and offset to page through results
|
2018-09-25 01:19:29 +02:00
|
|
|
cursor.execute('SELECT id, views, downloads FROM items ORDER BY id ASC LIMIT {} OFFSET {}'.format(limit, offset))
|
2018-09-24 15:07:26 +02:00
|
|
|
|
|
|
|
# create a list to hold dicts of item stats
|
|
|
|
statistics = list()
|
|
|
|
|
|
|
|
# iterate over results and build statistics object
|
2018-09-27 10:03:44 +02:00
|
|
|
for item in cursor:
|
2018-11-03 22:55:23 +01:00
|
|
|
statistics.append({'id': item['id'], 'views': item['views'], 'downloads': item['downloads']})
|
2018-09-24 15:07:26 +02:00
|
|
|
|
2018-09-27 10:03:44 +02:00
|
|
|
cursor.close()
|
|
|
|
|
2018-09-24 15:07:26 +02:00
|
|
|
message = {
|
2018-11-03 22:55:23 +01:00
|
|
|
'currentPage': page,
|
|
|
|
'totalPages': pages,
|
|
|
|
'limit': limit,
|
|
|
|
'statistics': statistics
|
2018-09-24 15:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.media = message
|
|
|
|
|
2018-11-03 22:55:23 +01:00
|
|
|
|
2018-09-18 13:03:15 +02:00
|
|
|
class ItemResource:
|
2018-09-23 15:23:33 +02:00
|
|
|
def on_get(self, req, resp, item_id):
|
2018-09-18 13:03:15 +02:00
|
|
|
"""Handles GET requests"""
|
|
|
|
|
2018-09-23 15:47:00 +02:00
|
|
|
cursor = db.cursor()
|
2018-09-25 01:19:29 +02:00
|
|
|
cursor.execute('SELECT views, downloads FROM items WHERE id={}'.format(item_id))
|
2018-09-25 12:12:53 +02:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise falcon.HTTPNotFound(
|
2018-11-03 22:55:23 +01:00
|
|
|
title='Item not found',
|
|
|
|
description='The item with id "{}" was not found.'.format(item_id)
|
2018-09-25 12:12:53 +02:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
results = cursor.fetchone()
|
2018-09-18 13:03:15 +02:00
|
|
|
|
2018-09-25 12:12:53 +02:00
|
|
|
statistics = {
|
|
|
|
'id': item_id,
|
|
|
|
'views': results['views'],
|
|
|
|
'downloads': results['downloads']
|
|
|
|
}
|
2018-09-18 13:03:15 +02:00
|
|
|
|
2018-09-25 12:12:53 +02:00
|
|
|
resp.media = statistics
|
|
|
|
|
|
|
|
cursor.close()
|
2018-09-18 13:03:15 +02:00
|
|
|
|
2018-11-03 22:55:23 +01:00
|
|
|
|
2018-10-26 18:21:27 +02:00
|
|
|
api = application = falcon.API()
|
2018-10-31 23:19:39 +01:00
|
|
|
api.add_route('/', RootResource())
|
2018-09-25 10:34:07 +02:00
|
|
|
api.add_route('/items', AllItemsResource())
|
2018-09-23 15:23:33 +02:00
|
|
|
api.add_route('/item/{item_id:int}', ItemResource())
|
2018-09-23 10:33:26 +02:00
|
|
|
|
|
|
|
# vim: set sw=4 ts=4 expandtab:
|