Update doc

This commit is contained in:
Lucas Verney 2015-12-25 00:56:45 +01:00
parent 357873d10c
commit 6fc337095d
3 changed files with 52 additions and 90 deletions

View File

@ -26,13 +26,14 @@ def fetch_papers(db):
"type": "papers", "type": "papers",
"id": 1, "id": 1,
"attributes": { "attributes": {
"doi": "TODO", "doi": "10.1126/science.1252319",
"arxiv_id": "TODO" "arxiv_id": "1401.2910"
}, },
"links": { "links": {
"self": "TODO" "self": "/papers/1"
}, },
"relationships": { "relationships": {
TODO
} }
} }
] ]
@ -44,9 +45,9 @@ def fetch_papers(db):
if k in ["id", "doi", "arxiv_id"]} if k in ["id", "doi", "arxiv_id"]}
resources = db.query(database.Paper).filter_by(**filters).all() resources = db.query(database.Paper).filter_by(**filters).all()
if resources: if resources:
return tools.pretty_json({ return tools.APIResponse(tools.pretty_json({
"data": [resource.json_api_repr() for resource in resources] "data": [resource.json_api_repr() for resource in resources]
}) }))
return bottle.HTTPError(404, "Not found") return bottle.HTTPError(404, "Not found")
@ -66,13 +67,14 @@ def fetch_by_id(id, db):
"type": "papers", "type": "papers",
"id": 1, "id": 1,
"attributes": { "attributes": {
"doi": "TODO", "doi": "10.1126/science.1252319",
"arxiv_id": "TODO" "arxiv_id": "1401.2910"
}, },
"links": { "links": {
"self": "TODO" "self": "/papers/1"
}, },
"relationships": { "relationships": {
TODO
} }
} }
} }
@ -81,81 +83,7 @@ def fetch_by_id(id, db):
""" """
resource = db.query(database.Paper).filter_by(id=id).first() resource = db.query(database.Paper).filter_by(id=id).first()
if resource: if resource:
return tools.pretty_json({ return tools.APIResponse(tools.pretty_json({
"data": resource.json_api_repr() "data": resource.json_api_repr()
}) }))
return bottle.HTTPError(404, "Not found")
def fetch_by_doi(doi, db):
"""
Fetch a resource identified by its DOI.
```
GET /doi/<DOI>
Accept: application/vnd.api+json
```
```
{
"data": {
{
"type": "papers",
"id": 1,
"attributes": {
"doi": "TODO",
"arxiv_id": "TODO"
},
"links": {
"self": "TODO"
},
"relationships": {
}
}
}
}
```
"""
resource = db.query(database.Paper).filter_by(doi=doi).first()
if resource:
return tools.pretty_json({
"data": resource.json_api_repr()
})
return bottle.HTTPError(404, "Not found")
def fetch_by_arxiv(arxiv, db):
"""
Fetch a resource identified by its arXiv eprint ID.
```
GET /arxiv/<arxiv_eprint_id>
Accept: application/vnd.api+json
```
```
{
"data": {
{
"type": "papers",
"id": 1,
"attributes": {
"doi": "TODO",
"arxiv_id": "TODO"
},
"links": {
"self": "TODO"
},
"relationships": {
}
}
}
}
```
"""
resource = db.query(database.Paper).filter_by(arxiv_id=arxiv).first()
if resource:
return tools.pretty_json({
"data": resource.json_api_repr()
})
return bottle.HTTPError(404, "Not found") return bottle.HTTPError(404, "Not found")

View File

@ -21,15 +21,32 @@ def create_paper(db):
{ {
"data": { "data": {
"doi": "DOI", "doi": "10.1126/science.1252319",
// OR // OR
"arxiv_id": "ARXIV_ID" "arxiv_id": "1401.2910"
} }
} }
``` ```
``` ```
{} TODO {
"data": {
{
"type": "papers",
"id": 1,
"attributes": {
"doi": "10.1126/science.1252319",
"arxiv_id": "1401.2910"
},
"links": {
"self": "/papers/1"
},
"relationships": {
TODO
}
}
}
}
``` ```
""" """
data = json.loads(bottle.request.body.read().decode("utf-8")) data = json.loads(bottle.request.body.read().decode("utf-8"))
@ -54,10 +71,12 @@ def create_paper(db):
response = { response = {
"data": paper.json_api_repr() "data": paper.json_api_repr()
} }
# Note: Return a 202 as the resource has been accepted but is not yet # TODO: Return a 202 as the resource has been accepted but is not yet
# processed, especially since its relationships have not yet been fetched. # processed, especially since its relationships have not yet been fetched.
# TODO: Redirection headers = {"Location": "/papers/%d" % (paper.id,)}
return bottle.HTTPResponse(status=202, body=tools.pretty_json(response)) return tools.APIResponse(status=202,
body=tools.pretty_json(response),
headers=headers)
def create_by_doi(doi, db): def create_by_doi(doi, db):

View File

@ -1,6 +1,7 @@
""" """
Various utility functions. Various utility functions.
""" """
import bottle
import json import json
@ -12,3 +13,17 @@ def pretty_json(data):
sort_keys=True, sort_keys=True,
indent=4, indent=4,
separators=(',', ': ')) separators=(',', ': '))
class APIResponse(bottle.HTTPResponse):
"""
Extend bottle.HTTPResponse base class to add Content-Type header.
"""
def __init__(self, body='', status=None, headers=None, **more_headers):
if headers is None:
headers = {}
headers["Content-Type"] = "application/vnd.api+json"
super(APIResponse, self).__init__(body,
status,
headers,
**more_headers)