| 1 |
from typing import Any, Dict, List, Optional, TypedDict, Union |
| 2 |
|
| 3 |
from rdflib import Namespace |
| 4 |
|
| 5 |
SD = Namespace("http://www.w3.org/ns/sparql-service-description#") |
| 6 |
FORMATS = Namespace("http://www.w3.org/ns/formats/") |
| 7 |
|
| 8 |
|
| 9 |
class Defaults: |
| 10 |
"""Default configuration values for the SPARQL endpoint.""" |
| 11 |
|
| 12 |
title: str = "SPARQL endpoint for RDFLib graph" |
| 13 |
description: str = "A SPARQL endpoint to serve machine learning models, or any other logic implemented in Python. \n[Source code](https://github.com/vemonet/rdflib-endpoint)" |
| 14 |
version: str = "0.1.0" |
| 15 |
public_url: str = "https://your-endpoint/sparql" |
| 16 |
favicon: str = "https://rdflib.readthedocs.io/en/stable/_static/RDFlib.png" |
| 17 |
example: str = """\ |
| 18 |
PREFIX myfunctions: <https://w3id.org/sparql-functions/> |
| 19 |
|
| 20 |
SELECT ?concat ?concatLength WHERE { |
| 21 |
BIND("First" AS ?first) |
| 22 |
BIND(myfunctions:custom_concat(?first, "last") AS ?concat) |
| 23 |
} |
| 24 |
""".rstrip() |
| 25 |
#: This is default for federated queries |
| 26 |
content_type: str = "application/xml" |
| 27 |
|
| 28 |
|
| 29 |
API_RESPONSES: Optional[Dict[Union[int, str], Dict[str, Any]]] = { |
| 30 |
200: { |
| 31 |
"description": "SPARQL query results", |
| 32 |
"content": { |
| 33 |
"application/sparql-results+json": {"example": {"results": {"bindings": []}, "head": {"vars": []}}}, |
| 34 |
"application/json": {"example": {"results": {"bindings": []}, "head": {"vars": []}}}, |
| 35 |
"text/csv": {"example": "s,p,o"}, |
| 36 |
"application/sparql-results+csv": {"example": "s,p,o"}, |
| 37 |
"application/sparql-results+xml": {"example": "<root></root>"}, |
| 38 |
"application/xml": {"example": "<root></root>"}, |
| 39 |
"text/turtle": {"example": "<http://subject> <http://predicate> <http://object> ."}, |
| 40 |
"application/n-triples": {"example": "<http://subject> <http://predicate> <http://object> ."}, |
| 41 |
"text/n3": {"example": "<http://subject> <http://predicate> <http://object> ."}, |
| 42 |
"application/n-quads": {"example": "<http://subject> <http://predicate> <http://object> <http://graph> ."}, |
| 43 |
"application/trig": { |
| 44 |
"example": "GRAPH <http://graph> {<http://subject> <http://predicate> <http://object> .}" |
| 45 |
}, |
| 46 |
"application/trix": {"example": "<xml></xml>"}, |
| 47 |
"application/ld+json": { |
| 48 |
"example": [ |
| 49 |
{ |
| 50 |
"@id": "http://subject", |
| 51 |
"@type": ["http://object"], |
| 52 |
"http://www.w3.org/2000/01/rdf-schema#label": [{"@value": "foo"}], |
| 53 |
} |
| 54 |
] |
| 55 |
}, |
| 56 |
# "application/rdf+xml": { |
| 57 |
# "example": '<?xml version="1.0" encoding="UTF-8"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"></rdf:RDF>' |
| 58 |
# }, |
| 59 |
}, |
| 60 |
}, |
| 61 |
400: { |
| 62 |
"description": "Bad Request", |
| 63 |
}, |
| 64 |
403: { |
| 65 |
"description": "Forbidden", |
| 66 |
}, |
| 67 |
422: { |
| 68 |
"description": "Unprocessable Entity", |
| 69 |
}, |
| 70 |
} |
| 71 |
|
| 72 |
#: A mapping from content types to the keys used for serializing |
| 73 |
#: in :meth:`rdflib.Graph.serialize` and other serialization functions |
| 74 |
GRAPH_CONTENT_TYPE_TO_RDFLIB_FORMAT = { |
| 75 |
# https://www.w3.org/TR/rdf-sparql-XMLres/ |
| 76 |
"application/rdf+xml": "xml", # for compatibility |
| 77 |
"application/ld+json": "json-ld", |
| 78 |
# https://www.w3.org/TR/sparql11-results-csv-tsv/ |
| 79 |
# Extras |
| 80 |
"text/turtle": "ttl", |
| 81 |
"text/n3": "n3", |
| 82 |
"application/n-triples": "nt", |
| 83 |
"text/plain": "nt", |
| 84 |
"application/trig": "trig", |
| 85 |
"application/trix": "trix", |
| 86 |
"application/n-quads": "nquads", |
| 87 |
} |
| 88 |
|
| 89 |
SPARQL_RESULT_CONTENT_TYPE_TO_RDFLIB_FORMAT = { |
| 90 |
# https://www.w3.org/TR/sparql11-results-json/ |
| 91 |
"application/sparql-results+json": "json", |
| 92 |
# https://www.w3.org/TR/rdf-sparql-XMLres/ |
| 93 |
"application/sparql-results+xml": "xml", |
| 94 |
# https://www.w3.org/TR/sparql11-results-csv-tsv/ |
| 95 |
"application/sparql-results+csv": "csv", |
| 96 |
} |
| 97 |
|
| 98 |
GENERIC_CONTENT_TYPE_TO_RDFLIB_FORMAT = { |
| 99 |
# https://www.w3.org/TR/sparql11-results-json/ |
| 100 |
"application/json": "json", |
| 101 |
"text/json": "json", |
| 102 |
# https://www.w3.org/TR/rdf-sparql-XMLres/ |
| 103 |
"application/xml": "xml", # for compatibility |
| 104 |
"text/xml": "xml", # not standard |
| 105 |
# https://www.w3.org/TR/sparql11-results-csv-tsv/ |
| 106 |
"text/csv": "csv", # for compatibility |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
def parse_accept_header(accept: str) -> List[str]: |
| 111 |
"""Given an accept header string, return a list of media types in order of preference. |
| 112 |
|
| 113 |
:param accept: Accept header value |
| 114 |
:return: Ordered list of media type preferences |
| 115 |
""" |
| 116 |
|
| 117 |
def _parse_preference(qpref: str) -> float: |
| 118 |
qparts = qpref.split("=") |
| 119 |
try: |
| 120 |
return float(qparts[1].strip()) |
| 121 |
except (ValueError, IndexError): |
| 122 |
pass |
| 123 |
return 1.0 |
| 124 |
|
| 125 |
preferences = [] |
| 126 |
types = accept.split(",") |
| 127 |
dpref = 2.0 |
| 128 |
for mtype in types: |
| 129 |
parts = mtype.split(";") |
| 130 |
parts = [part.strip() for part in parts] |
| 131 |
pref = dpref |
| 132 |
try: |
| 133 |
for part in parts[1:]: |
| 134 |
if part.startswith("q="): |
| 135 |
pref = _parse_preference(part) |
| 136 |
break |
| 137 |
except IndexError: |
| 138 |
pass |
| 139 |
# preserve order of appearance in the list |
| 140 |
dpref = dpref - 0.01 |
| 141 |
preferences.append((parts[0], pref)) |
| 142 |
preferences.sort(key=lambda x: -x[1]) |
| 143 |
return [pref[0] for pref in preferences] |
| 144 |
|
| 145 |
|
| 146 |
class QueryExample(TypedDict, total=False): |
| 147 |
"""Dictionary to store example queries for the SPARQL endpoint.""" |
| 148 |
|
| 149 |
query: str |
| 150 |
endpoint: Optional[str] |