|
@@ -3,7 +3,6 @@
|
|
|
import logging
|
|
import logging
|
|
|
import re
|
|
import re
|
|
|
import arklog
|
|
import arklog
|
|
|
-import pkg_resources
|
|
|
|
|
import rdflib
|
|
import rdflib
|
|
|
from typing import Any, Callable, Dict, List, Optional, Union
|
|
from typing import Any, Callable, Dict, List, Optional, Union
|
|
|
from urllib import parse
|
|
from urllib import parse
|
|
@@ -43,13 +42,11 @@ class SparqlEndpoint(FastAPI):
|
|
|
return mime.split(",")[0] in ("text/turtle",)
|
|
return mime.split(",")[0] in ("text/turtle",)
|
|
|
|
|
|
|
|
async def requested_result_type(self, request: Request, operation: str) -> str:
|
|
async def requested_result_type(self, request: Request, operation: str) -> str:
|
|
|
- logging.debug("Getting mime type.")
|
|
|
|
|
output_mime_type = request.headers["accept"]
|
|
output_mime_type = request.headers["accept"]
|
|
|
# TODO Ugly hack, fix later (Fuseki sends options)
|
|
# TODO Ugly hack, fix later (Fuseki sends options)
|
|
|
output_mime_type = output_mime_type.split(",")[0]
|
|
output_mime_type = output_mime_type.split(",")[0]
|
|
|
if isinstance(output_mime_type, list):
|
|
if isinstance(output_mime_type, list):
|
|
|
return output_mime_type[0]
|
|
return output_mime_type[0]
|
|
|
-
|
|
|
|
|
# TODO Use match or dict for this
|
|
# TODO Use match or dict for this
|
|
|
if not output_mime_type:
|
|
if not output_mime_type:
|
|
|
logging.warning("No mime type provided. Setting mimetype to 'application/xml'.")
|
|
logging.warning("No mime type provided. Setting mimetype to 'application/xml'.")
|
|
@@ -68,6 +65,7 @@ class SparqlEndpoint(FastAPI):
|
|
|
self.description = description
|
|
self.description = description
|
|
|
self.version = version
|
|
self.version = version
|
|
|
super().__init__(*args, title=title, description=description, version=version, **kwargs)
|
|
super().__init__(*args, title=title, description=description, version=version, **kwargs)
|
|
|
|
|
+ logging.debug(self.description)
|
|
|
rdflib.plugins.sparql.CUSTOM_EVALS["evalCustomFunctions"] = self.eval_custom_functions
|
|
rdflib.plugins.sparql.CUSTOM_EVALS["evalCustomFunctions"] = self.eval_custom_functions
|
|
|
api_responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = {
|
|
api_responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = {
|
|
|
200: {
|
|
200: {
|
|
@@ -86,9 +84,6 @@ class SparqlEndpoint(FastAPI):
|
|
|
"text/turtle": {"example": "service description"},
|
|
"text/turtle": {"example": "service description"},
|
|
|
"application/sparql-results+xml": {"example": "<root></root>"},
|
|
"application/sparql-results+xml": {"example": "<root></root>"},
|
|
|
"application/xml": {"example": "<root></root>"},
|
|
"application/xml": {"example": "<root></root>"},
|
|
|
- # "application/rdf+xml": {
|
|
|
|
|
- # "example": '<?xml version="1.0" encoding="UTF-8"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"></rdf:RDF>'
|
|
|
|
|
- # },
|
|
|
|
|
},
|
|
},
|
|
|
},
|
|
},
|
|
|
400: {
|
|
400: {
|
|
@@ -127,12 +122,11 @@ class SparqlEndpoint(FastAPI):
|
|
|
query_results = self.graph.query(query, initNs=graph_ns)
|
|
query_results = self.graph.query(query, initNs=graph_ns)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logging.error("Error executing the SPARQL query on the RDFLib Graph: " + str(e))
|
|
logging.error("Error executing the SPARQL query on the RDFLib Graph: " + str(e))
|
|
|
|
|
+ # TODO Send better error which can be parsed as a SPARQL response or check it client side
|
|
|
return JSONResponse(
|
|
return JSONResponse(
|
|
|
status_code=400,
|
|
status_code=400,
|
|
|
content={"message": "Error executing the SPARQL query on the RDFLib Graph"},
|
|
content={"message": "Error executing the SPARQL query on the RDFLib Graph"},
|
|
|
)
|
|
)
|
|
|
-
|
|
|
|
|
- logging.debug(f"{type(query_results)=}")
|
|
|
|
|
output_mime_type = await self.requested_result_type(request, query_operation)
|
|
output_mime_type = await self.requested_result_type(request, query_operation)
|
|
|
logging.debug(f"Returning {output_mime_type}.")
|
|
logging.debug(f"Returning {output_mime_type}.")
|
|
|
try:
|
|
try:
|
|
@@ -162,13 +156,6 @@ class SparqlEndpoint(FastAPI):
|
|
|
query = parse.unquote(params[1])
|
|
query = parse.unquote(params[1])
|
|
|
return await sparql_endpoint_get(request, query)
|
|
return await sparql_endpoint_get(request, query)
|
|
|
|
|
|
|
|
- @self.get("/gui", include_in_schema=False)
|
|
|
|
|
- async def serve_yasgui() -> Response:
|
|
|
|
|
- """Serve YASGUI interface"""
|
|
|
|
|
- with open(pkg_resources.resource_filename("spendpoint", "yasgui.html")) as f:
|
|
|
|
|
- html_str = f.read()
|
|
|
|
|
- html_str = html_str.replace("$EXAMPLE_QUERY", "")
|
|
|
|
|
- return Response(content=html_str, media_type="text/html")
|
|
|
|
|
|
|
|
|
|
def eval_custom_functions(self, ctx: QueryContext, part: CompValue) -> List[Any]:
|
|
def eval_custom_functions(self, ctx: QueryContext, part: CompValue) -> List[Any]:
|
|
|
if part.name != "Extend":
|
|
if part.name != "Extend":
|