use std::future::Future;
use std::sync::Arc;
use tokio::runtime::Runtime;
use crate::error::Error;
use crate::transport::HttpResponse;
use crate::RawRequest;
#[derive(Debug, Clone)]
pub struct Client {
inner: crate::Client,
runtime: Arc<Runtime>,
}
impl Client {
pub fn new(base_url: impl Into<String>) -> Result<Self, Error> {
crate::Client::new(base_url).and_then(Self::from_async)
}
pub fn from_async(inner: crate::Client) -> Result<Self, Error> {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.thread_name("crypto-service-sdk")
.enable_all()
.build()
.map(|runtime| Client {
inner,
runtime: Arc::new(runtime),
})
.map_err(runtime_failed)
}
#[must_use]
pub fn inner(&self) -> &crate::Client {
&self.inner
}
#[must_use]
pub fn base_url(&self) -> &str {
self.inner.base_url()
}
pub fn send_raw(&self, request: RawRequest) -> Result<HttpResponse, Error> {
self.block_on(self.inner.send_raw(request))
}
pub(crate) fn block_on<F: Future>(&self, future: F) -> F::Output {
self.runtime.block_on(future)
}
}
fn runtime_failed(_: std::io::Error) -> Error {
Error::Config("the blocking client's runtime could not start")
}
#[cfg(test)]
mod tests;