1use crate::Client;
2use crate::ClientResult;
3
4pub struct Cards {
5 pub client: Client,
6}
7
8impl Cards {
9 #[doc(hidden)]
10 pub fn new(client: Client) -> Self {
11 Cards { client }
12 }
13
14 pub async fn get_page(
30 &self,
31 start: &str,
32 page_size: f64,
33 user_id: &str,
34 card_program_id: &str,
35 ) -> ClientResult<crate::Response<Vec<crate::types::Card>>> {
36 let mut query_args: Vec<(String, String)> = Default::default();
37 if !card_program_id.is_empty() {
38 query_args.push(("card_program_id".to_string(), card_program_id.to_string()));
39 }
40 if !page_size.to_string().is_empty() {
41 query_args.push(("page_size".to_string(), page_size.to_string()));
42 }
43 if !start.is_empty() {
44 query_args.push(("start".to_string(), start.to_string()));
45 }
46 if !user_id.is_empty() {
47 query_args.push(("user_id".to_string(), user_id.to_string()));
48 }
49 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
50 let url = self.client.url(&format!("/cards?{}", query_), None);
51 let resp: crate::Response<crate::types::GetCardsResponse> = self
52 .client
53 .get(
54 &url,
55 crate::Message {
56 body: None,
57 content_type: None,
58 },
59 )
60 .await?;
61
62 Ok(crate::Response::new(
64 resp.status,
65 resp.headers,
66 resp.body.cards.to_vec(),
67 ))
68 }
69 pub async fn get_all(
79 &self,
80 user_id: &str,
81 card_program_id: &str,
82 ) -> ClientResult<crate::Response<Vec<crate::types::Card>>> {
83 let mut query_args: Vec<(String, String)> = Default::default();
84 if !card_program_id.is_empty() {
85 query_args.push(("card_program_id".to_string(), card_program_id.to_string()));
86 }
87 if !user_id.is_empty() {
88 query_args.push(("user_id".to_string(), user_id.to_string()));
89 }
90 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
91 let url = self.client.url(&format!("/cards?{}", query_), None);
92 let crate::Response::<crate::types::GetCardsResponse> {
93 mut status,
94 mut headers,
95 body,
96 } = self
97 .client
98 .get(
99 &url,
100 crate::Message {
101 body: None,
102 content_type: None,
103 },
104 )
105 .await?;
106
107 let mut cards = body.cards;
108 let mut page = body.page.next.to_string();
109
110 while !page.is_empty() {
112 match self
113 .client
114 .get::<crate::types::GetCardsResponse>(
115 page.trim_start_matches(&self.client.host),
116 crate::Message {
117 body: None,
118 content_type: None,
119 },
120 )
121 .await
122 {
123 Ok(mut resp) => {
124 cards.append(&mut resp.body.cards);
125 status = resp.status;
126 headers = resp.headers;
127
128 page = if body.page.next != page {
129 body.page.next.to_string()
130 } else {
131 "".to_string()
132 };
133 }
134 Err(e) => {
135 if e.to_string().contains("404 Not Found") {
136 page = "".to_string();
137 } else {
138 return Err(e);
139 }
140 }
141 }
142 }
143
144 Ok(crate::Response::new(status, headers, cards))
146 }
147 pub async fn get(&self, id: &str) -> ClientResult<crate::Response<crate::types::Card>> {
159 let url = self.client.url(
160 &format!("/cards/{}", crate::progenitor_support::encode_path(id),),
161 None,
162 );
163 self.client
164 .get(
165 &url,
166 crate::Message {
167 body: None,
168 content_type: None,
169 },
170 )
171 .await
172 }
173 pub async fn patch_resources(
185 &self,
186 id: &str,
187 body: &crate::types::PatchResourcesCardsCardRequest,
188 ) -> ClientResult<crate::Response<()>> {
189 let url = self.client.url(
190 &format!("/cards/{}", crate::progenitor_support::encode_path(id),),
191 None,
192 );
193 self.client
194 .patch(
195 &url,
196 crate::Message {
197 body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
198 content_type: Some("application/json".to_string()),
199 },
200 )
201 .await
202 }
203 pub async fn post_resources_physical(
215 &self,
216 body: &crate::types::PostResourcesCardPhysicalRequest,
217 ) -> ClientResult<crate::Response<crate::types::TaskResponse>> {
218 let url = self.client.url("/cards/deferred/physical", None);
219 self.client
220 .post(
221 &url,
222 crate::Message {
223 body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
224 content_type: Some("application/json".to_string()),
225 },
226 )
227 .await
228 }
229 pub async fn post_resources_virtual(
241 &self,
242 body: &crate::types::PostResourcesCardVirtualRequest,
243 ) -> ClientResult<crate::Response<crate::types::TaskResponse>> {
244 let url = self.client.url("/cards/deferred/virtual", None);
245 self.client
246 .post(
247 &url,
248 crate::Message {
249 body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
250 content_type: Some("application/json".to_string()),
251 },
252 )
253 .await
254 }
255 pub async fn post_resources_termination(
263 &self,
264 id: &str,
265 body: &crate::types::PostResourcesCardsCardSuspensionRequest,
266 ) -> ClientResult<crate::Response<crate::types::TaskResponse>> {
267 let url = self.client.url(
268 &format!(
269 "/cards/{}/deferred/termination",
270 crate::progenitor_support::encode_path(id),
271 ),
272 None,
273 );
274 self.client
275 .post(
276 &url,
277 crate::Message {
278 body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
279 content_type: Some("application/json".to_string()),
280 },
281 )
282 .await
283 }
284 pub async fn post_resources_suspension(
292 &self,
293 id: &str,
294 body: &crate::types::PostResourcesCardsCardSuspensionRequest,
295 ) -> ClientResult<crate::Response<crate::types::TaskResponse>> {
296 let url = self.client.url(
297 &format!(
298 "/cards/{}/deferred/suspension",
299 crate::progenitor_support::encode_path(id),
300 ),
301 None,
302 );
303 self.client
304 .post(
305 &url,
306 crate::Message {
307 body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
308 content_type: Some("application/json".to_string()),
309 },
310 )
311 .await
312 }
313 pub async fn post_resources_unsuspension(
321 &self,
322 id: &str,
323 body: &crate::types::PostResourcesCardsCardSuspensionRequest,
324 ) -> ClientResult<crate::Response<crate::types::TaskResponse>> {
325 let url = self.client.url(
326 &format!(
327 "/cards/{}/deferred/unsuspension",
328 crate::progenitor_support::encode_path(id),
329 ),
330 None,
331 );
332 self.client
333 .post(
334 &url,
335 crate::Message {
336 body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
337 content_type: Some("application/json".to_string()),
338 },
339 )
340 .await
341 }
342 pub async fn get_resources_deferred(
354 &self,
355 id: &str,
356 ) -> ClientResult<crate::Response<crate::types::GetResourcesCardsDeferredResponse>> {
357 let url = self.client.url(
358 &format!(
359 "/cards/deferred/status/{}",
360 crate::progenitor_support::encode_path(id),
361 ),
362 None,
363 );
364 self.client
365 .get(
366 &url,
367 crate::Message {
368 body: None,
369 content_type: None,
370 },
371 )
372 .await
373 }
374}