use std::future::Future;
use serde_json::Value;
use crate::core::{
check_registration, Error, IntoProxySpec, Result, CREATE_CONTEXT_METHOD, CREDENTIALS_METHOD,
};
use crate::registration::{with_proxy_registration, with_proxy_registration_blocking, ConnKey};
pub async fn new_proxy_context_with<P, S, SFut, C, CFut, T, E>(
conn: impl Into<ConnKey>,
proxy: P,
send: S,
create_context: C,
) -> Result<T, E>
where
P: IntoProxySpec,
S: FnOnce(&'static str, Value) -> SFut,
SFut: Future<Output = Result<Value, E>>,
C: FnOnce(String) -> CFut,
CFut: Future<Output = Result<T, E>>,
E: From<Error>,
{
with_proxy_registration(conn, proxy, |spec| async move {
let result = send(CREDENTIALS_METHOD, spec.credentials_params().to_value()).await?;
check_registration(&result).map_err(E::from)?;
create_context(spec.server().to_string()).await
})
.await
}
pub async fn new_proxy_context_raw<P, S, SFut, E>(
conn: impl Into<ConnKey>,
proxy: P,
send: S,
) -> Result<Value, E>
where
P: IntoProxySpec,
S: FnMut(&'static str, Value) -> SFut,
SFut: Future<Output = Result<Value, E>>,
E: From<Error>,
{
let mut send = send;
with_proxy_registration(conn, proxy, |spec| async move {
let result = send(CREDENTIALS_METHOD, spec.credentials_params().to_value()).await?;
check_registration(&result).map_err(E::from)?;
let created = send(
CREATE_CONTEXT_METHOD,
spec.create_context_params().to_value(),
)
.await?;
check_registration(&created).map_err(E::from)?;
Ok(created)
})
.await
}
pub fn new_proxy_context_with_blocking<P, S, C, T, E>(
conn: impl Into<ConnKey>,
proxy: P,
send: S,
create_context: C,
) -> Result<T, E>
where
P: IntoProxySpec,
S: FnOnce(&'static str, Value) -> Result<Value, E>,
C: FnOnce(&str) -> Result<T, E>,
E: From<Error>,
{
with_proxy_registration_blocking(conn, proxy, |spec| {
let result = send(CREDENTIALS_METHOD, spec.credentials_params().to_value())?;
check_registration(&result).map_err(E::from)?;
create_context(spec.server())
})
}
pub fn new_proxy_context_raw_blocking<P, S, E>(
conn: impl Into<ConnKey>,
proxy: P,
send: S,
) -> Result<Value, E>
where
P: IntoProxySpec,
S: FnMut(&'static str, Value) -> Result<Value, E>,
E: From<Error>,
{
let mut send = send;
with_proxy_registration_blocking(conn, proxy, |spec| {
let result = send(CREDENTIALS_METHOD, spec.credentials_params().to_value())?;
check_registration(&result).map_err(E::from)?;
let created = send(
CREATE_CONTEXT_METHOD,
spec.create_context_params().to_value(),
)?;
check_registration(&created).map_err(E::from)?;
Ok(created)
})
}