ic_web3_rs/transports/
either.rsuse crate::{api, error, rpc, BatchTransport, DuplexTransport, RequestId, Transport};
use futures::{
future::{BoxFuture, FutureExt},
stream::{BoxStream, StreamExt},
};
use ic_cdk::api::management_canister::http_request::TransformContext;
use super::ic_http_client::CallOptions;
#[derive(Debug, Clone)]
pub enum Either<A, B> {
Left(A),
Right(B),
}
impl<A, B, AOut, BOut> Transport for Either<A, B>
where
A: Transport<Out = AOut>,
B: Transport<Out = BOut>,
AOut: futures::Future<Output = error::Result<rpc::Value>> + 'static + Send,
BOut: futures::Future<Output = error::Result<rpc::Value>> + 'static + Send,
{
type Out = BoxFuture<'static, error::Result<rpc::Value>>;
fn prepare(&self, method: &str, params: Vec<rpc::Value>) -> (RequestId, rpc::Call) {
match *self {
Self::Left(ref a) => a.prepare(method, params),
Self::Right(ref b) => b.prepare(method, params),
}
}
fn send(&self, id: RequestId, request: rpc::Call, options: CallOptions) -> Self::Out {
match *self {
Self::Left(ref a) => a.send(id, request, options).boxed(),
Self::Right(ref b) => b.send(id, request, options).boxed(),
}
}
fn set_max_response_bytes(&mut self, v: u64) {
match *self {
Self::Left(ref mut a) => a.set_max_response_bytes(v),
Self::Right(ref mut b) => b.set_max_response_bytes(v),
}
}
}
impl<A, B, ABatch, BBatch> BatchTransport for Either<A, B>
where
A: BatchTransport<Batch = ABatch>,
B: BatchTransport<Batch = BBatch>,
A::Out: 'static + Send,
B::Out: 'static + Send,
ABatch: futures::Future<Output = error::Result<Vec<error::Result<rpc::Value>>>> + 'static + Send,
BBatch: futures::Future<Output = error::Result<Vec<error::Result<rpc::Value>>>> + 'static + Send,
{
type Batch = BoxFuture<'static, error::Result<Vec<error::Result<rpc::Value>>>>;
fn send_batch<T>(&self, requests: T) -> Self::Batch
where
T: IntoIterator<Item = (RequestId, rpc::Call)>,
{
match *self {
Self::Left(ref a) => a.send_batch(requests).boxed(),
Self::Right(ref b) => b.send_batch(requests).boxed(),
}
}
}
impl<A, B, AStream, BStream> DuplexTransport for Either<A, B>
where
A: DuplexTransport<NotificationStream = AStream>,
B: DuplexTransport<NotificationStream = BStream>,
A::Out: 'static + Send,
B::Out: 'static + Send,
AStream: futures::Stream<Item = rpc::Value> + 'static + Send,
BStream: futures::Stream<Item = rpc::Value> + 'static + Send,
{
type NotificationStream = BoxStream<'static, rpc::Value>;
fn subscribe(&self, id: api::SubscriptionId) -> error::Result<Self::NotificationStream> {
Ok(match *self {
Self::Left(ref a) => a.subscribe(id)?.boxed(),
Self::Right(ref b) => b.subscribe(id)?.boxed(),
})
}
fn unsubscribe(&self, id: api::SubscriptionId) -> error::Result {
match *self {
Self::Left(ref a) => a.unsubscribe(id),
Self::Right(ref b) => b.unsubscribe(id),
}
}
}