use std::fmt::Debug;
use std::sync::Arc;
use nyquest_interface::{r#async::AnyAsyncClient, register::BACKEND};
use super::response::Response;
use crate::ClientBuilder;
#[derive(Clone)]
pub struct AsyncClient {
pub(super) client: Arc<dyn AnyAsyncClient>,
}
impl ClientBuilder {
pub async fn build_async(self) -> crate::Result<AsyncClient> {
Ok(AsyncClient {
client: BACKEND
.get()
.expect("No backend registered. Please find a backend crate (e.g. nyquest-preset) and call the `register` method at program startup.")
.create_async_client(self.options)
.await?
})
}
}
impl AsyncClient {
pub async fn request(&self, req: super::Request) -> crate::Result<Response> {
let res = self.client.request(req.inner).await?;
Ok(res.into())
}
}
impl Debug for AsyncClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.client.describe(f)
}
}