box-open-sdk 0.4.1

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::internal::path_escape;
use crate::runtime::{self, Error};

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

/// Async paginator over [`ArchivesManager::list`], yielding one `crate::models::schemas::Archive`
/// per item across pages (FR-7.3).
pub struct ArchivesListPaginator {
    manager: ArchivesManager,
    options: ArchivesListOptions,
    buffer: std::vec::IntoIter<crate::models::schemas::Archive>,
    done: bool,
}

impl ArchivesListPaginator {
    /// 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::Archive, Error>> {
        loop {
            if let Some(item) = self.buffer.next() {
                return Some(Ok(item));
            }
            if self.done {
                return None;
            }
            let page = match self.manager.list_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,
            }
        }
    }
}

/// Operations for the "archives" API area.
pub struct ArchivesManager {
    session: std::sync::Arc<runtime::Client>,
}

impl ArchivesManager {
    pub(crate) fn new(session: std::sync::Arc<runtime::Client>) -> Self {
        Self { session }
    }

    async fn list_page(
        &self,
        opts: Option<ArchivesListOptions>,
    ) -> Result<crate::models::schemas::Archives, Error> {
        let mut url = self.session.base_url("api");
        url.push_str("/archives");
        let mut req = self.session.new_request("GET", &url);
        let opts = opts.unwrap_or_default();
        if let Some(value) = opts.limit {
            req = runtime::with_query(req, "limit", &value.to_string());
        }
        if let Some(value) = opts.marker {
            req = runtime::with_query(req, "marker", &value);
        }
        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::Archive` across pages, threading the cursor.
    pub fn list(&self, opts: Option<ArchivesListOptions>) -> ArchivesListPaginator {
        ArchivesListPaginator {
            manager: ArchivesManager::new(self.session.clone()),
            options: opts.unwrap_or_default(),
            buffer: Vec::new().into_iter(),
            done: false,
        }
    }

    pub async fn create(
        &self,
        body: crate::models::schemas::CreateArchiveRequest,
    ) -> Result<crate::models::schemas::Archive, Error> {
        let mut url = self.session.base_url("api");
        url.push_str("/archives");
        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)?)
    }

    pub async fn update(
        &self,
        archive_id: String,
        body: crate::models::schemas::UpdateArchiveRequest,
    ) -> Result<crate::models::schemas::Archive, Error> {
        let mut url = self.session.base_url("api");
        url.push_str("/archives");
        url.push('/');
        let seg = path_escape(&archive_id);
        url.push_str(&seg);
        let mut req = self.session.new_request("PUT", &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)?)
    }

    pub async fn delete(&self, archive_id: String) -> Result<(), Error> {
        let mut url = self.session.base_url("api");
        url.push_str("/archives");
        url.push('/');
        let seg = path_escape(&archive_id);
        url.push_str(&seg);
        let mut req = self.session.new_request("DELETE", &url);
        req = runtime::with_header(req, "box-version", "2025.0");
        let _ = self.session.fetch(req).await?;
        Ok(())
    }
}