mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-03-31 17:15:31 +00:00
update response namespace
This commit is contained in:
parent
70fc128c29
commit
4767929050
19 changed files with 59 additions and 50 deletions
|
|
@ -3,18 +3,16 @@
|
|||
|
||||
pub mod connection;
|
||||
pub mod error;
|
||||
pub mod response;
|
||||
|
||||
pub use connection::Connection;
|
||||
pub use error::Error;
|
||||
pub use response::Response;
|
||||
|
||||
use gio::{
|
||||
prelude::{IOStreamExt, OutputStreamExt, SocketClientExt, TlsConnectionExt},
|
||||
prelude::{SocketClientExt, TlsConnectionExt},
|
||||
Cancellable, SocketClient, SocketClientEvent, SocketProtocol, TlsCertificate,
|
||||
TlsClientConnection,
|
||||
};
|
||||
use glib::{object::Cast, Bytes, Priority, Uri};
|
||||
use glib::{object::Cast, Priority, Uri};
|
||||
|
||||
pub const DEFAULT_TIMEOUT: u32 = 10;
|
||||
|
||||
|
|
@ -74,7 +72,7 @@ impl Client {
|
|||
priority: Priority,
|
||||
cancellable: Cancellable,
|
||||
certificate: Option<TlsCertificate>,
|
||||
callback: impl Fn(Result<Response, Error>) + 'static,
|
||||
callback: impl Fn(Result<connection::Response, Error>) + 'static,
|
||||
) {
|
||||
// Toggle socket mode
|
||||
// * guest sessions will not work without!
|
||||
|
|
@ -94,12 +92,14 @@ impl Client {
|
|||
{
|
||||
Ok(connection) => {
|
||||
// Begin new request
|
||||
request_async(
|
||||
connection,
|
||||
connection.request_async(
|
||||
uri.to_string(),
|
||||
priority,
|
||||
cancellable,
|
||||
callback, // result
|
||||
move |result| match result {
|
||||
Ok(response) => callback(Ok(response)),
|
||||
Err(e) => callback(Err(Error::Connection(e))),
|
||||
},
|
||||
)
|
||||
}
|
||||
Err(e) => callback(Err(Error::Connection(e))),
|
||||
|
|
@ -112,30 +112,3 @@ impl Client {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Middle-level helper, makes new request to available `Connection`
|
||||
/// * callback with new `Response` on success or `Error` on failure
|
||||
pub fn request_async(
|
||||
connection: Connection,
|
||||
query: String,
|
||||
priority: Priority,
|
||||
cancellable: Cancellable,
|
||||
callback: impl Fn(Result<Response, Error>) + 'static,
|
||||
) {
|
||||
connection.stream().output_stream().write_bytes_async(
|
||||
&Bytes::from(format!("{query}\r\n").as_bytes()),
|
||||
priority,
|
||||
Some(&cancellable.clone()),
|
||||
move |result| match result {
|
||||
Ok(_) => {
|
||||
Response::from_connection_async(connection, priority, cancellable, move |result| {
|
||||
callback(match result {
|
||||
Ok(response) => Ok(response),
|
||||
Err(e) => Err(Error::Response(e)),
|
||||
})
|
||||
})
|
||||
}
|
||||
Err(e) => callback(Err(Error::OutputStream(e))),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,17 @@
|
|||
pub mod error;
|
||||
pub mod response;
|
||||
|
||||
pub use error::Error;
|
||||
pub use response::Response;
|
||||
|
||||
use gio::{
|
||||
prelude::TlsConnectionExt, IOStream, NetworkAddress, SocketConnection, TlsCertificate,
|
||||
TlsClientConnection,
|
||||
prelude::{IOStreamExt, OutputStreamExt, TlsConnectionExt},
|
||||
Cancellable, IOStream, NetworkAddress, SocketConnection, TlsCertificate, TlsClientConnection,
|
||||
};
|
||||
use glib::{
|
||||
object::{Cast, IsA, ObjectExt},
|
||||
Bytes, Priority,
|
||||
};
|
||||
use glib::object::{Cast, IsA, ObjectExt};
|
||||
|
||||
pub struct Connection {
|
||||
pub socket_connection: SocketConnection,
|
||||
|
|
@ -51,6 +57,40 @@ impl Connection {
|
|||
})
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// Make new request to available `Connection`
|
||||
/// * callback with new `Response` on success or `Error` on failure
|
||||
pub fn request_async(
|
||||
self,
|
||||
query: String,
|
||||
priority: Priority,
|
||||
cancellable: Cancellable,
|
||||
callback: impl Fn(Result<Response, Error>) + 'static,
|
||||
) {
|
||||
self.tls_client_connection
|
||||
.output_stream()
|
||||
.write_bytes_async(
|
||||
&Bytes::from(format!("{query}\r\n").as_bytes()),
|
||||
priority,
|
||||
Some(&cancellable.clone()),
|
||||
move |result| match result {
|
||||
Ok(_) => Response::from_connection_async(
|
||||
self,
|
||||
priority,
|
||||
cancellable,
|
||||
move |result| {
|
||||
callback(match result {
|
||||
Ok(response) => Ok(response),
|
||||
Err(e) => Err(Error::Response(e)),
|
||||
})
|
||||
},
|
||||
),
|
||||
Err(e) => callback(Err(Error::Stream(e))),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
/// Get [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
|
||||
|
|
|
|||
|
|
@ -2,12 +2,20 @@ use std::fmt::{Display, Formatter, Result};
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Response(crate::client::connection::response::Error),
|
||||
Stream(glib::Error),
|
||||
TlsClientConnection(glib::Error),
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
match self {
|
||||
Self::Stream(e) => {
|
||||
write!(f, "TLS client connection error: {e}")
|
||||
}
|
||||
Self::Response(e) => {
|
||||
write!(f, "Response error: {e}")
|
||||
}
|
||||
Self::TlsClientConnection(e) => {
|
||||
write!(f, "TLS client connection error: {e}")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,20 +3,14 @@ use std::fmt::{Display, Formatter, Result};
|
|||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Connect(glib::Error),
|
||||
Connectable(String),
|
||||
Connection(crate::client::connection::Error),
|
||||
NetworkAddress(crate::gio::network_address::Error),
|
||||
OutputStream(glib::Error),
|
||||
Request(glib::Error),
|
||||
Response(crate::client::response::Error),
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
match self {
|
||||
Self::Connectable(uri) => {
|
||||
write!(f, "Could not create connectable address for {uri}")
|
||||
}
|
||||
Self::Connection(e) => {
|
||||
write!(f, "Connection error: {e}")
|
||||
}
|
||||
|
|
@ -26,15 +20,9 @@ impl Display for Error {
|
|||
Self::NetworkAddress(e) => {
|
||||
write!(f, "Network address error: {e}")
|
||||
}
|
||||
Self::OutputStream(e) => {
|
||||
write!(f, "Output stream error: {e}")
|
||||
}
|
||||
Self::Request(e) => {
|
||||
write!(f, "Request error: {e}")
|
||||
}
|
||||
Self::Response(e) => {
|
||||
write!(f, "Response error: {e}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue