Views

RDFResourceView

Bases: RDFView

API endpoint for fetching individual subjects.

Source code in rdf/views.py
class RDFResourceView(RDFView):
    """ API endpoint for fetching individual subjects. """

    def get(self, request, format=None, **kwargs):
        data = self.get_graph(request, **kwargs)
        if len(data) == 0:
            raise NotFound()
        return Response(data)

    def get_graph(self, request, **kwargs):
        identifier = URIRef(self.get_resource_uri(request, **kwargs))
        pattern = (identifier, None, None)
        result = graph_from_triples(self.graph().triples(pattern))
        # TODO: also include related nodes
        return result

    def get_resource_uri(self, request, **kwargs):
        return request.build_absolute_uri(request.path)

RDFView

Bases: APIView

Expose a given graph as an RDF-encoded API endpoint.

Either set a static graph member function or override get_graph to compute the graph from the request.

By default, this view supports Turtle as output and JSON-LD as input, but this can be overridden by providing custom values for renderer_classes and parser_classes.

Only JsonLdRenderer as an alternative renderer class has been tested. If using this, JSON LD context may be provided by setting the json_ld_context property (generally a dict).

Source code in rdf/views.py
class RDFView(APIView):
    """
    Expose a given graph as an RDF-encoded API endpoint.

    Either set a static `graph` member function or override
    `get_graph` to compute the graph from the request.

    By default, this view supports Turtle as output and JSON-LD as input,
    but this can be overridden by providing custom values for
    `renderer_classes` and `parser_classes`.

    Only `JsonLdRenderer` as an alternative renderer class has been tested.
    If using this, JSON LD context may be provided by setting the
    `json_ld_context` property (generally a `dict`).
    """
    renderer_classes = (TurtleRenderer,)
    parser_classes = (JSONLDParser,)

    def get(self, request, format=None, **kwargs):
        return Response(self.get_graph(request, **kwargs))

    def get_graph(self, request, **kwargs):
        return self.graph()

    def get_exception_handler(self):
        return custom_exception_handler

custom_exception_handler(error, context)

Returns a rest_framework Response with an rdflib Graph as .data.

Normally, a Response would have a JSON-serializable dict or list as data.

Source code in rdf/views.py
def custom_exception_handler(error, context):
    """
    Returns a rest_framework Response with an rdflib Graph as .data.

    Normally, a Response would have a JSON-serializable dict or list
    as data.
    """
    response = exception_handler(error, context)
    if response is None:
        return response
    status = response.status_code
    original_data = response.data

    res = BNode()
    override_data = graph_from_triples((
        (res, RDF.type, HTTP.Response),
        (res, HTTP.statusCodeValue, Literal(status)),
    ))
    if isinstance(original_data, list):
        append_triples(override_data, (
            (res, HTTP.reasonPhrase, Literal(str(d))) for d in original_data
        ))
    else:
        override_data.add((res, HTTP.reasonPhrase, Literal(original_data['detail'])))
    status_uri = HTTPSC_MAP.get(status)
    if status_uri:
        override_data.add((res, HTTP.sc, status_uri))
    response.data = override_data
    return response

graph_from_request(request)

Safely obtain a graph, from the request if present, empty otherwise.

Source code in rdf/views.py
def graph_from_request(request):
    """ Safely obtain a graph, from the request if present, empty otherwise. """
    data = request.data
    if not isinstance(data, Graph):
        data = Graph()
    return data