Daniel
Daniel

Reputation: 107

REST API for SPARQL in Django (Python)?

Introduction

The challenge I bring to you today is: To implement a Real Rime REST API (GET, POST, PUT, DELETE, etc) to query and update any SPARQL endpoint using the Django REST Framework for a frontend application (I am using React) to request and use the serialized data provided by the REST API.

Please note that I'm using Django because I would like to implement Web AND Mobile applications in the future, but for now I will just implement it on a React Web application.

Specifications

The REST API should be able to:

ALL OF THIS while performing all queries and updates In Real Time.

What I mean with a Real Time API:

  1. A SPARQL query is executed from the REST API to a SPARQL endpoint via an HTTP request.
  2. The REST API reads the HTTP response generated from the request.
  3. The REST API serializes the response to the corresponding format.
  4. This serialized response is stored locally in a Python object for future use.

(Note: All the triples from the SPARQL endpoint in the query now exist both in the SPARQL endpoint as well as in a Python object, and are consistent both locally and remotely.)

  1. The triples are then (hypothetically) modified or updated (Either locally or remotely).
  2. Now the local triples are out of synch with the remote triples.
  3. The REST API now becomes aware of this update (maybe through Listener/Observer objects?).
  4. The REST API then automatically synchs the triples, either through an update query request (if the changes were made locally) or by updating the Python object with the response from a query request (if the update was made remotely).
  5. Finally, both (the SPARQL endpoint and the Python object) should share the latest updated triples and, therefore, be in synch.

Previous Attempts

I have currently been able to query a SPARQL endpoint using the SPARQLWrapper package (for executing the queries), and the RDFLib and JSON packages for serializing and instantiating Python objects from the response, like this:

import json

from rdflib import RDFS, Graph

from SPARQLWrapper import GET, JSON, JSONLD, POST, TURTLE, SPARQLWrapper


class Store(object):
    def __init__(self, query_endpoint, update_endpoint=None):
        self.query_endpoint = query_endpoint
        self.update_endpoint = update_endpoint
        self.sparql = SPARQLWrapper(query_endpoint, update_endpoint)

    def graph_query(self, query: str, format=JSONLD, only_conneg=True):
        results = self.query(query, format, only_conneg) 
        results_bytes = results.serialize(format=format)
        results_json = results_bytes.decode('utf8').replace("'", '"')
        data = json.loads(results_json)
        return data

    def query(self, query: str, format=JSON, only_conneg=True):
        self.sparql.resetQuery()
        self.sparql.setMethod(GET)
        self.sparql.setOnlyConneg(only_conneg)
        self.sparql.setQuery(query)
        self.sparql.setReturnFormat(format)
        return self.sparql.queryAndConvert()

    def update_query(self, query: str, only_conneg=True):
        self.sparql.resetQuery()
        self.sparql.setMethod(POST)
        self.sparql.setOnlyConneg(only_conneg)
        self.sparql.setQuery(query)
        self.sparql.query()


store = Store('http://www.example.com/sparql/Example')
print(store.query("""SELECT ?s WHERE {?s ?p ?o} LIMIT 1"""))
print(store.graph_query("""DESCRIBE <http://www.example.com/sparql/Example/>"""))

The Challenge

The previous code solves can already:

But still fails to implement these other aspects:

And last, but not least, it fails completely to implement the real time aspect of this challenge.

The Questions:

Thank you so much!

Upvotes: 5

Views: 1257

Answers (1)

Nicholas Car
Nicholas Car

Reputation: 1251

Sorry but I don't know anything much about Django so can't answer here with Django specifics.

However, I can say this: SPARQL has a specification for HTTP interactions (https://www.w3.org/TR/sparql11-protocol/) and it tells you to use sparql?query=... & sparql?update... style URIs for querying a store, so why define a new way of doing things with store.query & store.graph_query etc?

Is there a Django-specific reason?

You can already pose questions to a SPARQL Endpoint using React or whatever you want right now, just as it is.

You said what is missing is to "Provide an endpoint with the serialized response" but the SPARQL responses are this! SPARQL query response formats are defined in the spec (e.g. JSON: https://www.w3.org/TR/sparql11-results-json/) and SPARQLWrapper knows how to parse them into Python objects. Other language libraries, like rdflib.js in JavaScript also know.

See YASGUI (https://triply.cc/docs/yasgui) for a stand-alone JS SPARQL client.

Upvotes: 1

Related Questions