box_open_sdk/managers/
docgen.rs1use crate::internal::path_escape;
4use crate::runtime::{self, Error};
5
6#[derive(Clone, Debug, Default)]
8pub struct DocgenListJobsOptions {
9 pub marker: Option<String>,
10 pub limit: Option<i64>,
11}
12
13#[derive(Clone, Debug, Default)]
15pub struct DocgenListBatchJobOptions {
16 pub marker: Option<String>,
17 pub limit: Option<i64>,
18}
19
20pub struct DocgenListJobsPaginator {
23 manager: DocgenManager,
24 options: DocgenListJobsOptions,
25 buffer: std::vec::IntoIter<crate::models::schemas::DocGenJobFull>,
26 done: bool,
27}
28
29impl DocgenListJobsPaginator {
30 pub async fn next(&mut self) -> Option<Result<crate::models::schemas::DocGenJobFull, 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_jobs_page(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.unwrap_or_default().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 DocgenListBatchJobPaginator {
63 manager: DocgenManager,
64 batch_id: String,
65 options: DocgenListBatchJobOptions,
66 buffer: std::vec::IntoIter<crate::models::schemas::DocGenJob>,
67 done: bool,
68}
69
70impl DocgenListBatchJobPaginator {
71 pub async fn next(&mut self) -> Option<Result<crate::models::schemas::DocGenJob, Error>> {
74 loop {
75 if let Some(item) = self.buffer.next() {
76 return Some(Ok(item));
77 }
78 if self.done {
79 return None;
80 }
81 let page = match self
82 .manager
83 .list_batch_job_page(self.batch_id.clone(), Some(self.options.clone()))
84 .await
85 {
86 Ok(page) => page,
87 Err(err) => {
88 self.done = true;
89 return Some(Err(err));
90 }
91 };
92 self.buffer = page.entries.unwrap_or_default().into_iter();
93 match page.next_marker.flatten() {
94 Some(cursor) if !cursor.is_empty() => self.options.marker = Some(cursor),
95 _ => self.done = true,
96 }
97 }
98 }
99}
100
101pub struct DocgenManager {
103 session: std::sync::Arc<runtime::Client>,
104}
105
106impl DocgenManager {
107 pub(crate) fn new(session: std::sync::Arc<runtime::Client>) -> Self {
108 Self { session }
109 }
110
111 pub async fn get_job(
112 &self,
113 job_id: String,
114 ) -> Result<crate::models::schemas::DocGenJob, Error> {
115 let mut url = self.session.base_url("api");
116 url.push_str("/docgen_jobs");
117 url.push('/');
118 let seg = path_escape(&job_id);
119 url.push_str(&seg);
120 let mut req = self.session.new_request("GET", &url);
121 req = runtime::with_header(req, "box-version", "2025.0");
122 let resp = self.session.fetch(req).await?;
123 let data = runtime::response_bytes(&resp)?;
124 Ok(serde_json::from_slice(&data)?)
125 }
126
127 async fn list_jobs_page(
128 &self,
129 opts: Option<DocgenListJobsOptions>,
130 ) -> Result<crate::models::schemas::DocGenJobsFull, Error> {
131 let mut url = self.session.base_url("api");
132 url.push_str("/docgen_jobs");
133 let mut req = self.session.new_request("GET", &url);
134 let opts = opts.unwrap_or_default();
135 if let Some(value) = opts.marker {
136 req = runtime::with_query(req, "marker", &value);
137 }
138 if let Some(value) = opts.limit {
139 req = runtime::with_query(req, "limit", &value.to_string());
140 }
141 req = runtime::with_header(req, "box-version", "2025.0");
142 let resp = self.session.fetch(req).await?;
143 let data = runtime::response_bytes(&resp)?;
144 Ok(serde_json::from_slice(&data)?)
145 }
146
147 pub fn list_jobs(&self, opts: Option<DocgenListJobsOptions>) -> DocgenListJobsPaginator {
149 DocgenListJobsPaginator {
150 manager: DocgenManager::new(self.session.clone()),
151 options: opts.unwrap_or_default(),
152 buffer: Vec::new().into_iter(),
153 done: false,
154 }
155 }
156
157 async fn list_batch_job_page(
158 &self,
159 batch_id: String,
160 opts: Option<DocgenListBatchJobOptions>,
161 ) -> Result<crate::models::schemas::DocGenJobs, Error> {
162 let mut url = self.session.base_url("api");
163 url.push_str("/docgen_batch_jobs");
164 url.push('/');
165 let seg = path_escape(&batch_id);
166 url.push_str(&seg);
167 let mut req = self.session.new_request("GET", &url);
168 let opts = opts.unwrap_or_default();
169 if let Some(value) = opts.marker {
170 req = runtime::with_query(req, "marker", &value);
171 }
172 if let Some(value) = opts.limit {
173 req = runtime::with_query(req, "limit", &value.to_string());
174 }
175 req = runtime::with_header(req, "box-version", "2025.0");
176 let resp = self.session.fetch(req).await?;
177 let data = runtime::response_bytes(&resp)?;
178 Ok(serde_json::from_slice(&data)?)
179 }
180
181 pub fn list_batch_job(
183 &self,
184 batch_id: String,
185 opts: Option<DocgenListBatchJobOptions>,
186 ) -> DocgenListBatchJobPaginator {
187 DocgenListBatchJobPaginator {
188 manager: DocgenManager::new(self.session.clone()),
189 batch_id,
190 options: opts.unwrap_or_default(),
191 buffer: Vec::new().into_iter(),
192 done: false,
193 }
194 }
195
196 pub async fn create_batches(
197 &self,
198 body: crate::models::schemas::DocGenBatchCreateRequest,
199 ) -> Result<crate::models::schemas::DocGenBatchBase, Error> {
200 let mut url = self.session.base_url("api");
201 url.push_str("/docgen_batches");
202 let mut req = self.session.new_request("POST", &url);
203 req = runtime::with_header(req, "box-version", "2025.0");
204 let payload = serde_json::to_vec(&body)?;
205 req = runtime::with_json_body(req, &payload);
206 let resp = self.session.fetch(req).await?;
207 let data = runtime::response_bytes(&resp)?;
208 Ok(serde_json::from_slice(&data)?)
209 }
210}