pub mod files;
pub mod keys;
pub mod multimap;
pub mod nrs;
pub mod register;
pub mod resolver;
pub mod wallet;
pub use crate::safeurl::*;
pub use consts::DEFAULT_XORURL_BASE;
pub use sn_client::DEFAULT_NETWORK_CONTACTS_FILE_NAME;
pub use sn_interface::network_knowledge::SectionTree;
pub use xor_name::XorName;
mod auth;
mod consts;
mod helpers;
#[cfg(any(test, feature = "test-utils"))]
pub mod test_helpers;
use super::{common, constants, Error, Result};
use sn_client::Client;
use sn_dbc::Owner;
use sn_interface::types::Keypair;
use std::time::Duration;
use tracing::debug;
const APP_NOT_CONNECTED: &str = "Application is not connected to the network";
#[derive(Clone)]
pub struct Safe {
client: Option<Client>,
pub xorurl_base: XorUrlBase,
pub dry_run_mode: bool,
}
impl Safe {
pub fn dry_runner(xorurl_base: Option<XorUrlBase>) -> Self {
Self {
client: None,
xorurl_base: xorurl_base.unwrap_or(DEFAULT_XORURL_BASE),
dry_run_mode: true,
}
}
pub async fn connected(
keypair: Option<Keypair>,
xorurl_base: Option<XorUrlBase>,
timeout: Option<Duration>,
dbc_owner: Option<Owner>,
) -> Result<Self> {
let mut safe = Self {
client: None,
xorurl_base: xorurl_base.unwrap_or(DEFAULT_XORURL_BASE),
dry_run_mode: false,
};
safe.connect(keypair, timeout, dbc_owner).await?;
Ok(safe)
}
pub async fn connect(
&mut self,
keypair: Option<Keypair>,
timeout: Option<Duration>,
dbc_owner: Option<Owner>,
) -> Result<()> {
debug!("Connecting to SAFE Network...");
debug!("Client to be instantiated with specific pk?: {:?}", keypair);
let mut b = Client::builder()
.from_env() .keypair(keypair)
.dbc_owner(dbc_owner);
if let Some(timeout) = timeout {
b = b.query_timeout(timeout).cmd_timeout(timeout);
}
self.client = Some(b.build().await.map_err(|err| {
Error::ConnectionError(format!("Failed to connect to the SAFE Network: {err:?}"))
})?);
debug!("Successfully connected to the Network!!!");
Ok(())
}
pub fn is_connected(&self) -> bool {
self.client.is_some()
}
pub async fn section_tree(&self) -> Result<SectionTree> {
let section_tree = self.get_safe_client()?.section_tree().await;
Ok(section_tree)
}
pub(crate) fn get_safe_client(&self) -> Result<&Client> {
match &self.client {
Some(client) => Ok(client),
None => Err(Error::ConnectionError(APP_NOT_CONNECTED.to_string())),
}
}
}