Skip to content

Commit

Permalink
Port 'HTTP Image' demo to Python (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandlo authored Feb 8, 2024
1 parent 2fcb7bd commit d4be697
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
9 changes: 3 additions & 6 deletions demos/HTTP Image/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
}
12 changes: 8 additions & 4 deletions demos/HTTP Image/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
31 changes: 31 additions & 0 deletions demos/HTTP Image/main.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 3 additions & 3 deletions demos/HTTP Image/main.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d4be697

Please sign in to comment.