modules/rdf/sparql/protocol.zzm

rdf-0.0.3 documentation

NAME

rdf/sparql/protocol - SPARQL Protocol request handling.

SYNOPSIS

  from rdf/sparql/protocol import SPARQLProtocolEndpoint;

  let endpoint := new SPARQLProtocolEndpoint(store: store);
  let response := endpoint.handle({
      params: { query: "ASK { ?s ?p ?o }" },
      accept: "application/sparql-results+json",
  });

Using with std/web:

  from rdf import RDFStore;
  from rdf/sparql/protocol import SPARQLProtocolEndpoint;
  from std/web import Request, Response, Routes;

  let store := RDFStore.temp();
  store.install_schema();

  let sparql := new SPARQLProtocolEndpoint(store: store);
  let routes := new Routes();

  routes.any("/sparql").to(
      action: function ( req ) {
          let handled := sparql.handle({
              params: {
                  query: req.param("query"),
                  update: req.param("update"),
              },
              body: req.body_text() == null ? "" : req.body_text(),
              content_type: req.content_type() == null
                  ? ""
                  : req.content_type(),
              accept: req.header("Accept") == null
                  ? "application/sparql-results+json"
                  : req.header("Accept"),
          });
          return new Response(
              status: handled{status},
              headers: { "Content-Type": handled{content_type} },
              body: [ handled{body} ],
          );
      },
  );

  function __request__ ( env ) {
      return routes.dispatch( new Request( env: env ) );
  }

DESCRIPTION

SPARQLProtocolEndpoint implements the core request handling rules for the SPARQL Protocol. It accepts query or update strings from request dictionaries, decodes URL-encoded form bodies, performs content negotiation for supported result formats, and returns response dictionaries with status, content_type, and body.

EXPORTS

Classes

  • SPARQLProtocolEndpoint

    Construct with store.

    • handle(Dict request)

      Handles one request. Recognised request fields include params, body, content_type, accept, query, and update. Returns a response dictionary. Query responses use status 200, successful updates use 204, client errors use 400, and execution errors use 500.

COPYRIGHT AND LICENCE

rdf/sparql/protocol is copyright Toby Inkster.

It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.