use std::future::{Future, IntoFuture};
use std::ops::Deref;
use std::sync::Arc;
use crate::client::{ClientBuilder, Credentials};
use crate::error::{Error, Result};
#[derive(Clone, Debug)]
pub struct Client {
inner: crate::Client,
runtime: Arc<tokio::runtime::Runtime>,
}
impl Client {
pub fn connect(url: impl AsRef<str>, credentials: Credentials) -> Result<Self> {
Self::from_builder(crate::Client::builder(url).credentials(credentials))
}
pub fn from_builder(builder: ClientBuilder) -> Result<Self> {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|e| Error::Transport(Box::new(e)))?;
let inner = runtime.block_on(builder.connect())?;
Ok(Self {
inner,
runtime: Arc::new(runtime),
})
}
pub fn run<F: IntoFuture>(&self, request: F) -> F::Output {
self.runtime.block_on(request.into_future())
}
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
self.runtime.block_on(future)
}
#[must_use]
pub const fn as_async(&self) -> &crate::Client {
&self.inner
}
}
impl Deref for Client {
type Target = crate::Client;
fn deref(&self) -> &Self::Target {
&self.inner
}
}