Skip to content
Snippets Groups Projects
Commit d74eeabe authored by Matthias Stefan Bodenbenner's avatar Matthias Stefan Bodenbenner
Browse files

implemented resolving requests of profiles

parent eebbf91f
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,7 @@ from ..soil.figure import Figure ...@@ -19,6 +19,7 @@ from ..soil.figure import Figure
from ..soil.function import Function from ..soil.function import Function
from ..soil.measurement import Measurement from ..soil.measurement import Measurement
from ..soil.parameter import Parameter from ..soil.parameter import Parameter
from ..soil.semantics import Semantics
from ..soil.stream import StreamScheduler from ..soil.stream import StreamScheduler
from ..utils import root_logger from ..utils import root_logger
from ..utils import serialize from ..utils import serialize
...@@ -125,9 +126,8 @@ class HTTPServer(object): ...@@ -125,9 +126,8 @@ class HTTPServer(object):
return queried_attributes return queried_attributes
def prepare_response(self, body: Union[Dict, rdflib.Graph], element: Element, status: int = 200, def prepare_response(self, body: Union[Dict, rdflib.Graph], element: Element, status: int = 200,
query: MultiDict = None): query: MultiDict = None, semantic: bool = False):
dataformat = self._dataformat dataformat = self._dataformat
semantic = False
if query is not None and 'format' in query and query['format'] in ['json', 'xml', 'turtle']: if query is not None and 'format' in query and query['format'] in ['json', 'xml', 'turtle']:
dataformat = query['format'] dataformat = query['format']
if query is not None and 'semantic' in query and query['semantic'] in ['data', 'metadata', 'profile']: if query is not None and 'semantic' in query and query['semantic'] in ['data', 'metadata', 'profile']:
...@@ -171,6 +171,7 @@ class HTTPServer(object): ...@@ -171,6 +171,7 @@ class HTTPServer(object):
logger.info("GET Request from {}".format(request.url)) logger.info("GET Request from {}".format(request.url))
logger.debug('Request: {}'.format(request)) logger.debug('Request: {}'.format(request))
logger.debug('Query Parameters: {}'.format(request.query_string)) logger.debug('Query Parameters: {}'.format(request.query_string))
splitted_request = HTTPServer.parse_uuids(request)
keys = self._filter_query(request.query) keys = self._filter_query(request.query)
# determine whether a semantic answer is expected # determine whether a semantic answer is expected
...@@ -178,9 +179,11 @@ class HTTPServer(object): ...@@ -178,9 +179,11 @@ class HTTPServer(object):
if request.query is not None and 'semantic' in request.query and request.query['semantic'] in ['data', 'metadata', 'profile']: if request.query is not None and 'semantic' in request.query and request.query['semantic'] in ['data', 'metadata', 'profile']:
semantic = request.query['semantic'] semantic = request.query['semantic']
# retrieving the element if len(splitted_request) > 0 and splitted_request[0] == Semantics.prefix:
item, semantic = self.root.resolve_semantic_path(splitted_request[1:])
else:
try: try:
item = self.root[HTTPServer.parse_uuids(request)] item = self.root[splitted_request]
except KeyError as e: except KeyError as e:
logger.error(traceback.format_exc()) logger.error(traceback.format_exc())
response = {'error': str(e)} response = {'error': str(e)}
...@@ -200,7 +203,7 @@ class HTTPServer(object): ...@@ -200,7 +203,7 @@ class HTTPServer(object):
response = {'error': str(e)} response = {'error': str(e)}
status = 500 status = 500
logger.error('Response: {}'.format(response)) logger.error('Response: {}'.format(response))
return self.prepare_response(response, item, status=status, query=request.query) return self.prepare_response(response, item, status=status, query=request.query, semantic=semantic is not None)
async def post(self, request): async def post(self, request):
logger.info("POST Request from {}".format(request.url)) logger.info("POST Request from {}".format(request.url))
......
...@@ -393,3 +393,26 @@ class Component(Element): ...@@ -393,3 +393,26 @@ class Component(Element):
else: else:
raise DeviceException('The provided kind of semantic information cannot be returned.') raise DeviceException('The provided kind of semantic information cannot be returned.')
return result return result
def resolve_semantic_path(self, path: List[str]):
if len(path) == 0:
raise ChildNotFoundException('Could not resolve the semantic path.')
# first try to resolve whether it is a path to a profile
if path[0] == f'{self._profilename}Shape':
if len(path) == 1:
return self, 'profile'
else:
for child in self.children:
try:
return child.resolve_semantic_path(path)
except ChildNotFoundException:
continue
# TODO resolve data or metadata terms
raise ChildNotFoundException('Could not resolve the semantic path.')
...@@ -5,6 +5,7 @@ from typing import Any, Dict, List ...@@ -5,6 +5,7 @@ from typing import Any, Dict, List
import rdflib import rdflib
from .error import ChildNotFoundException
from ..utils.constants import BASE_UUID_PATTERN, HTTP_GET from ..utils.constants import BASE_UUID_PATTERN, HTTP_GET
from ..utils.error import SerialisationException from ..utils.error import SerialisationException
...@@ -110,3 +111,14 @@ class Element(ABC): ...@@ -110,3 +111,14 @@ class Element(ABC):
@abstractmethod @abstractmethod
def serialize_semantics(self, kind: str) -> rdflib.Graph: def serialize_semantics(self, kind: str) -> rdflib.Graph:
... ...
def resolve_semantic_path(self, path: List[str]) -> ('Element', str):
if len(path) == 0:
raise ChildNotFoundException('Could not resolve the semantic path.')
if path[0] == f'{self._profilename}Shape' and len(path) == 1:
return self, 'profile'
else:
raise ChildNotFoundException('Could not resolve the semantic path.')
# TODO resolve data or metadata terms
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment