Skip to content

Commit

Permalink
Merge pull request #225 from AKSW/feature/json-ld
Browse files Browse the repository at this point in the history
Add JSON-LD seriallization
  • Loading branch information
white-gecko authored Apr 17, 2019
2 parents 79293ca + 6098c0a commit 1a81c33
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
44 changes: 44 additions & 0 deletions quit/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,54 @@ def sequencePathCompareGt(self, other):
'sparql', UpdateProcessor,
'quit.tools.processor', 'SPARQLUpdateProcessor')

register(
'application/x-turtle', Serializer,
'rdflib.plugins.serializers.turtle', 'TurtleSerializer')

register(
'application/xml', Serializer,
'rdflib.plugins.serializers.rdfxml', 'XMLSerializer')

register(
'application/json', Serializer,
'rdflib_jsonld.serializer', 'JsonLDSerializer')

register(
'application/trig', Serializer,
'rdflib.plugins.serializers.trig', 'TrigSerializer')

register(
'application/xml', ResultSerializer,
'rdflib.plugins.sparql.results.xmlresults', 'XMLResultSerializer')

register(
'application/json', ResultSerializer,
'rdflib.plugins.sparql.results.jsonresults', 'JSONResultSerializer')

register(
'html', ResultSerializer,
'quit.plugins.serializers.results.htmlresults', 'HTMLResultSerializer')

register(
'text/html', ResultSerializer,
'quit.plugins.serializers.results.htmlresults', 'HTMLResultSerializer')

register(
'application/xhtml+xml', ResultSerializer,
'quit.plugins.serializers.results.htmlresults', 'HTMLResultSerializer')

register(
'text/csv', ResultSerializer,
'rdflib.plugins.sparql.results.csvresults', 'CSVResultSerializer')

register(
'application/sparql-results+xml', ResultSerializer,
'rdflib.plugins.sparql.results.xmlresults', 'XMLResultSerializer')

register(
'application/sparql-results+json', ResultSerializer,
'rdflib.plugins.sparql.results.jsonresults', 'JSONResultSerializer')

try:
config = QuitStoreConfiguration(
configfile=args.configfile,
Expand Down
27 changes: 6 additions & 21 deletions quit/web/modules/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
__all__ = ('endpoint')

endpoint = Blueprint('endpoint', __name__)

resultSetMimetypesDefault = 'application/sparql-results+xml'
askMimetypesDefault = 'application/sparql-results+xml'
rdfMimetypesDefault = 'text/turtle'
Expand All @@ -30,22 +29,8 @@
'application/sparql-results+json', 'application/json', 'text/html',
'application/xhtml+xml']
rdfMimetypes = ['text/turtle', 'application/x-turtle', 'application/rdf+xml', 'application/xml',
'application/n-triples', 'application/trig']

serializations = {
'text/turtle': 'turtle',
'application/x-turtle': 'turtle',
'text/csv': 'csv',
'text/html': 'html',
'application/xhtml+xml': 'html',
'application/sparql-results+xml': 'xml',
'application/xml': 'xml',
'application/rdf+xml': 'xml',
'application/sparql-results+json': 'json',
'application/json': 'json',
'application/n-triples': 'nt',
'application/trig': 'trig'
}
'application/n-triples', 'application/trig', 'application/ld+json',
'application/json']


@endpoint.route("/sparql", defaults={'branch_or_ref': None}, methods=['POST', 'GET'])
Expand Down Expand Up @@ -183,7 +168,7 @@ def sparql(branch_or_ref):
if not mimetype:
return make_response("Mimetype: {} not acceptable".format(mimetype), 406)

response = create_result_response(res, mimetype, serializations[mimetype])
response = create_result_response(res, mimetype)
if branch_or_ref:
response.headers["X-CurrentBranch"] = branch_or_ref
if commitid:
Expand Down Expand Up @@ -234,7 +219,7 @@ def provenance():
if not mimetype:
return make_response("Mimetype: {} not acceptable".format(mimetype), 406)

return create_result_response(res, mimetype, serializations[mimetype])
return create_result_response(res, mimetype)
else:
if request.accept_mimetypes.best_match(['text/html']) == 'text/html':
return render_template('sparql.html', mode='provenance')
Expand All @@ -260,9 +245,9 @@ def _getBestMatchingMimeType(request, queryType):
return mimetype


def create_result_response(res, mimetype, serialization):
def create_result_response(res, mimetype):
"""Create a response with the requested serialization."""
response = make_response(res.serialize(format=serialization), 200)
response = make_response(res.serialize(format=mimetype), 200)
response.headers['Content-Type'] = mimetype
return response

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Flask-Cors
pygit2==0.28.0
sortedcontainers
uritools
git+https://github.com/RDFLib/rdflib-jsonld@master

uwsgi
pyinstaller>=3.4
Expand Down
2 changes: 2 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,8 @@ def testContentNegotiation(self):
'application/xml': 'application/xml',
'application/n-triples': 'application/n-triples',
'application/trig': 'application/trig',
'application/json': 'application/json',
'application/ld+json': 'application/ld+json',
'foo/bar,text/turtle;q=0.5': 'text/turtle'}]}

for ep_path in ['/sparql', '/provenance']:
Expand Down

0 comments on commit 1a81c33

Please sign in to comment.