use super::clients::OnlineClient;
use crate::{
block::Block,
chain::{Best, Chain, Finalized},
subxt_rpcs::RpcClient,
transaction_api::TransactionApi,
};
use avail_rust_core::{rpc::Error as RpcError, types::metadata::HashStringNumber};
#[cfg(feature = "tracing")]
use tracing_subscriber::util::TryInitError;
#[derive(Clone)]
pub struct Client {
online_client: OnlineClient,
pub rpc_client: RpcClient,
}
impl Client {
#[cfg(feature = "reqwest")]
pub async fn new(endpoint: &str) -> Result<Client, crate::Error> {
Self::new_ext(endpoint, true).await
}
#[cfg(feature = "reqwest")]
pub async fn new_ext(endpoint: &str, retry: bool) -> Result<Client, crate::Error> {
use super::clients::ReqwestClient;
let op = async || -> Result<Client, crate::Error> {
let rpc_client = ReqwestClient::new(endpoint);
let rpc_client = RpcClient::new(rpc_client);
Self::from_rpc_client(rpc_client).await.map_err(|e| e.into())
};
crate::utils::with_retry_on_error(op, retry).await
}
pub async fn from_rpc_client(rpc_client: RpcClient) -> Result<Client, RpcError> {
let online_client = OnlineClient::new(&rpc_client).await?;
Self::from_components(rpc_client, online_client).await
}
pub async fn from_components(rpc_client: RpcClient, online_client: OnlineClient) -> Result<Client, RpcError> {
Ok(Self { online_client, rpc_client })
}
#[cfg(feature = "tracing")]
pub fn init_tracing(json_format: bool) -> Result<(), TryInitError> {
use tracing_subscriber::util::SubscriberInitExt;
let builder = tracing_subscriber::fmt::SubscriberBuilder::default();
if json_format {
let builder = builder.json();
builder.finish().try_init()
} else {
builder.finish().try_init()
}
}
pub fn online_client(&self) -> OnlineClient {
self.online_client.clone()
}
pub fn tx(&self) -> TransactionApi {
TransactionApi(self.clone())
}
pub fn block(&self, block_id: impl Into<HashStringNumber>) -> Block {
Block::new(self.clone(), block_id)
}
pub fn chain(&self) -> Chain {
Chain::new(self.clone())
}
pub fn best(&self) -> Best {
Best::new(self.clone())
}
pub fn finalized(&self) -> Finalized {
Finalized::new(self.clone())
}
pub fn is_global_retries_enabled(&self) -> bool {
self.online_client.is_global_retries_enabled()
}
pub fn set_global_retries_enabled(&self, value: bool) {
self.online_client.set_global_retries_enabled(value);
}
}