box-open-sdk 0.3.0

Box API client for Rust (open source, community, punk rock) — typed models, async managers, and a reqwest runtime with retry, backoff, and token refresh.
Documentation
// Code generated by box-gantry. DO NOT EDIT.

use crate::runtime::{self, Error};

/// Optional parameters for [`HubDocumentManager::list_pages`].
#[derive(Clone, Debug, Default)]
pub struct HubDocumentListPagesOptions {
    pub marker: Option<String>,
    pub limit: Option<i64>,
}

/// Optional parameters for [`HubDocumentManager::list_blocks`].
#[derive(Clone, Debug, Default)]
pub struct HubDocumentListBlocksOptions {
    pub marker: Option<String>,
    pub limit: Option<i64>,
}

/// Async paginator over [`HubDocumentManager::list_pages`], yielding one `crate::models::schemas::HubDocumentPage`
/// per item across pages (FR-7.3).
pub struct HubDocumentListPagesPaginator {
    manager: HubDocumentManager,
    hub_id: String,
    options: HubDocumentListPagesOptions,
    buffer: std::vec::IntoIter<crate::models::schemas::HubDocumentPage>,
    done: bool,
}

impl HubDocumentListPagesPaginator {
    /// The next item, advancing pages as needed; `None` at the end.
    /// An error is yielded once, then iteration stops.
    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,
            }
        }
    }
}

/// Async paginator over [`HubDocumentManager::list_blocks`], yielding one `crate::models::schemas::HubDocumentBlockEntry`
/// per item across pages (FR-7.3).
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 {
    /// The next item, advancing pages as needed; `None` at the end.
    /// An error is yielded once, then iteration stops.
    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,
            }
        }
    }
}

/// Operations for the "hub_document" API area.
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)?)
    }

    /// Iterate every `crate::models::schemas::HubDocumentPage` across pages, threading the cursor.
    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)?)
    }

    /// Iterate every `crate::models::schemas::HubDocumentBlockEntry` across pages, threading the cursor.
    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,
        }
    }
}