[grilo-plugins/wip/hadess/soup3] tmdb: Port to GUri and remove libsoup dependency



commit 2ece4f4e2c388c47241ebad638ef9987a00cfd45
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Jul 6 11:00:42 2022 +0200

    tmdb: Port to GUri and remove libsoup dependency

 meson.build                 |  2 +-
 src/tmdb/grl-tmdb-request.c | 43 ++++++++++++++++++++++++++++++++++---------
 src/tmdb/grl-tmdb.c         | 17 ++++++-----------
 3 files changed, 41 insertions(+), 21 deletions(-)
---
diff --git a/meson.build b/meson.build
index 5d20f1a0..55c93ea5 100644
--- a/meson.build
+++ b/meson.build
@@ -141,7 +141,7 @@ plugins = [ # NAME, REQ_DEPS, OPT_DEPS
     ['raitv', [grilo_net_dep, libxml_dep], []],
     ['shoutcast', [grilo_net_dep, libxml_dep], []],
     ['thetvdb', [grilo_net_dep, libxml_dep, libarchive_dep, gom_dep], []],
-    ['tmdb', [json_glib_dep, libsoup24_dep, grilo_net_dep], []],
+    ['tmdb', [json_glib_dep, grilo_net_dep], []],
     ['tracker', [tracker_sparql_dep], []],
     ['tracker3', [tracker3_dep], []],
     ['youtube', [grilo_net_dep, libxml_dep, libgdata_dep, totem_plparser_dep], []],
diff --git a/src/tmdb/grl-tmdb-request.c b/src/tmdb/grl-tmdb-request.c
index 5d2e5501..3d97f78d 100644
--- a/src/tmdb/grl-tmdb-request.c
+++ b/src/tmdb/grl-tmdb-request.c
@@ -28,7 +28,6 @@
 
 #include <grilo.h>
 #include <net/grl-net.h>
-#include <libsoup/soup.h>
 #include <json-glib/json-glib.h>
 
 #include "grl-tmdb-request.h"
@@ -84,7 +83,7 @@ struct _GrlTmdbRequestPrivate {
   char *uri;
   char *api_key;
   GHashTable *args;
-  SoupURI *base;
+  GUri *base;
   GTask *task;
   JsonParser *parser;
   GrlTmdbRequestDetail detail;
@@ -142,7 +141,7 @@ static void
 grl_tmdb_request_init (GrlTmdbRequest *self)
 {
   self->priv = grl_tmdb_request_get_instance_private (self);
-  self->priv->base = soup_uri_new (TMDB_BASE_URI);
+  self->priv->base = g_uri_parse (TMDB_BASE_URI, G_URI_FLAGS_NONE, NULL);
   self->priv->parser = json_parser_new ();
   self->priv->detail = GRL_TMDB_REQUEST_DETAIL_COUNT;
 }
@@ -180,7 +179,7 @@ grl_tmdb_request_finalize (GObject *object)
   g_clear_pointer (&self->priv->api_key, g_free);
   g_clear_pointer (&self->priv->uri, g_free);
   g_clear_pointer (&self->priv->args, g_hash_table_unref);
-  g_clear_pointer (&self->priv->base, soup_uri_free);
+  g_clear_pointer (&self->priv->base, g_uri_unref);
   g_clear_object (&self->priv->parser);
 
   G_OBJECT_CLASS (grl_tmdb_request_parent_class)->finalize (object);
@@ -507,6 +506,23 @@ append_details_list (GrlTmdbRequest *self,
   return NULL;
 }
 
+static char *
+args_to_string (GHashTable *args)
+{
+  GHashTableIter iter;
+  const char *key, *value;
+  GString *s = NULL;
+
+  s = g_string_new (NULL);
+  g_hash_table_iter_init (&iter, args);
+  while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *)&value)) {
+    g_string_append_uri_escaped (s, key, G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, FALSE);
+    g_string_append_c (s, '=');
+    g_string_append_uri_escaped (s, value, G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, FALSE);
+  }
+  return g_string_free (s, !(s->len > 0));
+}
+
 /**
  * grl_tmdb_request_run_async:
  * @self: Instance of GrlTmdbRequest
@@ -523,14 +539,23 @@ grl_tmdb_request_run_async (GrlTmdbRequest *self,
                             GCancellable *cancellable,
                             gpointer user_data)
 {
-  SoupURI *uri;
+  g_autoptr(GUri) absolute_uri = NULL;
+  g_autoptr(GUri) uri = NULL;
+  g_autofree char *query = NULL;
   char *call, *new_call;
   GHashTable *headers;
 
-  uri = soup_uri_new_with_base (self->priv->base, self->priv->uri);
-  soup_uri_set_query_from_form (uri, self->priv->args);
-  call = soup_uri_to_string (uri, FALSE);
-  soup_uri_free (uri);
+  absolute_uri = g_uri_parse_relative (self->priv->base, self->priv->uri, G_URI_FLAGS_NONE, NULL);
+  query = args_to_string (self->priv->args);
+  uri = g_uri_build (G_URI_FLAGS_NONE,
+                     g_uri_get_scheme (absolute_uri),
+                     g_uri_get_userinfo (absolute_uri),
+                     g_uri_get_host (absolute_uri),
+                     g_uri_get_port (absolute_uri),
+                     g_uri_get_path (absolute_uri),
+                     query,
+                     g_uri_get_fragment (absolute_uri));
+  call = g_uri_to_string (uri);
 
   new_call = append_details_list (self, call);
   if (new_call != NULL) {
diff --git a/src/tmdb/grl-tmdb.c b/src/tmdb/grl-tmdb.c
index 70a135e6..99f3391e 100644
--- a/src/tmdb/grl-tmdb.c
+++ b/src/tmdb/grl-tmdb.c
@@ -29,7 +29,6 @@
 
 #include <grilo.h>
 #include <net/grl-net.h>
-#include <libsoup/soup-uri.h>
 #include <json-glib/json-glib.h>
 #include <glib/gi18n-lib.h>
 
@@ -64,7 +63,7 @@ struct _GrlTmdbSourcePrivate {
   GrlTmdbRequest *configuration;
   gboolean config_pending;
   GQueue *pending_resolves;
-  SoupURI *image_base_uri;
+  GUri *image_base_uri;
 };
 
 struct _ResolveClosure {
@@ -357,10 +356,7 @@ grl_tmdb_source_finalize (GObject *object)
     self->priv->api_key = NULL;
   }
 
-  if (self->priv->image_base_uri != NULL) {
-    soup_uri_free (self->priv->image_base_uri);
-    self->priv->image_base_uri = NULL;
-  }
+  g_clear_pointer (&self->priv->image_base_uri, g_uri_unref);
 
   if (self->priv->configuration != NULL) {
     g_object_unref (self->priv->configuration);
@@ -603,16 +599,16 @@ add_image (GrlTmdbSource *self,
            GrlKeyID       detail_key,
            const char    *image_path)
 {
-  SoupURI *uri;
+  g_autoptr(GUri) uri = NULL;
   GrlRelatedKeys *related_keys;
   char *str;
   int i, l;
 
   str = g_strconcat ("original", image_path, NULL);
-  uri = soup_uri_new_with_base (self->priv->image_base_uri, str);
+  uri = g_uri_parse_relative (self->priv->image_base_uri, str, G_URI_FLAGS_NONE, NULL);
   g_free (str);
 
-  str = soup_uri_to_string (uri, FALSE);
+  str = g_uri_to_string (uri);
 
   l = grl_data_length (GRL_DATA (media), detail_key);
 
@@ -627,7 +623,6 @@ add_image (GrlTmdbSource *self,
   }
 
   g_free (str);
-  soup_uri_free (uri);
 }
 
 static void
@@ -1203,7 +1198,7 @@ on_configuration_ready (GObject *source,
   value = grl_tmdb_request_get (request, "$.images.base_url");
   if (value != NULL) {
     GRL_DEBUG ("Got TMDb configuration.");
-    self->priv->image_base_uri = soup_uri_new (g_value_get_string (value));
+    self->priv->image_base_uri = g_uri_parse (g_value_get_string (value), G_URI_FLAGS_NONE, NULL);
     g_value_unset (value);
     g_free (value);
   }


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