Skip to content

Commit

Permalink
Various small fixes
Browse files Browse the repository at this point in the history
- Added a decorator to make sure parameters are used for caching (See #74)
- Added a _external parameter to make URIs relative or absolute URIs (See #73)
  • Loading branch information
PonteIneptique committed May 8, 2018
1 parent f5cb758 commit 753a61f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
23 changes: 23 additions & 0 deletions capitains_nautilus/apis/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
from flask import request


def query_parameters_as_kwargs(params, mapping=None):
""" Default decorator to turn a dictionary of parameters into routes parameters
"""
if not mapping:
mapping = {}

def function_decorator(function):
def wrapper(*args):
kwargs = {}
for argument, default_value in params.items():

kwargs[mapping.get(argument, argument)] = \
request.args.get(argument, default_value)

return function(*args, **kwargs)

return wrapper
return function_decorator


class AdditionalAPIPrototype:
""" Additional APIs classes are used to connect new
APIs to the Nautilus Flask Extensions
Expand Down
69 changes: 45 additions & 24 deletions capitains_nautilus/apis/dts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from collections import defaultdict
import json

from capitains_nautilus.apis.base import AdditionalAPIPrototype
from capitains_nautilus.apis.base import AdditionalAPIPrototype, \
query_parameters_as_kwargs
from capitains_nautilus.errors import NautilusError


Expand Down Expand Up @@ -76,7 +77,7 @@ def _metadata(triples):
return output


def _base_description(collection):
def _base_description(collection, _external=False):
j = {
"@id": collection.id,
"@type": _resource_type(collection),
Expand All @@ -86,8 +87,8 @@ def _base_description(collection):
if hasattr(collection, "get_description"):
j["description"] = collection.get_description()
if collection.readable:
j["dts:passage"] = url_for(".dts_document", id=collection.id, _external=True)
j["dts:references"] = url_for(".dts_navigation", id=collection.id, _external=True)
j["dts:passage"] = url_for(".dts_document", id=collection.id, _external=_external)
j["dts:references"] = url_for(".dts_navigation", id=collection.id, _external=_external)
return j


Expand All @@ -112,6 +113,10 @@ class DTSApi(AdditionalAPIPrototype):
"r_dts_main"
]

def __init__(self, _external=False):
super(DTSApi, self).__init__()
self._external = _external

def dts_error(self, error_name, message=None):
""" Create a DTS Error reply
Expand All @@ -135,18 +140,23 @@ def r_dts_main(self):
"@id": "dts/",
"@type": "EntryPoint",

"collections": url_for(".dts_collection", _external=True),
"documents": "dts/documents",
"navigation": "dts/navigation"
"collections": url_for(".dts_collection", _external=self._external),
"documents": url_for(".dts_document", _external=self._external),
"navigation": url_for(".dts_navigation", _external=self._external)
})

def r_dts_collection(self):
@query_parameters_as_kwargs(
mapping={"id": "objectId", "nav": "direction"},
params={
"id": None,
"nav": "children"
}
)
def r_dts_collection(self, objectId, direction):
""" DTS Collection Metadata reply for given objectId
:return: JSON Format of DTS Collection
"""
objectId = request.args.get("id", None)
direction = request.args.get("nav", "children")

try:
collection = self.resolver.getMetadata(objectId=objectId)
Expand All @@ -161,11 +171,11 @@ def r_dts_collection(self):
"tei": "http://www.tei-c.org/ns/1.0",
},
"members": [
_base_description(member)
_base_description(member, _external=self._external)
for member in _nav_direction(collection, direction)
]
}
j.update(_base_description(collection))
j.update(_base_description(collection, _external=self._external))
j.update(_dc_dictionary(triples))
j.update(_metadata(triples))

Expand All @@ -175,12 +185,17 @@ def r_dts_collection(self):
j.status_code = 200
return j

def r_dts_navigation(self):
objectId = request.args.get("id", None)
passageId = request.args.get("passage", None)
start = request.args.get("start", None)
end = request.args.get("end", None)
level = request.args.get("level", 1)
@query_parameters_as_kwargs(
mapping={"id": "objectId", "passage": "passageId"},
params={
"id": None,
"passage": None,
"start": None,
"end": None,
"level": 1
}
)
def r_dts_navigation(self, objectId=None, passageId=None, start=None, end=None, level=1):
if not objectId:
raise Exception()
if start and end:
Expand All @@ -201,16 +216,22 @@ def r_dts_navigation(self):
"@context": {
"passage": "https://w3id.org/dts/api#passage"
},
"@base": url_for(".dts_document", _external=True),
"@base": url_for(".dts_document", _external=self._external),
"@id": objectId,
"passage": references
})

def r_dts_document(self):
objectId = request.args.get("id", None)
passageId = request.args.get("passage", None)
start = request.args.get("start", None)
end = request.args.get("end", None)
@query_parameters_as_kwargs(
mapping={"id": "objectId", "passage": "passageId"},
params={
"id": None,
"passage": None,
"start": None,
"end": None
}
)
def r_dts_document(self, objectId=None, passageId=None, start=None, end=None):

if not objectId:
raise Exception()
if start and end:
Expand Down

0 comments on commit 753a61f

Please sign in to comment.