[gnome-maps/wip/mlundblad/libsoup3] WIP: Port to libsoup3




commit 375d7ac98585907cdf287c5795d830d165a37b6e
Author: Marcus Lundblad <ml dfupdate se>
Date:   Wed Aug 17 22:49:12 2022 +0200

    WIP: Port to libsoup3

 lib/meson.build           |  2 +-
 meson.build               |  4 +--
 org.gnome.Maps.json       | 18 +++++++++----
 src/graphHopper.js        | 12 ++++++---
 src/graphHopperGeocode.js | 26 ++++++++++---------
 src/main.js               |  6 ++---
 src/osmConnection.js      | 24 ++++++++----------
 src/overpass.js           | 36 ++++++++++++++------------
 src/photonGeocode.js      | 23 ++++++++---------
 src/wikipedia.js          | 64 ++++++++++++++++++++++++-----------------------
 tests/urisTest.js         |  2 +-
 tests/utilsTest.js        |  1 +
 tests/wikipediaTest.js    |  2 +-
 13 files changed, 120 insertions(+), 100 deletions(-)
---
diff --git a/lib/meson.build b/lib/meson.build
index ada8239d..82824e8c 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -49,7 +49,7 @@ gnome.generate_gir(
        includes: [
                'GLib-2.0',
                'GObject-2.0',
-               'Rest-0.7',
+               'Rest-1.0',
                'Shumate-1.0'
        ],
        install: true,
diff --git a/meson.build b/meson.build
index 13ddd0d4..f6b4a604 100644
--- a/meson.build
+++ b/meson.build
@@ -38,12 +38,12 @@ gtk4 = dependency('gtk4')
 geoclue2 = dependency('geoclue-2.0', version: '>= 0.12.99')
 libadwaita = dependency('libadwaita-1')
 gweather = dependency('gweather4', version: '>= 3.90.0')
-geocodeglib = dependency('geocode-glib-1.0', version: '>= 3.15.2')
+geocodeglib = dependency('geocode-glib-2.0', version: '>= 3.15.2')
 
 libmaps_deps = [
        dependency('shumate-1.0'),
        dependency('libxml-2.0'),
-       dependency('rest-0.7', version: '>= 0.7.90')
+       dependency('rest-1.0')
 ]
 
 msgfmt = find_program('msgfmt')
diff --git a/org.gnome.Maps.json b/org.gnome.Maps.json
index be095983..35a27a54 100644
--- a/org.gnome.Maps.json
+++ b/org.gnome.Maps.json
@@ -54,7 +54,8 @@
             "builddir" : true,
             "config-opts" : [
                 "--libdir=/app/lib",
-                "-Denable-gtk-doc=false"
+                "-Denable-gtk-doc=false",
+                "-Dsoup2=false"
             ],
             "sources" : [
                 {
@@ -68,7 +69,7 @@
             "config-opts" : [
                 "-Denable_vala=false",
                 "-Dgtk_doc=false",
-                "-Dsoup2=true"
+                "-Dsoup2=false"
             ],
             "buildsystem" : "meson",
             "sources" : [
@@ -81,11 +82,17 @@
         },
         {
             "name" : "librest",
+            "buildsystem": "meson",
+            "config-opts": [
+               "-Dgtk_doc=false",
+               "-Dexamples=false",
+               "-Dsoup2=false"
+            ],
             "sources" : [
                 {
                     "type" : "archive",
-                    "url" : "https://gitlab.gnome.org/GNOME/librest/-/archive/0.8.1/librest-0.8.1.tar.gz";,
-                    "sha256" : "2bdd1be07a9150b1c6ceea6e01dedf2efcff564381cae0cd6c7330403efe59d7"
+                    "url" : "https://gitlab.gnome.org/GNOME/librest/-/archive/1.0.0/librest-1.0.0.tar.gz";,
+                    "sha256" : "eeba5ddbf91a29decec01c3ccce64b922bd9bf52d631e307e185227295aea51d"
                 }
             ]
         },
@@ -95,7 +102,8 @@
             "config-opts": [
                 "-Ddemos=false",
                 "-Dgtk_doc=false",
-                "-Dvapi=false"
+                "-Dvapi=false",
+                "-Dlibsoup3=true"
             ],
             "sources" : [
                 {
diff --git a/src/graphHopper.js b/src/graphHopper.js
index 76a9b533..78044465 100644
--- a/src/graphHopper.js
+++ b/src/graphHopper.js
@@ -85,9 +85,15 @@ export class GraphHopper {
     _queryGraphHopper(points, transportationType, callback) {
         let url = this._buildURL(points, transportationType);
         let msg = Soup.Message.new('GET', url);
-        this._session.queue_message(msg, (session, message) => {
+        this._session.send_and_read_async(msg, GLib.PRIORITY_DEFAULT, null,
+                                          (source,res) => {
+            let bytes = this._session.send_and_read_finish(res);
+            let body = bytes ? Utils.getBufferText(bytes.get_data()) : null;
+
             try {
-                let result = this._parseMessage(message);
+                let result = this._parseMessage({ status_code:   msg.get_status(),
+                                                  response_body: body,
+                                                  uri:           url });
                 if (!result)
                     callback(null, null);
                 else
@@ -168,7 +174,7 @@ export class GraphHopper {
         if (status_code !== 200)
             return null;
 
-        let result = JSON.parse(response_body.data);
+        let result = JSON.parse(response_body);
 
         if (!Array.isArray(result.paths)) {
             Utils.debug("No route found");
diff --git a/src/graphHopperGeocode.js b/src/graphHopperGeocode.js
index 790beb2a..70020011 100644
--- a/src/graphHopperGeocode.js
+++ b/src/graphHopperGeocode.js
@@ -44,21 +44,20 @@ export class GraphHopperGeocode {
         let url = this._buildURL(string, latitude, longitude);
         let msg = Soup.Message.new('GET', url);
 
-        let handler = cancellable.connect(() => {
-            this._session.cancel_message(msg, Soup.Status.CANCELLED);
-        });
-
-        this._session.queue_message(msg, (session, message) => {
-            cancellable.disconnect(handler);
-
+        this._session.send_and_read_async(msg, GLib.PRIORITY_DEFAULT,
+                                          cancellable,
+                                          (source, res) => {
             if (cancellable.is_cancelled())
                 return;
 
-            if (message.status_code !== Soup.KnownStatusCode.OK) {
-                callback(null, msg.status_code);
+            if (msg.get_status() !== Soup.Status.OK) {
+                callback(null, msg.get_status());
             } else {
                 try {
-                    let result = this._parseMessage(message.response_body.data);
+                    let buffer =
+                        this._session.send_and_read_finish(res).get_data();
+                    let result = this._parseMessage(Utils.getBufferText(buffer));
+
                     if (!result)
                         callback(null, null);
                     else
@@ -76,10 +75,13 @@ export class GraphHopperGeocode {
         let msg = Soup.Message.new('GET', url);
 
         Application.application.mark_busy();
-        this._session.queue_message(msg, (session, message) => {
+        this._session.send_and_read_async(msg, GLib.PRIORITY_DEFAULT, null,
+                                          (source, res) => {
             Application.application.unmark_busy();
             try {
-                let result = this._parseMessage(message.response_body.data);
+                let buffer = this._session.send_and_read_finish(res).get_data();
+
+                let result = this._parseMessage(Utils.getBufferText(buffer));
                 if (!result)
                     callback(null, null);
                 else
diff --git a/src/main.js b/src/main.js
index b7f0b87b..c2d16458 100644
--- a/src/main.js
+++ b/src/main.js
@@ -21,7 +21,7 @@
  */
 
 import 'gi://Adw?version=1';
-import 'gi://GeocodeGlib?version=1.0';
+import 'gi://GeocodeGlib?version=2.0';
 import 'gi://Gdk?version=4.0';
 import 'gi://GdkPixbuf?version=2.0';
 import 'gi://Gio?version=2.0';
@@ -29,9 +29,9 @@ import 'gi://GLib?version=2.0';
 import 'gi://GObject?version=2.0';
 import 'gi://Gtk?version=4.0';
 import 'gi://GWeather?version=4.0';
-import 'gi://Rest?version=0.7';
+import 'gi://Rest?version=1.0';
 import 'gi://Shumate?version=1.0';
-import 'gi://Soup?version=2.4';
+import 'gi://Soup?version=3.0';
 
 import * as system from 'system';
 
diff --git a/src/osmConnection.js b/src/osmConnection.js
index c61b505c..5a18f350 100644
--- a/src/osmConnection.js
+++ b/src/osmConnection.js
@@ -25,6 +25,7 @@ import gettext from 'gettext';
 import GnomeMaps from 'gi://GnomeMaps';
 
 import Gio from 'gi://Gio';
+import GLib from 'gi://GLib';
 import Rest from 'gi://Rest';
 import Secret from 'gi://Secret';
 import Soup from 'gi://Soup';
@@ -62,26 +63,23 @@ export class OSMConnection {
 
     getOSMObject(type, id, callback, cancellable) {
         let url = this._getQueryUrl(type, id);
-        let uri = new Soup.URI(url);
-        let request = new Soup.Message({ method: 'GET', uri: uri });
+        let request = Soup.Message.new('GET', url);
 
-        cancellable.connect(() => {
-            this._session.cancel_message(request, Soup.STATUS_CANCELLED);
-        });
-
-        this._session.queue_message(request, (obj, message) => {
-            if (message.status_code !== Soup.Status.OK) {
-                callback(false, message.status_code, null, type, null);
+        this._session.send_and_read_async(request, GLib.PRIORITY_DEFAULT, cancellable,
+                                          (source, res) => {
+            if (request.get_status() !== Soup.Status.OK) {
+                callback(false, message.get_status(), null, type, null);
                 return;
             }
 
             try {
-                let object = GnomeMaps.osm_parse (message.response_body.data,
-                                                  message.response_body.length);
-                callback(true, message.status_code, object, type, null);
+                let body = this._session.send_and_read_finish(res);
+                let object = GnomeMaps.osm_parse (Utils.getBufferText(body.get_data()),
+                                                  body.get_size());
+                callback(true, request.get_status(), object, type, null);
             } catch (e) {
                 Utils.debug(e);
-                callback(false, message.status_code, null, type, e);
+                callback(false, request.get_status(), null, type, e);
             }
         });
     }
diff --git a/src/overpass.js b/src/overpass.js
index 039cb4e6..8948b2a4 100644
--- a/src/overpass.js
+++ b/src/overpass.js
@@ -18,6 +18,7 @@
  */
 
 import Geocode from 'gi://GeocodeGlib';
+import GLib from 'gi://GLib';
 import GObject from 'gi://GObject';
 import Soup from 'gi://Soup';
 
@@ -67,17 +68,18 @@ export class Overpass extends GObject.Object {
     addInfo(place) {
         let url = this._getQueryUrl(Utils.osmTypeToString(place.osm_type),
                                     place.osm_id);
-        let uri = new Soup.URI(url);
-        let request = new Soup.Message({ method: 'GET',
-                                         uri: uri });
+        let request = Soup.Message.new('GET', url);
 
-        this._session.queue_message(request, (obj, message) => {
-            if (message.status_code !== Soup.KnownStatusCode.OK) {
-                Utils.debug('Failed to fetch Overpass result: ' + message.status_code);
+        this._session.send_and_read_async(request, GLib.PRIORITY_DEFAULT, null,
+                                          (source, res) => {
+            if (request.get_status() !== Soup.Status.OK) {
+                Utils.debug('Failed to fetch Overpass result: ' +
+                            request.get_status());
                 return;
             }
             try {
-                let jsonObj = JSON.parse(message.response_body.data);
+                let buffer = this._session.send_and_read_finish(res).get_data();
+                let jsonObj = JSON.parse(Utils.getBufferText(buffer));
                 this._populatePlace(place, jsonObj);
                 this.place = place;
                 this.notify('place');
@@ -88,18 +90,20 @@ export class Overpass extends GObject.Object {
     }
 
     fetchPlace(osmType, osmId, callback) {
-        let url = this._getQueryUrl(osmType, osmId);
-        let uri = new Soup.URI(url);
-        let request = new Soup.Message({ method: 'GET',
-                                         uri: uri });
-
-        this._session.queue_message(request, (obj, message) => {
-            if (message.status_code !== Soup.KnownStatusCode.OK) {
-                Utils.debug('Failed to fetch Overpass result: ' + message.status_code);
+        let url = this._getQueryUrl(osmType, osmId)
+        let request = Soup.Message.new('GET', url);
+
+        this._session.send_and_read_async(request, GLib.PRIORITY_DEFAULT, null,
+                                          (source, res) => {
+            if (request.get_status() !== Soup.Status.OK) {
+                Utils.debug('Failed to fetch Overpass result: ' +
+                            request.get_status());
                 callback(null);
+                return;
             }
             try {
-                let jsonObj = JSON.parse(message.response_body.data);
+                let buffer = this._session.send_and_read_finish(res).get_data();
+                let jsonObj = JSON.parse(Utils.getBufferText(buffer));
                 let place = this._createPlace(jsonObj, osmType, osmId);
                 callback(place);
             } catch(e) {
diff --git a/src/photonGeocode.js b/src/photonGeocode.js
index 4c3a9579..29c98c58 100644
--- a/src/photonGeocode.js
+++ b/src/photonGeocode.js
@@ -42,22 +42,19 @@ export class PhotonGeocode {
 
     search(string, latitude, longitude, cancellable, callback) {
         let url = this._buildURL(string, latitude, longitude);
-        let msg = Soup.Message.new('GET', url);
-        let handler = cancellable.connect(() => {
-            this._session.cancel_message(msg, Soup.Status.CANCELLED);
-        });
-
-        this._session.queue_message(msg, (session, message) => {
-            cancellable.disconnect(handler);
+        let msg = Soup.Message.new('GET', url)
 
+        this._session.send_and_read_async(msg, GLib.PRIORITY_DEFAULT, cancellable,
+                                          (source, res) => {
             if (cancellable.is_cancelled())
                 return;
 
-            if (message.status_code !== Soup.KnownStatusCode.OK) {
-                callback(null, msg.status_code);
+            if (msg.get_status() !== Soup.Status.OK) {
+                callback(null, msg.get_status());
             } else {
                 try {
-                    let result = this._parseMessage(message.response_body.data);
+                    let buffer = this._session.send_and_read_finish(res).get_data();
+                    let result = this._parseMessage(Utils.getBufferText(buffer));
                     if (!result)
                         callback(null, null);
                     else
@@ -75,10 +72,12 @@ export class PhotonGeocode {
         let msg = Soup.Message.new('GET', url);
 
         Application.application.mark_busy();
-        this._session.queue_message(msg, (session, message) => {
+        this._session.send_and_read_async(msg, GLib.PRIORITY_DEFAULT, null,
+                                          (source, res) => {
             Application.application.unmark_busy();
             try {
-                let result = this._parseMessage(message.response_body.data);
+                let buffer = this._session.send_and_read_finish(res).get_data();
+                let result = this._parseMessage(Utils.getBufferText(buffer));
                 if (!result)
                     callback(null, null);
                 else
diff --git a/src/wikipedia.js b/src/wikipedia.js
index c8dd556d..80ad3a7d 100644
--- a/src/wikipedia.js
+++ b/src/wikipedia.js
@@ -49,8 +49,8 @@ export function getLanguage(wiki) {
 }
 
 export function getArticle(wiki) {
-    return Soup.uri_encode(wiki.replace(/ /g, '_').split(':').splice(1).join(':'),
-                           '\'');
+    return GLib.uri_escape_string(wiki.replace(/ /g, '_').split(':').splice(1).join(':'),
+                                  '\'', false);
 }
 
 export function getHtmlEntityEncodedArticle(wiki) {
@@ -89,26 +89,28 @@ export function fetchArticleInfo(wiki, size, metadataCb, thumbnailCb) {
     let lang = getLanguage(wiki);
     let title = getHtmlEntityEncodedArticle(wiki);
     let uri = `https://${lang}.wikipedia.org/w/api.php`;
-    let msg = Soup.form_request_new_from_hash('GET', uri, { action: 'query',
-                                                            titles: title,
-                                                            prop: 'extracts|pageimages|langlinks',
-                                                            format: 'json',
-
-                                                            /* Allow redirects, for example if an
-                                                               article is renamed. */
-                                                            redirects: '1',
-
-                                                            /* Make sure we get all lang links */
-                                                            lllimit: 'max',
-
-                                                            /* don't go past first section header */
-                                                            exintro: 'yes',
-                                                            /* limit the length   */
-                                                            exchars: '200',
-                                                            /* for plain text rather than HTML */
-                                                            explaintext: 'yes',
-
-                                                            pithumbsize: size + ''});
+    let encodedForm =
+        Soup.form_encode_hash({ action: 'query',
+                                titles: title,
+                                prop: 'extracts|pageimages|langlinks',
+                                format: 'json',
+
+                                /* Allow redirects, for example if an
+                                   article is renamed. */
+                                redirects: '1',
+
+                                /* Make sure we get all lang links */
+                                lllimit: 'max',
+
+                                /* don't go past first section header */
+                                exintro: 'yes',
+                                /* limit the length   */
+                                exchars: '200',
+                                /* for plain text rather than HTML */
+                                explaintext: 'yes',
+
+                                pithumbsize: size + '' });
+    let msg = Soup.Message.new_from_encoded_form('GET', uri, encodedForm);
     let session = _getSoupSession();
     let cachedMetadata = _metadataCache[wiki];
 
@@ -117,8 +119,9 @@ export function fetchArticleInfo(wiki, size, metadataCb, thumbnailCb) {
         return;
     }
 
-    session.queue_message(msg, (session, msg) => {
-        if (msg.status_code !== Soup.KnownStatusCode.OK) {
+    session.send_and_read_async(msg, GLib.PRIORIRY_DEFAULT, null,
+                                     (source, res) => {
+        if (msg.get_status() !== Soup.Status.OK) {
             log("Failed to request Wikipedia metadata: " + msg.reason_phrase);
             metadataCb(null, {});
             if (thumbnailCb) {
@@ -127,7 +130,8 @@ export function fetchArticleInfo(wiki, size, metadataCb, thumbnailCb) {
             return;
         }
 
-        let response = JSON.parse(msg.response_body.data);
+        let buffer = session.send_and_read_finish(res).get_data();
+        let response = JSON.parse(Utils.getBufferText(buffer));
         let pages = response.query.pages;
 
         if (pages) {
@@ -175,8 +179,7 @@ function _onMetadataFetched(wiki, page, size, metadataCb, thumbnailCb) {
 }
 
 function _fetchThumbnailImage(wiki, size, source, callback) {
-    let uri = new Soup.URI(source);
-    let msg = new Soup.Message({ method: 'GET', uri: uri });
+    let msg = Soup.Message.new('GET', source);
     let session = _getSoupSession();
 
     let cachedThumbnail = _thumbnailCache[wiki + '/' + size];
@@ -185,15 +188,14 @@ function _fetchThumbnailImage(wiki, size, source, callback) {
         return;
     }
 
-    session.queue_message(msg, (session, msg) => {
-        if (msg.status_code !== Soup.KnownStatusCode.OK) {
+    session.send_async(msg, GLib.PRIORITY_DEFAULT, null, (source, res) => {
+        if (msg.get_status() !== Soup.Status.OK) {
             log("Failed to download thumbnail: " + msg.reason_phrase);
             callback(null);
             return;
         }
 
-        let contents = msg.response_body_data;
-        let stream = Gio.MemoryInputStream.new_from_bytes(contents);
+        let stream = session.send_finish(res);
 
         try {
             let pixbuf = GdkPixbuf.Pixbuf.new_from_stream(stream, null);
diff --git a/tests/urisTest.js b/tests/urisTest.js
index f5fe6e86..8b8b4d9f 100644
--- a/tests/urisTest.js
+++ b/tests/urisTest.js
@@ -21,7 +21,7 @@
 
 import 'gi://Gdk?version=3.0';
 import 'gi://Gtk?version=3.0';
-import 'gi://Soup?version=2.4';
+import 'gi://Soup?version=3.0';
 
 const JsUnit = imports.jsUnit;
 
diff --git a/tests/utilsTest.js b/tests/utilsTest.js
index af3719e0..118f9a72 100644
--- a/tests/utilsTest.js
+++ b/tests/utilsTest.js
@@ -21,6 +21,7 @@
 
 import 'gi://Gdk?version=3.0';
 import 'gi://Gtk?version=3.0';
+import 'gi://GeocodeGlib?version=2.0';
 
 import GeocodeGlib from 'gi://GeocodeGlib';
 import GLib from 'gi://GLib';
diff --git a/tests/wikipediaTest.js b/tests/wikipediaTest.js
index 72d55178..9b6f2962 100644
--- a/tests/wikipediaTest.js
+++ b/tests/wikipediaTest.js
@@ -21,7 +21,7 @@
 
 import 'gi://Gdk?version=3.0';
 import 'gi://Gtk?version=3.0';
-import 'gi://Soup?version=2.4';
+import 'gi://Soup?version=3.0';
 
 import * as Wikipedia from './wikipedia.js';
 


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