2015-12-24 20:34:34 +01:00
|
|
|
"""
|
|
|
|
This file contains GET routes methods.
|
|
|
|
"""
|
|
|
|
import bottle
|
|
|
|
|
|
|
|
import database
|
|
|
|
import tools
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_papers(db):
|
|
|
|
"""
|
|
|
|
Fetch all matching papers.
|
|
|
|
|
2015-12-25 01:43:49 +01:00
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
GET /papers
|
|
|
|
Accept: application/vnd.api+json
|
|
|
|
|
|
|
|
|
|
|
|
Filtering is possible using ``id=ID``, ``doi=DOI``, ``arxiv_id=ARXIV_ID`` \
|
|
|
|
or any combination of these GET parameters. Other parameters are ignored.
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": [
|
|
|
|
{
|
|
|
|
"type": "papers",
|
|
|
|
"id": 1,
|
|
|
|
"attributes": {
|
|
|
|
"doi": "10.1126/science.1252319",
|
|
|
|
"arxiv_id": "1401.2910"
|
|
|
|
},
|
|
|
|
"links": {
|
|
|
|
"self": "/papers/1"
|
|
|
|
},
|
|
|
|
"relationships": {
|
|
|
|
TODO
|
|
|
|
}
|
2015-12-24 20:34:34 +01:00
|
|
|
}
|
2015-12-25 01:43:49 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
:param db: A database session, injected by the ``Bottle`` plugin.
|
|
|
|
:returns: An ``HTTPResponse``.
|
2015-12-24 20:34:34 +01:00
|
|
|
"""
|
|
|
|
filters = {k: bottle.request.params[k]
|
|
|
|
for k in bottle.request.params
|
|
|
|
if k in ["id", "doi", "arxiv_id"]}
|
|
|
|
resources = db.query(database.Paper).filter_by(**filters).all()
|
|
|
|
if resources:
|
2015-12-25 00:56:45 +01:00
|
|
|
return tools.APIResponse(tools.pretty_json({
|
2015-12-24 20:34:34 +01:00
|
|
|
"data": [resource.json_api_repr() for resource in resources]
|
2015-12-25 00:56:45 +01:00
|
|
|
}))
|
2015-12-24 20:34:34 +01:00
|
|
|
return bottle.HTTPError(404, "Not found")
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_by_id(id, db):
|
|
|
|
"""
|
|
|
|
Fetch a resource identified by its internal id.
|
|
|
|
|
2015-12-25 01:43:49 +01:00
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
GET /id/<id>
|
|
|
|
Accept: application/vnd.api+json
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
{
|
|
|
|
"type": "papers",
|
|
|
|
"id": 1,
|
|
|
|
"attributes": {
|
|
|
|
"doi": "10.1126/science.1252319",
|
|
|
|
"arxiv_id": "1401.2910"
|
|
|
|
},
|
|
|
|
"links": {
|
|
|
|
"self": "/papers/1"
|
|
|
|
},
|
|
|
|
"relationships": {
|
|
|
|
TODO
|
|
|
|
}
|
2015-12-24 20:34:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-25 01:43:49 +01:00
|
|
|
|
|
|
|
:param id: The id of the requested article.
|
|
|
|
:param db: A database session, injected by the ``Bottle`` plugin.
|
|
|
|
:returns: An ``HTTPResponse``.
|
2015-12-24 20:34:34 +01:00
|
|
|
"""
|
|
|
|
resource = db.query(database.Paper).filter_by(id=id).first()
|
|
|
|
if resource:
|
2015-12-25 00:56:45 +01:00
|
|
|
return tools.APIResponse(tools.pretty_json({
|
2015-12-24 20:34:34 +01:00
|
|
|
"data": resource.json_api_repr()
|
2015-12-25 00:56:45 +01:00
|
|
|
}))
|
2015-12-24 20:34:34 +01:00
|
|
|
return bottle.HTTPError(404, "Not found")
|
2015-12-25 20:09:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
def fetch_relationship(id, name, db):
|
|
|
|
"""
|
|
|
|
TODO
|
|
|
|
"""
|
|
|
|
pass
|