diff --git a/demos/HTTP Image/code.rs b/demos/HTTP Image/code.rs index 02e6f9d0..3286cf7f 100644 --- a/demos/HTTP Image/code.rs +++ b/demos/HTTP Image/code.rs @@ -5,6 +5,7 @@ use crate::workbench; use glib::source::Priority; use gtk::{gdk, glib}; use soup::prelude::*; +use soup::Status; pub fn main() { glib::spawn_future_local(async_main()); @@ -27,12 +28,8 @@ async fn get_image_bytes(url: &str) -> glib::Bytes { .await .unwrap(); - if message.status_code() != 200 { - panic!( - "Got {}, {:?}", - message.status_code(), - message.reason_phrase() - ); + if message.status() != Status::Ok { + panic!("Got {}, {:?}", message.status(), message.reason_phrase()); } image_bytes } diff --git a/demos/HTTP Image/main.js b/demos/HTTP Image/main.js index b6b48622..3b27e8aa 100644 --- a/demos/HTTP Image/main.js +++ b/demos/HTTP Image/main.js @@ -22,10 +22,14 @@ async function getImageBytes(url) { method: "GET", uri: GLib.Uri.parse(url, GLib.UriFlags.NONE), }); - const bytes = await session.send_and_read_async(message, null, null); - const { status_code, reason_phrase } = message; - if (status_code !== 200) { - throw new Error(`Got ${status_code}, ${reason_phrase}`); + const bytes = await session.send_and_read_async( + message, + GLib.PRIORITY_DEFAULT, + null, + ); + const status = message.get_status(); + if (status !== Soup.Status.OK) { + throw new Error(`Got ${status}, ${message.reason_phrase}`); } return bytes; } diff --git a/demos/HTTP Image/main.py b/demos/HTTP Image/main.py new file mode 100644 index 00000000..2da67cf6 --- /dev/null +++ b/demos/HTTP Image/main.py @@ -0,0 +1,31 @@ +import gi + +gi.require_version("Gdk", "4.0") +gi.require_version("Soup", "3.0") +from gi.repository import GLib, Gdk, Soup +import workbench + +# https://picsum.photos/ +IMAGE_URL = "https://picsum.photos/800" + + +def on_receive_bytes(session, result, message): + bytes = session.send_and_read_finish(result) + if message.get_status() != Soup.Status.OK: + raise Exception(f"Got {message.get_status()}, {message.get_reason_phrase()}") + texture = Gdk.Texture.new_from_bytes(bytes) + workbench.builder.get_object("picture").set_paintable(texture) + + +def get_image_bytes(url): + session = Soup.Session() + message = Soup.Message( + method="GET", + uri=GLib.Uri.parse(url, GLib.UriFlags.NONE), + ) + session.send_and_read_async( + message, GLib.PRIORITY_DEFAULT, None, on_receive_bytes, message + ) + + +get_image_bytes(IMAGE_URL) diff --git a/demos/HTTP Image/main.vala b/demos/HTTP Image/main.vala index 95b56a52..f397e693 100644 --- a/demos/HTTP Image/main.vala +++ b/demos/HTTP Image/main.vala @@ -24,11 +24,11 @@ private async Bytes ? get_image_bytes (string url) throws Error { Bytes image_bytes = yield session.send_and_read_async (message, Priority.DEFAULT, null); - uint status_code = message.status_code; + Soup.Status status = message.get_status (); string reason = message.reason_phrase; - if (status_code != 200) { - throw new MessageError.FAILED (@"Got $status_code: $reason"); + if (status != Soup.Status.OK) { + throw new MessageError.FAILED (@"Got $status: $reason"); } return image_bytes;