use futures::TryStreamExt;
use crate::{Iroh, IrohError, NodeAddr, PublicKey, RemoteInfo};
#[derive(uniffi::Object)]
pub struct Net {
client: NetClient,
}
#[uniffi::export]
impl Iroh {
pub fn net(&self) -> Net {
let client = self.client.clone().boxed();
let client = iroh_node_util::rpc::client::net::Client::new(client);
Net { client }
}
}
type NetClient = iroh_node_util::rpc::client::net::Client;
#[uniffi::export]
impl Net {
pub async fn node_id(&self) -> Result<String, IrohError> {
let id = self.client.node_id().await?;
Ok(id.to_string())
}
pub async fn node_addr(&self) -> Result<NodeAddr, IrohError> {
let addr = self.client.node_addr().await?;
Ok(addr.into())
}
pub async fn add_node_addr(&self, addr: &NodeAddr) -> Result<(), IrohError> {
self.client.add_node_addr(addr.clone().try_into()?).await?;
Ok(())
}
pub async fn home_relay(&self) -> Result<Option<String>, IrohError> {
let relay = self.client.home_relay().await?;
Ok(relay.map(|u| u.to_string()))
}
#[uniffi::method(async_runtime = "tokio")]
pub async fn remote_info_list(&self) -> Result<Vec<RemoteInfo>, IrohError> {
let infos = self
.client
.remote_info_iter()
.await?
.map_ok(|info| info.into())
.try_collect::<Vec<_>>()
.await?;
Ok(infos)
}
#[uniffi::method(async_runtime = "tokio")]
pub async fn remote_info(&self, node_id: &PublicKey) -> Result<Option<RemoteInfo>, IrohError> {
let info = self
.client
.remote_info(node_id.into())
.await
.map(|i| i.map(|i| i.into()))?;
Ok(info)
}
}