[tracker/wip/carlosg/serialize-api: 10/11] libtracker-sparql: Implement serialize_async/finish on remote connection




commit 6f0b05a55520f9c4ed6218f19e123b06eedff8fe
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Nov 21 23:45:56 2021 +0100

    libtracker-sparql: Implement serialize_async/finish on remote connection
    
    HTTP endpoints must already serve RDF formats on DESCRIBE/CONSTRUCT queries
    if asked so. Hook the TrackerSparqlConnection serialize_async call there,
    and handle those formats in the TrackerEndpointHttp.

 src/libtracker-sparql/remote/tracker-remote.vala | 35 ++++++++++++++++++++++++
 src/libtracker-sparql/tracker-endpoint-http.c    | 10 +++++++
 2 files changed, 45 insertions(+)
---
diff --git a/src/libtracker-sparql/remote/tracker-remote.vala 
b/src/libtracker-sparql/remote/tracker-remote.vala
index f3be3147a..9c74b6fb0 100644
--- a/src/libtracker-sparql/remote/tracker-remote.vala
+++ b/src/libtracker-sparql/remote/tracker-remote.vala
@@ -28,6 +28,8 @@ public class Tracker.Remote.Connection : Tracker.Sparql.Connection {
 
        const string XML_TYPE = "application/sparql-results+xml";
        const string JSON_TYPE = "application/sparql-results+json";
+       const string TTL_TYPE = "text/turtle";
+       const string TRIG_TYPE = "application/trig";
        const string USER_AGENT = "Tracker/" + PACKAGE_VERSION + " 
(https://gitlab.gnome.org/GNOME/tracker/issues/; tracker-list lists gnome org) Tracker/" + PACKAGE_VERSION;
 
        public Connection (string base_uri) {
@@ -36,6 +38,25 @@ public class Tracker.Remote.Connection : Tracker.Sparql.Connection {
                _session = new Soup.Session ();
        }
 
+       private Soup.Message create_describe_request (string sparql, RdfFormat format) {
+               var uri = _base_uri + "?query=" + GLib.Uri.escape_string (sparql, null, false);
+               var message = new Soup.Message ("GET", uri);
+#if SOUP2
+               var headers = message.request_headers;
+#else
+                var headers = message.get_request_headers();
+#endif
+
+               headers.append ("User-Agent", USER_AGENT);
+
+               if (format == RdfFormat.TURTLE)
+                       headers.append ("Accept", TTL_TYPE);
+               else if (format == RdfFormat.TRIG)
+                       headers.append ("Accept", TRIG_TYPE);
+
+               return message;
+       }
+
        private Soup.Message create_request (string sparql) {
                var uri = _base_uri + "?query=" + GLib.Uri.escape_string (sparql, null, false);
                var message = new Soup.Message ("GET", uri);
@@ -120,4 +141,18 @@ public class Tracker.Remote.Connection : Tracker.Sparql.Connection {
        public async override bool close_async () throws GLib.IOError {
                return true;
        }
+
+       public async override GLib.InputStream serialize_async (RdfFormat format, string sparql, 
GLib.Cancellable? cancellable = null) throws Sparql.Error, GLib.Error, GLib.IOError, GLib.DBusError {
+               var message = create_describe_request (sparql, format);
+#if SOUP2
+               yield _session.send_async (message, cancellable);
+               var data = message.response_body.flatten ().data;
+#else
+                var body = yield _session.send_and_read_async (message, GLib.Priority.DEFAULT, cancellable);
+                var data = body.get_data ();
+#endif
+
+                var istream = new GLib.MemoryInputStream.from_data (data);
+                return istream;
+       }
 }
diff --git a/src/libtracker-sparql/tracker-endpoint-http.c b/src/libtracker-sparql/tracker-endpoint-http.c
index 0d56bd561..2f1282004 100644
--- a/src/libtracker-sparql/tracker-endpoint-http.c
+++ b/src/libtracker-sparql/tracker-endpoint-http.c
@@ -83,6 +83,8 @@ enum {
 
 #define XML_TYPE "application/sparql-results+xml"
 #define JSON_TYPE "application/sparql-results+json"
+#define TTL_TYPE "text/turtle"
+#define TRIG_TYPE "application/trig"
 
 static GParamSpec *props[N_PROPS];
 static guint signals[N_SIGNALS];
@@ -235,6 +237,14 @@ pick_format (SoupMessage             *message,
                soup_message_headers_set_content_type (response_headers, XML_TYPE, NULL);
                *format = TRACKER_SERIALIZER_FORMAT_XML;
                return TRUE;
+       } else if (soup_message_headers_header_contains (request_headers, "Accept", TTL_TYPE)) {
+               soup_message_headers_set_content_type (response_headers, TTL_TYPE, NULL);
+               *format = TRACKER_SERIALIZER_FORMAT_TTL;
+               return TRUE;
+       } else if (soup_message_headers_header_contains (request_headers, "Accept", TRIG_TYPE)) {
+               soup_message_headers_set_content_type (response_headers, TRIG_TYPE, NULL);
+               *format = TRACKER_SERIALIZER_FORMAT_TRIG;
+               return TRUE;
        } else {
                return FALSE;
        }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]