fedimint_api_client/api/global_api/
with_request_hook.rsuse std::collections::BTreeSet;
use std::sync::Arc;
use fedimint_core::core::ModuleInstanceId;
use fedimint_core::module::ApiRequestErased;
use fedimint_core::task::{MaybeSend, MaybeSync};
use fedimint_core::{apply, async_trait_maybe_send, maybe_add_send_sync, PeerId};
use serde_json::Value;
use super::super::{DynModuleApi, IRawFederationApi};
use crate::api::PeerResult;
pub type ApiRequestHook =
Arc<maybe_add_send_sync!(dyn Fn(DynIRawFederationApi) -> DynIRawFederationApi + 'static)>;
pub type DynIRawFederationApi = Box<maybe_add_send_sync!(dyn IRawFederationApi + 'static)>;
pub trait RawFederationApiWithRequestHookExt
where
Self: Sized,
{
fn with_request_hook(self, hook: &ApiRequestHook) -> RawFederationApiWithRequestHook;
}
impl<T> RawFederationApiWithRequestHookExt for T
where
T: IRawFederationApi + MaybeSend + MaybeSync + 'static,
{
fn with_request_hook(self, hook: &ApiRequestHook) -> RawFederationApiWithRequestHook {
RawFederationApiWithRequestHook::new(self, hook)
}
}
#[derive(Debug)]
pub struct RawFederationApiWithRequestHook {
pub(crate) inner: DynIRawFederationApi,
}
impl RawFederationApiWithRequestHook {
pub fn new<T>(inner: T, hook: &ApiRequestHook) -> RawFederationApiWithRequestHook
where
T: IRawFederationApi + MaybeSend + MaybeSync + 'static,
{
RawFederationApiWithRequestHook {
inner: hook(Box::new(inner)),
}
}
}
#[apply(async_trait_maybe_send!)]
impl IRawFederationApi for RawFederationApiWithRequestHook {
fn all_peers(&self) -> &BTreeSet<PeerId> {
self.inner.all_peers()
}
fn self_peer(&self) -> Option<PeerId> {
self.inner.self_peer()
}
fn with_module(&self, id: ModuleInstanceId) -> DynModuleApi {
self.inner.with_module(id)
}
async fn request_raw(
&self,
peer_id: PeerId,
method: &str,
params: &ApiRequestErased,
) -> PeerResult<Value> {
self.inner.request_raw(peer_id, method, params).await
}
}