box_open_sdk/managers/
hub_items.rs1use crate::internal::path_escape;
4use crate::runtime::{self, Error};
5
6#[derive(Clone, Debug, Default)]
8pub struct HubItemsListOptions {
9 pub parent_id: Option<String>,
10 pub marker: Option<String>,
11 pub limit: Option<i64>,
12}
13
14pub struct HubItemsListPaginator {
17 manager: HubItemsManager,
18 hub_id: String,
19 options: HubItemsListOptions,
20 buffer: std::vec::IntoIter<crate::models::schemas::HubItem>,
21 done: bool,
22}
23
24impl HubItemsListPaginator {
25 pub async fn next(&mut self) -> Option<Result<crate::models::schemas::HubItem, Error>> {
28 loop {
29 if let Some(item) = self.buffer.next() {
30 return Some(Ok(item));
31 }
32 if self.done {
33 return None;
34 }
35 let page = match self
36 .manager
37 .list_page(self.hub_id.clone(), Some(self.options.clone()))
38 .await
39 {
40 Ok(page) => page,
41 Err(err) => {
42 self.done = true;
43 return Some(Err(err));
44 }
45 };
46 self.buffer = page.entries.unwrap_or_default().into_iter();
47 match page.next_marker.flatten() {
48 Some(cursor) if !cursor.is_empty() => self.options.marker = Some(cursor),
49 _ => self.done = true,
50 }
51 }
52 }
53}
54
55pub struct HubItemsManager {
57 session: std::sync::Arc<runtime::Client>,
58}
59
60impl HubItemsManager {
61 pub(crate) fn new(session: std::sync::Arc<runtime::Client>) -> Self {
62 Self { session }
63 }
64
65 async fn list_page(
66 &self,
67 hub_id: String,
68 opts: Option<HubItemsListOptions>,
69 ) -> Result<crate::models::schemas::HubItems, Error> {
70 let mut url = self.session.base_url("api");
71 url.push_str("/hub_items");
72 let mut req = self.session.new_request("GET", &url);
73 req = runtime::with_query(req, "hub_id", &hub_id);
74 let opts = opts.unwrap_or_default();
75 if let Some(value) = opts.parent_id {
76 req = runtime::with_query(req, "parent_id", &value);
77 }
78 if let Some(value) = opts.marker {
79 req = runtime::with_query(req, "marker", &value);
80 }
81 if let Some(value) = opts.limit {
82 req = runtime::with_query(req, "limit", &value.to_string());
83 }
84 req = runtime::with_header(req, "box-version", "2025.0");
85 let resp = self.session.fetch(req).await?;
86 let data = runtime::response_bytes(&resp)?;
87 Ok(serde_json::from_slice(&data)?)
88 }
89
90 pub fn list(&self, hub_id: String, opts: Option<HubItemsListOptions>) -> HubItemsListPaginator {
92 HubItemsListPaginator {
93 manager: HubItemsManager::new(self.session.clone()),
94 hub_id,
95 options: opts.unwrap_or_default(),
96 buffer: Vec::new().into_iter(),
97 done: false,
98 }
99 }
100
101 pub async fn create_hub_manage_items(
102 &self,
103 hub_id: String,
104 body: crate::models::schemas::HubItemsManageRequest,
105 ) -> Result<crate::models::schemas::HubItemsManageResponse, Error> {
106 let mut url = self.session.base_url("api");
107 url.push_str("/hubs");
108 url.push('/');
109 let seg = path_escape(&hub_id);
110 url.push_str(&seg);
111 url.push_str("/manage_items");
112 let mut req = self.session.new_request("POST", &url);
113 req = runtime::with_header(req, "box-version", "2025.0");
114 let payload = serde_json::to_vec(&body)?;
115 req = runtime::with_json_body(req, &payload);
116 let resp = self.session.fetch(req).await?;
117 let data = runtime::response_bytes(&resp)?;
118 Ok(serde_json::from_slice(&data)?)
119 }
120}