alloy_rpc_client/
builder.rsuse crate::RpcClient;
use alloy_transport::{
BoxTransport, BoxTransportConnect, Transport, TransportConnect, TransportResult,
};
use tower::{
layer::util::{Identity, Stack},
Layer, ServiceBuilder,
};
#[derive(Debug)]
pub struct ClientBuilder<L> {
pub(crate) builder: ServiceBuilder<L>,
}
impl Default for ClientBuilder<Identity> {
fn default() -> Self {
Self { builder: ServiceBuilder::new() }
}
}
impl<L> ClientBuilder<L> {
pub fn layer<M>(self, layer: M) -> ClientBuilder<Stack<M, L>> {
ClientBuilder { builder: self.builder.layer(layer) }
}
pub fn transport<T>(self, transport: T, is_local: bool) -> RpcClient<L::Service>
where
L: Layer<T>,
T: Transport,
L::Service: Transport,
{
RpcClient::new(self.builder.service(transport), is_local)
}
#[cfg(feature = "reqwest")]
pub fn http(self, url: url::Url) -> RpcClient<L::Service>
where
L: Layer<alloy_transport_http::Http<reqwest::Client>>,
L::Service: Transport,
{
let transport = alloy_transport_http::Http::new(url);
let is_local = transport.guess_local();
self.transport(transport, is_local)
}
#[cfg(all(not(target_arch = "wasm32"), feature = "hyper"))]
pub fn hyper_http(self, url: url::Url) -> RpcClient<L::Service>
where
L: Layer<alloy_transport_http::HyperTransport>,
L::Service: Transport,
{
let transport = alloy_transport_http::HyperTransport::new_hyper(url);
let is_local = transport.guess_local();
self.transport(transport, is_local)
}
#[cfg(feature = "pubsub")]
pub async fn pubsub<C>(self, pubsub_connect: C) -> TransportResult<RpcClient<L::Service>>
where
C: alloy_pubsub::PubSubConnect,
L: Layer<alloy_pubsub::PubSubFrontend>,
L::Service: Transport,
{
let is_local = pubsub_connect.is_local();
let transport = pubsub_connect.into_service().await?;
Ok(self.transport(transport, is_local))
}
#[cfg(feature = "ws")]
pub async fn ws(
self,
ws_connect: alloy_transport_ws::WsConnect,
) -> TransportResult<RpcClient<L::Service>>
where
L: Layer<alloy_pubsub::PubSubFrontend>,
L::Service: Transport,
{
self.pubsub(ws_connect).await
}
#[cfg(feature = "ipc")]
pub async fn ipc<T>(
self,
ipc_connect: alloy_transport_ipc::IpcConnect<T>,
) -> TransportResult<RpcClient<L::Service>>
where
alloy_transport_ipc::IpcConnect<T>: alloy_pubsub::PubSubConnect,
L: Layer<alloy_pubsub::PubSubFrontend>,
L::Service: Transport,
{
self.pubsub(ipc_connect).await
}
pub async fn connect<C>(self, connect: C) -> TransportResult<RpcClient<L::Service>>
where
C: TransportConnect,
L: Layer<C::Transport>,
L::Service: Transport,
{
let transport = connect.get_transport().await?;
Ok(self.transport(transport, connect.is_local()))
}
pub async fn connect_boxed<C>(self, connect: C) -> TransportResult<RpcClient<L::Service>>
where
C: BoxTransportConnect,
L: Layer<BoxTransport>,
L::Service: Transport,
{
let transport = connect.get_boxed_transport().await?;
Ok(self.transport(transport, connect.is_local()))
}
}