use std::collections::HashMap;
use std::path::Path;
use std::time::Duration;
use crate::error::DoclingError;
use crate::models::enums::TargetName;
use crate::models::requests::{ConvertDocumentsRequest, ConvertDocumentsRequestOptions};
use crate::models::responses::{ConvertDocumentResponse, HealthCheckResponse, TaskStatusResponse};
pub struct DoclingClient {
runtime: tokio::runtime::Runtime,
inner: crate::client::DoclingClient,
}
impl DoclingClient {
pub fn new(base_url: impl Into<String>) -> Self {
let runtime = tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime");
let inner = crate::client::DoclingClient::new(base_url);
Self { runtime, inner }
}
pub fn with_api_key(base_url: impl Into<String>, api_key: impl Into<String>) -> Self {
let runtime = tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime");
let inner = crate::client::DoclingClient::with_api_key(base_url, api_key);
Self { runtime, inner }
}
pub fn health(&self) -> Result<HealthCheckResponse, DoclingError> {
self.runtime.block_on(self.inner.health())
}
pub fn version(&self) -> Result<HashMap<String, serde_json::Value>, DoclingError> {
self.runtime.block_on(self.inner.version())
}
pub fn convert_source(
&self,
url: &str,
options: Option<ConvertDocumentsRequestOptions>,
) -> Result<ConvertDocumentResponse, DoclingError> {
self.runtime
.block_on(self.inner.convert_source(url, options))
}
pub fn convert(
&self,
request: &ConvertDocumentsRequest,
) -> Result<ConvertDocumentResponse, DoclingError> {
self.runtime.block_on(self.inner.convert(request))
}
pub fn convert_source_async(
&self,
url: &str,
options: Option<ConvertDocumentsRequestOptions>,
) -> Result<TaskStatusResponse, DoclingError> {
self.runtime
.block_on(self.inner.convert_source_async(url, options))
}
pub fn convert_async(
&self,
request: &ConvertDocumentsRequest,
) -> Result<TaskStatusResponse, DoclingError> {
self.runtime.block_on(self.inner.convert_async(request))
}
pub fn poll_task_status(
&self,
task_id: &str,
wait_secs: Option<f64>,
) -> Result<TaskStatusResponse, DoclingError> {
self.runtime
.block_on(self.inner.poll_task_status(task_id, wait_secs))
}
pub fn get_task_result(&self, task_id: &str) -> Result<ConvertDocumentResponse, DoclingError> {
self.runtime.block_on(self.inner.get_task_result(task_id))
}
pub fn wait_for_conversion(
&self,
url: &str,
options: Option<ConvertDocumentsRequestOptions>,
timeout: Duration,
poll_interval_secs: Option<f64>,
) -> Result<ConvertDocumentResponse, DoclingError> {
self.runtime.block_on(self.inner.wait_for_conversion(
url,
options,
timeout,
poll_interval_secs,
))
}
pub fn convert_file(
&self,
file_paths: &[impl AsRef<Path>],
options: Option<&ConvertDocumentsRequestOptions>,
target_type: Option<&TargetName>,
) -> Result<ConvertDocumentResponse, DoclingError> {
self.runtime
.block_on(self.inner.convert_file(file_paths, options, target_type))
}
pub fn convert_file_async(
&self,
file_paths: &[impl AsRef<Path>],
options: Option<&ConvertDocumentsRequestOptions>,
target_type: Option<&TargetName>,
) -> Result<TaskStatusResponse, DoclingError> {
self.runtime.block_on(
self.inner
.convert_file_async(file_paths, options, target_type),
)
}
pub fn wait_for_file_conversion(
&self,
file_paths: &[impl AsRef<Path>],
options: Option<&ConvertDocumentsRequestOptions>,
target_type: Option<&TargetName>,
timeout: Duration,
poll_interval_secs: Option<f64>,
) -> Result<ConvertDocumentResponse, DoclingError> {
self.runtime.block_on(self.inner.wait_for_file_conversion(
file_paths,
options,
target_type,
timeout,
poll_interval_secs,
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn blocking_client_new() {
let _client = DoclingClient::new("http://localhost:5001");
assert!(true);
}
#[test]
fn blocking_client_with_api_key() {
let _client = DoclingClient::with_api_key("http://localhost:5001", "test-key");
assert!(true);
}
}