use crate::runtime::{self, Error};
#[derive(Clone, Debug, Default)]
pub struct HubDocumentListPagesOptions {
pub marker: Option<String>,
pub limit: Option<i64>,
}
#[derive(Clone, Debug, Default)]
pub struct HubDocumentListBlocksOptions {
pub marker: Option<String>,
pub limit: Option<i64>,
}
pub struct HubDocumentListPagesPaginator {
manager: HubDocumentManager,
hub_id: String,
options: HubDocumentListPagesOptions,
buffer: std::vec::IntoIter<crate::models::schemas::HubDocumentPage>,
done: bool,
}
impl HubDocumentListPagesPaginator {
pub async fn next(&mut self) -> Option<Result<crate::models::schemas::HubDocumentPage, Error>> {
loop {
if let Some(item) = self.buffer.next() {
return Some(Ok(item));
}
if self.done {
return None;
}
let page = match self
.manager
.list_pages_page(self.hub_id.clone(), Some(self.options.clone()))
.await
{
Ok(page) => page,
Err(err) => {
self.done = true;
return Some(Err(err));
}
};
self.buffer = page.entries.into_iter();
match page.next_marker.flatten() {
Some(cursor) if !cursor.is_empty() => self.options.marker = Some(cursor),
_ => self.done = true,
}
}
}
}
pub struct HubDocumentListBlocksPaginator {
manager: HubDocumentManager,
hub_id: String,
page_id: String,
options: HubDocumentListBlocksOptions,
buffer: std::vec::IntoIter<crate::models::schemas::HubDocumentBlockEntry>,
done: bool,
}
impl HubDocumentListBlocksPaginator {
pub async fn next(
&mut self,
) -> Option<Result<crate::models::schemas::HubDocumentBlockEntry, Error>> {
loop {
if let Some(item) = self.buffer.next() {
return Some(Ok(item));
}
if self.done {
return None;
}
let page = match self
.manager
.list_blocks_page(
self.hub_id.clone(),
self.page_id.clone(),
Some(self.options.clone()),
)
.await
{
Ok(page) => page,
Err(err) => {
self.done = true;
return Some(Err(err));
}
};
self.buffer = page.entries.into_iter();
match page.next_marker.flatten() {
Some(cursor) if !cursor.is_empty() => self.options.marker = Some(cursor),
_ => self.done = true,
}
}
}
}
pub struct HubDocumentManager {
session: std::sync::Arc<runtime::Client>,
}
impl HubDocumentManager {
pub(crate) fn new(session: std::sync::Arc<runtime::Client>) -> Self {
Self { session }
}
async fn list_pages_page(
&self,
hub_id: String,
opts: Option<HubDocumentListPagesOptions>,
) -> Result<crate::models::schemas::HubDocumentPages, Error> {
let mut url = self.session.base_url("api");
url.push_str("/hub_document_pages");
let mut req = self.session.new_request("GET", &url);
req = runtime::with_query(req, "hub_id", &hub_id);
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_pages(
&self,
hub_id: String,
opts: Option<HubDocumentListPagesOptions>,
) -> HubDocumentListPagesPaginator {
HubDocumentListPagesPaginator {
manager: HubDocumentManager::new(self.session.clone()),
hub_id,
options: opts.unwrap_or_default(),
buffer: Vec::new().into_iter(),
done: false,
}
}
async fn list_blocks_page(
&self,
hub_id: String,
page_id: String,
opts: Option<HubDocumentListBlocksOptions>,
) -> Result<crate::models::schemas::HubDocumentBlocks, Error> {
let mut url = self.session.base_url("api");
url.push_str("/hub_document_blocks");
let mut req = self.session.new_request("GET", &url);
req = runtime::with_query(req, "hub_id", &hub_id);
req = runtime::with_query(req, "page_id", &page_id);
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_blocks(
&self,
hub_id: String,
page_id: String,
opts: Option<HubDocumentListBlocksOptions>,
) -> HubDocumentListBlocksPaginator {
HubDocumentListBlocksPaginator {
manager: HubDocumentManager::new(self.session.clone()),
hub_id,
page_id,
options: opts.unwrap_or_default(),
buffer: Vec::new().into_iter(),
done: false,
}
}
}