ramp_api/
cards.rs

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    /**
15     * List cards.
16     *
17     * This function performs a `GET` to the `/cards` endpoint.
18     *
19     * Retrieve all cards.
20     *
21     * **Parameters:**
22     *
23     * * `authorization: &str` -- The OAuth2 token header.
24     * * `start: &str` -- The ID of the last entity of the previous page, used for pagination to get the next page.
25     * * `page_size: f64` -- The number of results to be returned in each page. The value must be between 2 and 10,000. If not specified, the default will be 1,000.
26     * * `user_id: &str` -- The OAuth2 token header.
27     * * `card_program_id: &str` -- The OAuth2 token header.
28     */
29    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        // Return our response data.
63        Ok(crate::Response::new(
64            resp.status,
65            resp.headers,
66            resp.body.cards.to_vec(),
67        ))
68    }
69    /**
70     * List cards.
71     *
72     * This function performs a `GET` to the `/cards` endpoint.
73     *
74     * As opposed to `get`, this function returns all the pages of the request at once.
75     *
76     * Retrieve all cards.
77     */
78    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        // Paginate if we should.
111        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        // Return our response data.
145        Ok(crate::Response::new(status, headers, cards))
146    }
147    /**
148     * GET a card.
149     *
150     * This function performs a `GET` to the `/cards/{id}` endpoint.
151     *
152     * Retrieve a single card.
153     *
154     * **Parameters:**
155     *
156     * * `authorization: &str` -- The OAuth2 token header.
157     */
158    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    /**
174     * Update card.
175     *
176     * This function performs a `PATCH` to the `/cards/{id}` endpoint.
177     *
178     * Update card details
179     *
180     * **Parameters:**
181     *
182     * * `authorization: &str` -- The OAuth2 token header.
183     */
184    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    /**
204     * Create a physical card.
205     *
206     * This function performs a `POST` to the `/cards/deferred/physical` endpoint.
207     *
208     *
209     *
210     * **Parameters:**
211     *
212     * * `authorization: &str` -- The OAuth2 token header.
213     */
214    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    /**
230     * Create a virtual card.
231     *
232     * This function performs a `POST` to the `/cards/deferred/virtual` endpoint.
233     *
234     *
235     *
236     * **Parameters:**
237     *
238     * * `authorization: &str` -- The OAuth2 token header.
239     */
240    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    /**
256     * Delete a card.
257     *
258     * This function performs a `POST` to the `/cards/{id}/deferred/termination` endpoint.
259     *
260     * Terminates a card permanently.
261     */
262    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    /**
285     * Suspend a card.
286     *
287     * This function performs a `POST` to the `/cards/{id}/deferred/suspension` endpoint.
288     *
289     * Suspends a card so that it is locked from use. The suspension is revertable.
290     */
291    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    /**
314     * Removes a card's suspension.
315     *
316     * This function performs a `POST` to the `/cards/{id}/deferred/unsuspension` endpoint.
317     *
318     * Removes a card's suspension so that it may be used again.
319     */
320    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    /**
343     * Get status of a deferred card task.
344     *
345     * This function performs a `GET` to the `/cards/deferred/status/{id}` endpoint.
346     *
347     * Gets status of a deferred task for cards
348     *
349     * **Parameters:**
350     *
351     * * `authorization: &str` -- The OAuth2 token header.
352     */
353    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}