box-open-sdk 0.3.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 [`AppItemAssociationsManager::list_file`].
#[derive(Clone, Debug, Default)]
pub struct AppItemAssociationsListFileOptions {
    pub limit: Option<i64>,
    pub marker: Option<String>,
    pub application_type: Option<String>,
}

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

/// Async paginator over [`AppItemAssociationsManager::list_file`], yielding one `crate::models::schemas::AppItemAssociation`
/// per item across pages (FR-7.3).
pub struct AppItemAssociationsListFilePaginator {
    manager: AppItemAssociationsManager,
    file_id: String,
    options: AppItemAssociationsListFileOptions,
    buffer: std::vec::IntoIter<crate::models::schemas::AppItemAssociation>,
    done: bool,
}

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

/// Async paginator over [`AppItemAssociationsManager::list_folder`], yielding one `crate::models::schemas::AppItemAssociation`
/// per item across pages (FR-7.3).
pub struct AppItemAssociationsListFolderPaginator {
    manager: AppItemAssociationsManager,
    folder_id: String,
    options: AppItemAssociationsListFolderOptions,
    buffer: std::vec::IntoIter<crate::models::schemas::AppItemAssociation>,
    done: bool,
}

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

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

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

    async fn list_file_page(
        &self,
        file_id: String,
        opts: Option<AppItemAssociationsListFileOptions>,
    ) -> Result<crate::models::schemas::AppItemAssociations, Error> {
        let mut url = self.session.base_url("api");
        url.push_str("/files");
        url.push('/');
        let seg = path_escape(&file_id);
        url.push_str(&seg);
        url.push_str("/app_item_associations");
        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);
        }
        if let Some(value) = opts.application_type {
            req = runtime::with_query(req, "application_type", &value);
        }
        let resp = self.session.fetch(req).await?;
        let data = runtime::response_bytes(&resp)?;
        Ok(serde_json::from_slice(&data)?)
    }

    /// Iterate every `crate::models::schemas::AppItemAssociation` across pages, threading the cursor.
    pub fn list_file(
        &self,
        file_id: String,
        opts: Option<AppItemAssociationsListFileOptions>,
    ) -> AppItemAssociationsListFilePaginator {
        AppItemAssociationsListFilePaginator {
            manager: AppItemAssociationsManager::new(self.session.clone()),
            file_id,
            options: opts.unwrap_or_default(),
            buffer: Vec::new().into_iter(),
            done: false,
        }
    }

    async fn list_folder_page(
        &self,
        folder_id: String,
        opts: Option<AppItemAssociationsListFolderOptions>,
    ) -> Result<crate::models::schemas::AppItemAssociations, Error> {
        let mut url = self.session.base_url("api");
        url.push_str("/folders");
        url.push('/');
        let seg = path_escape(&folder_id);
        url.push_str(&seg);
        url.push_str("/app_item_associations");
        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);
        }
        if let Some(value) = opts.application_type {
            req = runtime::with_query(req, "application_type", &value);
        }
        let resp = self.session.fetch(req).await?;
        let data = runtime::response_bytes(&resp)?;
        Ok(serde_json::from_slice(&data)?)
    }

    /// Iterate every `crate::models::schemas::AppItemAssociation` across pages, threading the cursor.
    pub fn list_folder(
        &self,
        folder_id: String,
        opts: Option<AppItemAssociationsListFolderOptions>,
    ) -> AppItemAssociationsListFolderPaginator {
        AppItemAssociationsListFolderPaginator {
            manager: AppItemAssociationsManager::new(self.session.clone()),
            folder_id,
            options: opts.unwrap_or_default(),
            buffer: Vec::new().into_iter(),
            done: false,
        }
    }
}