use crate::internal::path_escape;
use crate::runtime::{self, Error};
#[derive(Clone, Debug, Default)]
pub struct DocgenListJobsOptions {
pub marker: Option<String>,
pub limit: Option<i64>,
}
#[derive(Clone, Debug, Default)]
pub struct DocgenListBatchJobOptions {
pub marker: Option<String>,
pub limit: Option<i64>,
}
pub struct DocgenListJobsPaginator {
manager: DocgenManager,
options: DocgenListJobsOptions,
buffer: std::vec::IntoIter<crate::models::schemas::DocGenJobFull>,
done: bool,
}
impl DocgenListJobsPaginator {
pub async fn next(&mut self) -> Option<Result<crate::models::schemas::DocGenJobFull, Error>> {
loop {
if let Some(item) = self.buffer.next() {
return Some(Ok(item));
}
if self.done {
return None;
}
let page = match self
.manager
.list_jobs_page(Some(self.options.clone()))
.await
{
Ok(page) => page,
Err(err) => {
self.done = true;
return Some(Err(err));
}
};
self.buffer = page.entries.unwrap_or_default().into_iter();
match page.next_marker.flatten() {
Some(cursor) if !cursor.is_empty() => self.options.marker = Some(cursor),
_ => self.done = true,
}
}
}
}
pub struct DocgenListBatchJobPaginator {
manager: DocgenManager,
batch_id: String,
options: DocgenListBatchJobOptions,
buffer: std::vec::IntoIter<crate::models::schemas::DocGenJob>,
done: bool,
}
impl DocgenListBatchJobPaginator {
pub async fn next(&mut self) -> Option<Result<crate::models::schemas::DocGenJob, Error>> {
loop {
if let Some(item) = self.buffer.next() {
return Some(Ok(item));
}
if self.done {
return None;
}
let page = match self
.manager
.list_batch_job_page(self.batch_id.clone(), Some(self.options.clone()))
.await
{
Ok(page) => page,
Err(err) => {
self.done = true;
return Some(Err(err));
}
};
self.buffer = page.entries.unwrap_or_default().into_iter();
match page.next_marker.flatten() {
Some(cursor) if !cursor.is_empty() => self.options.marker = Some(cursor),
_ => self.done = true,
}
}
}
}
pub struct DocgenManager {
session: std::sync::Arc<runtime::Client>,
}
impl DocgenManager {
pub(crate) fn new(session: std::sync::Arc<runtime::Client>) -> Self {
Self { session }
}
pub async fn get_job(
&self,
job_id: String,
) -> Result<crate::models::schemas::DocGenJob, Error> {
let mut url = self.session.base_url("api");
url.push_str("/docgen_jobs");
url.push('/');
let seg = path_escape(&job_id);
url.push_str(&seg);
let mut req = self.session.new_request("GET", &url);
req = runtime::with_header(req, "box-version", "2025.0");
let resp = self.session.fetch(req).await?;
let data = runtime::response_bytes(&resp)?;
Ok(serde_json::from_slice(&data)?)
}
async fn list_jobs_page(
&self,
opts: Option<DocgenListJobsOptions>,
) -> Result<crate::models::schemas::DocGenJobsFull, Error> {
let mut url = self.session.base_url("api");
url.push_str("/docgen_jobs");
let mut req = self.session.new_request("GET", &url);
let opts = opts.unwrap_or_default();
if let Some(value) = opts.marker {
req = runtime::with_query(req, "marker", &value);
}
if let Some(value) = opts.limit {
req = runtime::with_query(req, "limit", &value.to_string());
}
req = runtime::with_header(req, "box-version", "2025.0");
let resp = self.session.fetch(req).await?;
let data = runtime::response_bytes(&resp)?;
Ok(serde_json::from_slice(&data)?)
}
pub fn list_jobs(&self, opts: Option<DocgenListJobsOptions>) -> DocgenListJobsPaginator {
DocgenListJobsPaginator {
manager: DocgenManager::new(self.session.clone()),
options: opts.unwrap_or_default(),
buffer: Vec::new().into_iter(),
done: false,
}
}
async fn list_batch_job_page(
&self,
batch_id: String,
opts: Option<DocgenListBatchJobOptions>,
) -> Result<crate::models::schemas::DocGenJobs, Error> {
let mut url = self.session.base_url("api");
url.push_str("/docgen_batch_jobs");
url.push('/');
let seg = path_escape(&batch_id);
url.push_str(&seg);
let mut req = self.session.new_request("GET", &url);
let opts = opts.unwrap_or_default();
if let Some(value) = opts.marker {
req = runtime::with_query(req, "marker", &value);
}
if let Some(value) = opts.limit {
req = runtime::with_query(req, "limit", &value.to_string());
}
req = runtime::with_header(req, "box-version", "2025.0");
let resp = self.session.fetch(req).await?;
let data = runtime::response_bytes(&resp)?;
Ok(serde_json::from_slice(&data)?)
}
pub fn list_batch_job(
&self,
batch_id: String,
opts: Option<DocgenListBatchJobOptions>,
) -> DocgenListBatchJobPaginator {
DocgenListBatchJobPaginator {
manager: DocgenManager::new(self.session.clone()),
batch_id,
options: opts.unwrap_or_default(),
buffer: Vec::new().into_iter(),
done: false,
}
}
pub async fn create_batches(
&self,
body: crate::models::schemas::DocGenBatchCreateRequest,
) -> Result<crate::models::schemas::DocGenBatchBase, Error> {
let mut url = self.session.base_url("api");
url.push_str("/docgen_batches");
let mut req = self.session.new_request("POST", &url);
req = runtime::with_header(req, "box-version", "2025.0");
let payload = serde_json::to_vec(&body)?;
req = runtime::with_json_body(req, &payload);
let resp = self.session.fetch(req).await?;
let data = runtime::response_bytes(&resp)?;
Ok(serde_json::from_slice(&data)?)
}
}