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




commit ebf1d4451b154d909bd1d0db6534727d44aed34f
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 | 30 ++++++++++++++++++++++++
 src/libtracker-sparql/tracker-endpoint-http.c    | 10 ++++++++
 2 files changed, 40 insertions(+)
---
diff --git a/src/libtracker-sparql/remote/tracker-remote.vala 
b/src/libtracker-sparql/remote/tracker-remote.vala
index f3be3147a..98c85467f 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,13 @@ 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
+               return yield _session.send_async (message, cancellable);
+#else
+               return yield _session.send_async (message, GLib.Priority.DEFAULT, cancellable);
+#endif
+       }
 }
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]