use ByteBuffer::read_input_stream_async API

This commit is contained in:
yggverse 2024-10-24 15:39:54 +03:00
parent 35e0e2d47d
commit b867eb596f

View File

@ -20,8 +20,8 @@ use gtk::{
Uri, UriFlags, Uri, UriFlags,
}, },
prelude::{ prelude::{
ActionExt, IOStreamExt, InputStreamExtManual, OutputStreamExtManual, SocketClientExt, ActionExt, IOStreamExt, OutputStreamExtManual, SocketClientExt, StaticVariantType,
StaticVariantType, ToVariant, ToVariant,
}, },
Box, Box,
}; };
@ -235,24 +235,30 @@ impl Page {
action_update.activate(Some(&id)); action_update.activate(Some(&id));
// Read response
connection.input_stream().read_all_async(
vec![0; 0xfffff], // 1Mb @TODO
Priority::DEFAULT,
Some(&cancellable.clone()),
move |i| match i {
Ok(i) => {
// Define local NS // Define local NS
use gemini::client::response::{ use gemini::client::{
connection::input_stream::ByteBuffer,
response::{
header::{ header::{
Mime as ResponseMime, Mime as ResponseMime,
Status as ResponseStatus Status as ResponseStatus
}, },
Response, Response,
}
}; };
// Read response
ByteBuffer::new().read_input_stream_async(
connection.input_stream(),
cancellable.clone(),
Priority::DEFAULT,
None,
None,
move |i| {
match i {
Ok(buffer) => {
match Response::from_utf8( match Response::from_utf8(
&i.0.to_vec(), &buffer.to_utf8(),
) { ) {
Ok(response) => { Ok(response) => {
// Format response // Format response
@ -447,11 +453,11 @@ impl Page {
todo!("Error closing connection: {}", error.message()); todo!("Error closing connection: {}", error.message());
} }
} }
Err(error) => { Err(_) => {
// Define common data // Define common data
let status = Status::Failure; let status = Status::Failure;
let title = gformat!("Oops"); let title = gformat!("Oops");
let description = gformat!("Failed to read response: {}", error.1.message()); let description = gformat!("Failed to read response");
// Update widget // Update widget
content.set_status_failure(title.as_str(), description.as_str()); content.set_status_failure(title.as_str(), description.as_str());
@ -469,6 +475,7 @@ impl Page {
todo!("Error closing response connection: {}", error.message()); todo!("Error closing response connection: {}", error.message());
} }
} }
}
}, },
); );
} }