From 6fc337095d1dc0d089cd0cab516a202e2b8b5bd4 Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Fri, 25 Dec 2015 00:56:45 +0100 Subject: [PATCH] Update doc --- routes/get.py | 96 +++++++------------------------------------------- routes/post.py | 31 ++++++++++++---- tools.py | 15 ++++++++ 3 files changed, 52 insertions(+), 90 deletions(-) diff --git a/routes/get.py b/routes/get.py index a74c319..3411a80 100644 --- a/routes/get.py +++ b/routes/get.py @@ -26,13 +26,14 @@ def fetch_papers(db): "type": "papers", "id": 1, "attributes": { - "doi": "TODO", - "arxiv_id": "TODO" + "doi": "10.1126/science.1252319", + "arxiv_id": "1401.2910" }, "links": { - "self": "TODO" + "self": "/papers/1" }, "relationships": { + TODO } } ] @@ -44,9 +45,9 @@ def fetch_papers(db): if k in ["id", "doi", "arxiv_id"]} resources = db.query(database.Paper).filter_by(**filters).all() if resources: - return tools.pretty_json({ + return tools.APIResponse(tools.pretty_json({ "data": [resource.json_api_repr() for resource in resources] - }) + })) return bottle.HTTPError(404, "Not found") @@ -66,13 +67,14 @@ def fetch_by_id(id, db): "type": "papers", "id": 1, "attributes": { - "doi": "TODO", - "arxiv_id": "TODO" + "doi": "10.1126/science.1252319", + "arxiv_id": "1401.2910" }, "links": { - "self": "TODO" + "self": "/papers/1" }, "relationships": { + TODO } } } @@ -81,81 +83,7 @@ def fetch_by_id(id, db): """ resource = db.query(database.Paper).filter_by(id=id).first() if resource: - return tools.pretty_json({ + return tools.APIResponse(tools.pretty_json({ "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/ - 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/ - 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") diff --git a/routes/post.py b/routes/post.py index 9ff4284..577047b 100644 --- a/routes/post.py +++ b/routes/post.py @@ -21,15 +21,32 @@ def create_paper(db): { "data": { - "doi": "DOI", + "doi": "10.1126/science.1252319", // 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")) @@ -54,10 +71,12 @@ def create_paper(db): response = { "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. - # TODO: Redirection - return bottle.HTTPResponse(status=202, body=tools.pretty_json(response)) + headers = {"Location": "/papers/%d" % (paper.id,)} + return tools.APIResponse(status=202, + body=tools.pretty_json(response), + headers=headers) def create_by_doi(doi, db): diff --git a/tools.py b/tools.py index b42e1c5..23b5d4e 100644 --- a/tools.py +++ b/tools.py @@ -1,6 +1,7 @@ """ Various utility functions. """ +import bottle import json @@ -12,3 +13,17 @@ def pretty_json(data): sort_keys=True, indent=4, 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)