use crate::error::{Error, Result};
use crate::types::{
Answer, AskRequest, Health, Library, Manual, PassageSearch, SearchRequest, SourceList,
SourceText,
};
#[derive(Debug)]
pub struct Client {
inner: crate::Client,
runtime: tokio::runtime::Runtime,
}
impl Client {
pub fn new(base: impl Into<String>) -> Result<Self> {
Self::from_async(crate::Client::new(base)?)
}
pub fn local() -> Result<Self> {
Self::from_async(crate::Client::local()?)
}
pub fn from_async(inner: crate::Client) -> Result<Self> {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(Error::Runtime)?;
Ok(Self { inner, runtime })
}
pub fn health(&self) -> Result<Health> {
self.runtime.block_on(self.inner.health())
}
pub fn library(&self) -> Result<Library> {
self.runtime.block_on(self.inner.library())
}
pub fn ask(&self, request: &AskRequest) -> Result<Answer> {
self.runtime.block_on(self.inner.ask(request))
}
pub fn search_passages(
&self,
notebook_id: &str,
request: &SearchRequest,
) -> Result<PassageSearch> {
self.runtime
.block_on(self.inner.search_passages(notebook_id, request))
}
pub fn sources(&self, notebook_id: &str) -> Result<SourceList> {
self.runtime.block_on(self.inner.sources(notebook_id))
}
pub fn source(
&self,
notebook_id: &str,
source_id: &str,
cursor: Option<u64>,
) -> Result<SourceText> {
self.runtime
.block_on(self.inner.source(notebook_id, source_id, cursor))
}
pub fn manual(&self) -> Result<Manual> {
self.runtime.block_on(self.inner.manual())
}
}