Skip to main content

box_open_sdk/managers/
hubs.rs

1// Code generated by box-gantry. DO NOT EDIT.
2
3use crate::internal::path_escape;
4use crate::runtime::{self, Error};
5
6/// Optional parameters for [`HubsManager::list`].
7#[derive(Clone, Debug, Default)]
8pub struct HubsListOptions {
9    pub query: Option<String>,
10    pub scope: Option<String>,
11    pub sort: Option<String>,
12    pub direction: Option<crate::models::schemas::GetDirection>,
13    pub marker: Option<String>,
14    pub limit: Option<i64>,
15}
16
17/// Optional parameters for [`HubsManager::list_enterprise`].
18#[derive(Clone, Debug, Default)]
19pub struct HubsListEnterpriseOptions {
20    pub query: Option<String>,
21    pub sort: Option<String>,
22    pub direction: Option<crate::models::schemas::GetDirection>,
23    pub marker: Option<String>,
24    pub limit: Option<i64>,
25}
26
27/// Async paginator over [`HubsManager::list`], yielding one `crate::models::schemas::Hub`
28/// per item across pages (FR-7.3).
29pub struct HubsListPaginator {
30    manager: HubsManager,
31    options: HubsListOptions,
32    buffer: std::vec::IntoIter<crate::models::schemas::Hub>,
33    done: bool,
34}
35
36impl HubsListPaginator {
37    /// The next item, advancing pages as needed; `None` at the end.
38    /// An error is yielded once, then iteration stops.
39    pub async fn next(&mut self) -> Option<Result<crate::models::schemas::Hub, Error>> {
40        loop {
41            if let Some(item) = self.buffer.next() {
42                return Some(Ok(item));
43            }
44            if self.done {
45                return None;
46            }
47            let page = match self.manager.list_page(Some(self.options.clone())).await {
48                Ok(page) => page,
49                Err(err) => {
50                    self.done = true;
51                    return Some(Err(err));
52                }
53            };
54            self.buffer = page.entries.unwrap_or_default().into_iter();
55            match page.next_marker.flatten() {
56                Some(cursor) if !cursor.is_empty() => self.options.marker = Some(cursor),
57                _ => self.done = true,
58            }
59        }
60    }
61}
62
63/// Async paginator over [`HubsManager::list_enterprise`], yielding one `crate::models::schemas::Hub`
64/// per item across pages (FR-7.3).
65pub struct HubsListEnterprisePaginator {
66    manager: HubsManager,
67    options: HubsListEnterpriseOptions,
68    buffer: std::vec::IntoIter<crate::models::schemas::Hub>,
69    done: bool,
70}
71
72impl HubsListEnterprisePaginator {
73    /// The next item, advancing pages as needed; `None` at the end.
74    /// An error is yielded once, then iteration stops.
75    pub async fn next(&mut self) -> Option<Result<crate::models::schemas::Hub, Error>> {
76        loop {
77            if let Some(item) = self.buffer.next() {
78                return Some(Ok(item));
79            }
80            if self.done {
81                return None;
82            }
83            let page = match self
84                .manager
85                .list_enterprise_page(Some(self.options.clone()))
86                .await
87            {
88                Ok(page) => page,
89                Err(err) => {
90                    self.done = true;
91                    return Some(Err(err));
92                }
93            };
94            self.buffer = page.entries.unwrap_or_default().into_iter();
95            match page.next_marker.flatten() {
96                Some(cursor) if !cursor.is_empty() => self.options.marker = Some(cursor),
97                _ => self.done = true,
98            }
99        }
100    }
101}
102
103/// Operations for the "hubs" API area.
104pub struct HubsManager {
105    session: std::sync::Arc<runtime::Client>,
106}
107
108impl HubsManager {
109    pub(crate) fn new(session: std::sync::Arc<runtime::Client>) -> Self {
110        Self { session }
111    }
112
113    async fn list_page(
114        &self,
115        opts: Option<HubsListOptions>,
116    ) -> Result<crate::models::schemas::Hubs, Error> {
117        let mut url = self.session.base_url("api");
118        url.push_str("/hubs");
119        let mut req = self.session.new_request("GET", &url);
120        let opts = opts.unwrap_or_default();
121        if let Some(value) = opts.query {
122            req = runtime::with_query(req, "query", &value);
123        }
124        if let Some(value) = opts.scope {
125            req = runtime::with_query(req, "scope", &value);
126        }
127        if let Some(value) = opts.sort {
128            req = runtime::with_query(req, "sort", &value);
129        }
130        if let Some(value) = opts.direction {
131            req = runtime::with_query(req, "direction", &value.0);
132        }
133        if let Some(value) = opts.marker {
134            req = runtime::with_query(req, "marker", &value);
135        }
136        if let Some(value) = opts.limit {
137            req = runtime::with_query(req, "limit", &value.to_string());
138        }
139        req = runtime::with_header(req, "box-version", "2025.0");
140        let resp = self.session.fetch(req).await?;
141        let data = runtime::response_bytes(&resp)?;
142        Ok(serde_json::from_slice(&data)?)
143    }
144
145    /// Iterate every `crate::models::schemas::Hub` across pages, threading the cursor.
146    pub fn list(&self, opts: Option<HubsListOptions>) -> HubsListPaginator {
147        HubsListPaginator {
148            manager: HubsManager::new(self.session.clone()),
149            options: opts.unwrap_or_default(),
150            buffer: Vec::new().into_iter(),
151            done: false,
152        }
153    }
154
155    pub async fn create(
156        &self,
157        body: crate::models::schemas::HubCreateRequest,
158    ) -> Result<crate::models::schemas::Hub, Error> {
159        let mut url = self.session.base_url("api");
160        url.push_str("/hubs");
161        let mut req = self.session.new_request("POST", &url);
162        req = runtime::with_header(req, "box-version", "2025.0");
163        let payload = serde_json::to_vec(&body)?;
164        req = runtime::with_json_body(req, &payload);
165        let resp = self.session.fetch(req).await?;
166        let data = runtime::response_bytes(&resp)?;
167        Ok(serde_json::from_slice(&data)?)
168    }
169
170    async fn list_enterprise_page(
171        &self,
172        opts: Option<HubsListEnterpriseOptions>,
173    ) -> Result<crate::models::schemas::Hubs, Error> {
174        let mut url = self.session.base_url("api");
175        url.push_str("/enterprise_hubs");
176        let mut req = self.session.new_request("GET", &url);
177        let opts = opts.unwrap_or_default();
178        if let Some(value) = opts.query {
179            req = runtime::with_query(req, "query", &value);
180        }
181        if let Some(value) = opts.sort {
182            req = runtime::with_query(req, "sort", &value);
183        }
184        if let Some(value) = opts.direction {
185            req = runtime::with_query(req, "direction", &value.0);
186        }
187        if let Some(value) = opts.marker {
188            req = runtime::with_query(req, "marker", &value);
189        }
190        if let Some(value) = opts.limit {
191            req = runtime::with_query(req, "limit", &value.to_string());
192        }
193        req = runtime::with_header(req, "box-version", "2025.0");
194        let resp = self.session.fetch(req).await?;
195        let data = runtime::response_bytes(&resp)?;
196        Ok(serde_json::from_slice(&data)?)
197    }
198
199    /// Iterate every `crate::models::schemas::Hub` across pages, threading the cursor.
200    pub fn list_enterprise(
201        &self,
202        opts: Option<HubsListEnterpriseOptions>,
203    ) -> HubsListEnterprisePaginator {
204        HubsListEnterprisePaginator {
205            manager: HubsManager::new(self.session.clone()),
206            options: opts.unwrap_or_default(),
207            buffer: Vec::new().into_iter(),
208            done: false,
209        }
210    }
211
212    pub async fn get(&self, hub_id: String) -> Result<crate::models::schemas::Hub, Error> {
213        let mut url = self.session.base_url("api");
214        url.push_str("/hubs");
215        url.push('/');
216        let seg = path_escape(&hub_id);
217        url.push_str(&seg);
218        let mut req = self.session.new_request("GET", &url);
219        req = runtime::with_header(req, "box-version", "2025.0");
220        let resp = self.session.fetch(req).await?;
221        let data = runtime::response_bytes(&resp)?;
222        Ok(serde_json::from_slice(&data)?)
223    }
224
225    pub async fn update(
226        &self,
227        hub_id: String,
228        body: crate::models::schemas::HubUpdateRequest,
229    ) -> Result<crate::models::schemas::Hub, Error> {
230        let mut url = self.session.base_url("api");
231        url.push_str("/hubs");
232        url.push('/');
233        let seg = path_escape(&hub_id);
234        url.push_str(&seg);
235        let mut req = self.session.new_request("PUT", &url);
236        req = runtime::with_header(req, "box-version", "2025.0");
237        let payload = serde_json::to_vec(&body)?;
238        req = runtime::with_json_body(req, &payload);
239        let resp = self.session.fetch(req).await?;
240        let data = runtime::response_bytes(&resp)?;
241        Ok(serde_json::from_slice(&data)?)
242    }
243
244    pub async fn delete(&self, hub_id: String) -> Result<(), Error> {
245        let mut url = self.session.base_url("api");
246        url.push_str("/hubs");
247        url.push('/');
248        let seg = path_escape(&hub_id);
249        url.push_str(&seg);
250        let mut req = self.session.new_request("DELETE", &url);
251        req = runtime::with_header(req, "box-version", "2025.0");
252        let _ = self.session.fetch(req).await?;
253        Ok(())
254    }
255
256    pub async fn copy(
257        &self,
258        hub_id: String,
259        body: crate::models::schemas::HubCopyRequest,
260    ) -> Result<crate::models::schemas::Hub, Error> {
261        let mut url = self.session.base_url("api");
262        url.push_str("/hubs");
263        url.push('/');
264        let seg = path_escape(&hub_id);
265        url.push_str(&seg);
266        url.push_str("/copy");
267        let mut req = self.session.new_request("POST", &url);
268        req = runtime::with_header(req, "box-version", "2025.0");
269        let payload = serde_json::to_vec(&body)?;
270        req = runtime::with_json_body(req, &payload);
271        let resp = self.session.fetch(req).await?;
272        let data = runtime::response_bytes(&resp)?;
273        Ok(serde_json::from_slice(&data)?)
274    }
275}