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 [`StoragePoliciesManager::list`].
#[derive(Clone, Debug, Default)]
pub struct StoragePoliciesListOptions {
    pub fields: Option<Vec<String>>,
    pub marker: Option<String>,
    pub limit: Option<i64>,
}

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

impl StoragePoliciesListPaginator {
    /// 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::StoragePolicy, 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 "storage_policies" API area.
pub struct StoragePoliciesManager {
    session: std::sync::Arc<runtime::Client>,
}

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

    async fn list_page(
        &self,
        opts: Option<StoragePoliciesListOptions>,
    ) -> Result<crate::models::schemas::StoragePolicies, Error> {
        let mut url = self.session.base_url("api");
        url.push_str("/storage_policies");
        let mut req = self.session.new_request("GET", &url);
        let opts = opts.unwrap_or_default();
        if let Some(value) = opts.fields {
            req = runtime::with_query(req, "fields", &value.join(","));
        }
        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());
        }
        let resp = self.session.fetch(req).await?;
        let data = runtime::response_bytes(&resp)?;
        Ok(serde_json::from_slice(&data)?)
    }

    /// Iterate every `crate::models::schemas::StoragePolicy` across pages, threading the cursor.
    pub fn list(&self, opts: Option<StoragePoliciesListOptions>) -> StoragePoliciesListPaginator {
        StoragePoliciesListPaginator {
            manager: StoragePoliciesManager::new(self.session.clone()),
            options: opts.unwrap_or_default(),
            buffer: Vec::new().into_iter(),
            done: false,
        }
    }

    pub async fn get(
        &self,
        storage_policy_id: String,
    ) -> Result<crate::models::schemas::StoragePolicy, Error> {
        let mut url = self.session.base_url("api");
        url.push_str("/storage_policies");
        url.push('/');
        let seg = path_escape(&storage_policy_id);
        url.push_str(&seg);
        let req = self.session.new_request("GET", &url);
        let resp = self.session.fetch(req).await?;
        let data = runtime::response_bytes(&resp)?;
        Ok(serde_json::from_slice(&data)?)
    }
}