box_open_sdk/managers/
hub_document.rs1use crate::runtime::{self, Error};
4
5#[derive(Clone, Debug, Default)]
7pub struct HubDocumentListPagesOptions {
8 pub marker: Option<String>,
9 pub limit: Option<i64>,
10}
11
12#[derive(Clone, Debug, Default)]
14pub struct HubDocumentListBlocksOptions {
15 pub marker: Option<String>,
16 pub limit: Option<i64>,
17}
18
19pub struct HubDocumentListPagesPaginator {
22 manager: HubDocumentManager,
23 hub_id: String,
24 options: HubDocumentListPagesOptions,
25 buffer: std::vec::IntoIter<crate::models::schemas::HubDocumentPage>,
26 done: bool,
27}
28
29impl HubDocumentListPagesPaginator {
30 pub async fn next(&mut self) -> Option<Result<crate::models::schemas::HubDocumentPage, Error>> {
33 loop {
34 if let Some(item) = self.buffer.next() {
35 return Some(Ok(item));
36 }
37 if self.done {
38 return None;
39 }
40 let page = match self
41 .manager
42 .list_pages_page(self.hub_id.clone(), Some(self.options.clone()))
43 .await
44 {
45 Ok(page) => page,
46 Err(err) => {
47 self.done = true;
48 return Some(Err(err));
49 }
50 };
51 self.buffer = page.entries.into_iter();
52 match page.next_marker.flatten() {
53 Some(cursor) if !cursor.is_empty() => self.options.marker = Some(cursor),
54 _ => self.done = true,
55 }
56 }
57 }
58}
59
60pub struct HubDocumentListBlocksPaginator {
63 manager: HubDocumentManager,
64 hub_id: String,
65 page_id: String,
66 options: HubDocumentListBlocksOptions,
67 buffer: std::vec::IntoIter<crate::models::schemas::HubDocumentBlockEntry>,
68 done: bool,
69}
70
71impl HubDocumentListBlocksPaginator {
72 pub async fn next(
75 &mut self,
76 ) -> Option<Result<crate::models::schemas::HubDocumentBlockEntry, Error>> {
77 loop {
78 if let Some(item) = self.buffer.next() {
79 return Some(Ok(item));
80 }
81 if self.done {
82 return None;
83 }
84 let page = match self
85 .manager
86 .list_blocks_page(
87 self.hub_id.clone(),
88 self.page_id.clone(),
89 Some(self.options.clone()),
90 )
91 .await
92 {
93 Ok(page) => page,
94 Err(err) => {
95 self.done = true;
96 return Some(Err(err));
97 }
98 };
99 self.buffer = page.entries.into_iter();
100 match page.next_marker.flatten() {
101 Some(cursor) if !cursor.is_empty() => self.options.marker = Some(cursor),
102 _ => self.done = true,
103 }
104 }
105 }
106}
107
108pub struct HubDocumentManager {
110 session: std::sync::Arc<runtime::Client>,
111}
112
113impl HubDocumentManager {
114 pub(crate) fn new(session: std::sync::Arc<runtime::Client>) -> Self {
115 Self { session }
116 }
117
118 async fn list_pages_page(
119 &self,
120 hub_id: String,
121 opts: Option<HubDocumentListPagesOptions>,
122 ) -> Result<crate::models::schemas::HubDocumentPages, Error> {
123 let mut url = self.session.base_url("api");
124 url.push_str("/hub_document_pages");
125 let mut req = self.session.new_request("GET", &url);
126 req = runtime::with_query(req, "hub_id", &hub_id);
127 let opts = opts.unwrap_or_default();
128 if let Some(value) = opts.marker {
129 req = runtime::with_query(req, "marker", &value);
130 }
131 if let Some(value) = opts.limit {
132 req = runtime::with_query(req, "limit", &value.to_string());
133 }
134 req = runtime::with_header(req, "box-version", "2025.0");
135 let resp = self.session.fetch(req).await?;
136 let data = runtime::response_bytes(&resp)?;
137 Ok(serde_json::from_slice(&data)?)
138 }
139
140 pub fn list_pages(
142 &self,
143 hub_id: String,
144 opts: Option<HubDocumentListPagesOptions>,
145 ) -> HubDocumentListPagesPaginator {
146 HubDocumentListPagesPaginator {
147 manager: HubDocumentManager::new(self.session.clone()),
148 hub_id,
149 options: opts.unwrap_or_default(),
150 buffer: Vec::new().into_iter(),
151 done: false,
152 }
153 }
154
155 async fn list_blocks_page(
156 &self,
157 hub_id: String,
158 page_id: String,
159 opts: Option<HubDocumentListBlocksOptions>,
160 ) -> Result<crate::models::schemas::HubDocumentBlocks, Error> {
161 let mut url = self.session.base_url("api");
162 url.push_str("/hub_document_blocks");
163 let mut req = self.session.new_request("GET", &url);
164 req = runtime::with_query(req, "hub_id", &hub_id);
165 req = runtime::with_query(req, "page_id", &page_id);
166 let opts = opts.unwrap_or_default();
167 if let Some(value) = opts.marker {
168 req = runtime::with_query(req, "marker", &value);
169 }
170 if let Some(value) = opts.limit {
171 req = runtime::with_query(req, "limit", &value.to_string());
172 }
173 req = runtime::with_header(req, "box-version", "2025.0");
174 let resp = self.session.fetch(req).await?;
175 let data = runtime::response_bytes(&resp)?;
176 Ok(serde_json::from_slice(&data)?)
177 }
178
179 pub fn list_blocks(
181 &self,
182 hub_id: String,
183 page_id: String,
184 opts: Option<HubDocumentListBlocksOptions>,
185 ) -> HubDocumentListBlocksPaginator {
186 HubDocumentListBlocksPaginator {
187 manager: HubDocumentManager::new(self.session.clone()),
188 hub_id,
189 page_id,
190 options: opts.unwrap_or_default(),
191 buffer: Vec::new().into_iter(),
192 done: false,
193 }
194 }
195}