Skip to content

Commit

Permalink
Add support for libsoup3 (#4466)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcw authored Sep 24, 2022
1 parent 1ab1a96 commit 02565d9
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions [email protected]/files/[email protected]/applet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const PopupMenu = imports.ui.popupMenu;
const St = imports.gi.St;
const Gtk = imports.gi.Gtk;
const Soup = imports.gi.Soup;
const ByteArray = imports.byteArray;

const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
Expand All @@ -13,8 +14,13 @@ const UUID = '[email protected]';
const QUtils = require('./js/QUtils.js');
const QPopupSwitch = QUtils.QPopupSwitch;

const soupASyncSession = new Soup.SessionAsync();
Soup.Session.prototype.add_feature.call(soupASyncSession, new Soup.ProxyResolverDefault());
let soupASyncSession;
if (Soup.MAJOR_VERSION == 2) {
soupASyncSession = new Soup.SessionAsync();
Soup.Session.prototype.add_feature.call(soupASyncSession, new Soup.ProxyResolverDefault());
} else if (Soup.MAJOR_VERSION == 3) {
soupASyncSession = new Soup.Session();
}

function MyApplet(orientation, panelHeight, instanceId) {
this._init(orientation, panelHeight, instanceId);
Expand Down Expand Up @@ -79,18 +85,34 @@ MyApplet.prototype = {

getVerse: function (testament, callback) {
let request = Soup.Message.new('GET', "https://devotionalium.com/api/v2?lang=en");
soupASyncSession.queue_message(request, function(soupASyncSession, message) {
if (message.status_code !== 200) {
var return_message = "Connection error.";
callback(return_message);
return;
}

var responseParsed = JSON.parse(request.response_body.data);
var verse = responseParsed[testament].text;

callback(verse);
});
if (Soup.MAJOR_VERSION === 2) {
soupASyncSession.queue_message(request, (soupASyncSession, response) => {
if (response.status_code !== 200) {
var return_message = "Connection error.";
callback(return_message);
return;
}

var responseParsed = JSON.parse(response.response_body.data);
var verse = responseParsed[testament].text;

callback(verse);
});
} else { //version 3
soupASyncSession.send_and_read_async(request, Soup.MessagePriority.NORMAL, null, (soupASyncSession, response) => {
if (request.get_status() !== 200) {
var return_message = "Connection error.";
callback(return_message);
return;
}

const bytes = soupASyncSession.send_and_read_finish(response);
var responseParsed = JSON.parse(ByteArray.toString(bytes.get_data()));
var verse = responseParsed[testament].text;

callback(verse);
});
}
},

displayVerse: function(verseReturned) {
Expand Down

0 comments on commit 02565d9

Please sign in to comment.