[polari] utils: Port to Soup3



commit 3a56bfa9355a2d71d55b3f34b68a016d9d0a4290
Author: Florian Müllner <fmuellner gnome org>
Date:   Fri Jun 25 14:53:10 2021 +0200

    utils: Port to Soup3
    
    After 13 years, Soup will release a new, API-incompatible
    version[0]. This is a good thing, make sure we support it.
    
    [0] https://blog.tingping.se/2021/02/23/future-of-libsoup.html
    
    https://gitlab.gnome.org/GNOME/polari/-/merge_requests/207

 src/main.js        |  2 +-
 src/thumbnailer.js |  2 +-
 src/utils.js       | 70 +++++++++++++++++++++++++++++++++---------------------
 3 files changed, 45 insertions(+), 29 deletions(-)
---
diff --git a/src/main.js b/src/main.js
index 46cbc1fc..fa8c569c 100755
--- a/src/main.js
+++ b/src/main.js
@@ -22,7 +22,7 @@ pkg.require({
     'Pango': '1.0',
     'PangoCairo': '1.0',
     'Secret': '1',
-    'Soup': '2.4',
+    'Soup': '3.0',
     'TelepathyGLib': '0.12',
     'TelepathyLogger': '0.2',
 });
diff --git a/src/thumbnailer.js b/src/thumbnailer.js
index 6e0c8626..01ed7885 100644
--- a/src/thumbnailer.js
+++ b/src/thumbnailer.js
@@ -4,7 +4,7 @@ import Gio from 'gi://Gio';
 import GLib from 'gi://GLib';
 import GObject from 'gi://GObject';
 import Gtk from 'gi://Gtk?version=3.0';
-import WebKit2 from 'gi://WebKit2?version=4.0';
+import WebKit2 from 'gi://WebKit2?version=4.1';
 
 Gio._promisify(WebKit2.WebView.prototype, 'get_snapshot', 'get_snapshot_finish');
 Gio._promisify(WebKit2.WebView.prototype, 'run_javascript', 'run_javascript_finish');
diff --git a/src/utils.js b/src/utils.js
index 2706ba70..ce1d4fd7 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -20,11 +20,13 @@
  *
  */
 
+const ByteArray = imports.byteArray;
+
 import Gio from 'gi://Gio';
 import GLib from 'gi://GLib';
 import Gtk from 'gi://Gtk';
 import Secret from 'gi://Secret';
-import Soup from 'gi://Soup?version=2.4';
+import Soup from 'gi://Soup?version=3.0';
 import Tp from 'gi://TelepathyGLib';
 
 import * as AppNotifications from './appNotifications.js';
@@ -32,6 +34,8 @@ import * as AppNotifications from './appNotifications.js';
 Gio._promisify(Secret, 'password_store', 'password_store_finish');
 Gio._promisify(Secret, 'password_lookup', 'password_lookup_finish');
 Gio._promisify(Secret, 'password_clear', 'password_clear_finish');
+Gio._promisify(Soup.Session.prototype,
+    'send_and_read_async', 'send_and_read_finish');
 
 const SECRET_SCHEMA_ACCOUNT = new Secret.Schema(
     'org.gnome.Polari.Account',
@@ -253,24 +257,17 @@ export function updateTerms(terms, str) {
     return changed;
 }
 
-function _queueSoupMessage(session, message) {
-    return new Promise((resolve, reject) => {
-        session.queue_message(message, () => {
-            const { statusCode } = message;
-            if (statusCode === Soup.KnownStatusCode.OK)
-                resolve(message.responseBody.data);
-            else
-                reject(new Error(`Got unexpected response ${statusCode}`));
-        });
-    });
-}
-
 async function _getGpasteExpire() {
-    let session = new Soup.Session();
-    let paramUrl = `${GPASTE_BASEURL}api/json/parameter/expire`;
-    let message = Soup.form_request_new_from_hash('GET', paramUrl, {});
-
-    const json = await _queueSoupMessage(session, message);
+    const session = new Soup.Session();
+    const message = Soup.Message.new('GET',
+        `${GPASTE_BASEURL}api/json/parameter/expire`);
+
+    const bytes = await session.send_and_read_async(
+        message,
+        GLib.PRIORITY_DEFAULT,
+        null);
+    checkResponse(message);
+    const json = ByteArray.toString(bytes.get_data());
     const info = JSON.parse(json);
 
     const values = info.result?.values;
@@ -297,11 +294,17 @@ export async function gpaste(text, title) {
         language: 'text',
     };
 
-    let session = new Soup.Session();
-    let createUrl = `${GPASTE_BASEURL}api/json/create`;
-    let message = Soup.form_request_new_from_hash('POST', createUrl, params);
-
-    const json = await _queueSoupMessage(session, message);
+    const session = new Soup.Session();
+    const message = Soup.Message.new_from_encoded_form('POST',
+        `${GPASTE_BASEURL}api/json/create`,
+        Soup.form_encode_hash(params));
+
+    const bytes = await session.send_and_read_async(
+        message,
+        GLib.PRIORITY_DEFAULT,
+        null);
+    checkResponse(message);
+    const json = ByteArray.toString(bytes.get_data());
     const info = JSON.parse(json);
 
     if (!info.result?.id)
@@ -319,14 +322,20 @@ export async function imgurPaste(pixbuf, title) {
         image: GLib.base64_encode(buffer),
     };
 
-    let session = new Soup.Session();
-    let createUrl = 'https://api.imgur.com/3/image';
-    let message = Soup.form_request_new_from_hash('POST', createUrl, params);
+    const session = new Soup.Session();
+    const message = Soup.Message.new_from_encoded_form('POST',
+        'https://api.imgur.com/3/image',
+        Soup.form_encode_hash(params));
 
     let requestHeaders = message.request_headers;
     requestHeaders.append('Authorization', `Client-ID ${IMGUR_CLIENT_ID}`);
 
-    const json = await _queueSoupMessage(session, message);
+    const bytes = await session.send_and_read_async(
+        message,
+        GLib.PRIORITY_DEFAULT,
+        null);
+    checkResponse(message);
+    const json = ByteArray.toString(bytes.get_data());
     const info = JSON.parse(json);
 
     if (!info.success)
@@ -335,6 +344,13 @@ export async function imgurPaste(pixbuf, title) {
     return info.data.link;
 }
 
+function checkResponse(message) {
+    const { statusCode } = message;
+    const phrase = Soup.Status.get_phrase(statusCode);
+    if (statusCode !== Soup.Status.OK)
+        throw new Error(`Unexpected response: ${phrase}`);
+}
+
 export function formatTimePassed(seconds) {
     if (seconds === 0)
         return _('Now');


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