ramp_api/
card_programs.rs

1use crate::Client;
2use crate::ClientResult;
3
4pub struct CardPrograms {
5    pub client: Client,
6}
7
8impl CardPrograms {
9    #[doc(hidden)]
10    pub fn new(client: Client) -> Self {
11        CardPrograms { client }
12    }
13
14    /**
15     * List card programs.
16     *
17     * This function performs a `GET` to the `/card-programs` endpoint.
18     *
19     * Retrieve all card programs.
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     */
27    pub async fn get_page(
28        &self,
29        start: &str,
30        page_size: f64,
31    ) -> ClientResult<crate::Response<Vec<crate::types::CardProgram>>> {
32        let mut query_args: Vec<(String, String)> = Default::default();
33        if !page_size.to_string().is_empty() {
34            query_args.push(("page_size".to_string(), page_size.to_string()));
35        }
36        if !start.is_empty() {
37            query_args.push(("start".to_string(), start.to_string()));
38        }
39        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
40        let url = self.client.url(&format!("/card-programs?{}", query_), None);
41        let resp: crate::Response<crate::types::GetCardProgramsResponse> = self
42            .client
43            .get(
44                &url,
45                crate::Message {
46                    body: None,
47                    content_type: None,
48                },
49            )
50            .await?;
51
52        // Return our response data.
53        Ok(crate::Response::new(
54            resp.status,
55            resp.headers,
56            resp.body.card_programs.to_vec(),
57        ))
58    }
59    /**
60     * List card programs.
61     *
62     * This function performs a `GET` to the `/card-programs` endpoint.
63     *
64     * As opposed to `get`, this function returns all the pages of the request at once.
65     *
66     * Retrieve all card programs.
67     */
68    pub async fn get_all(&self) -> ClientResult<crate::Response<Vec<crate::types::CardProgram>>> {
69        let url = self.client.url("/card-programs", None);
70        let crate::Response::<crate::types::GetCardProgramsResponse> {
71            mut status,
72            mut headers,
73            body,
74        } = self
75            .client
76            .get(
77                &url,
78                crate::Message {
79                    body: None,
80                    content_type: None,
81                },
82            )
83            .await?;
84
85        let mut card_programs = body.card_programs;
86        let mut page = body.page.next.to_string();
87
88        // Paginate if we should.
89        while !page.is_empty() {
90            match self
91                .client
92                .get::<crate::types::GetCardProgramsResponse>(
93                    page.trim_start_matches(&self.client.host),
94                    crate::Message {
95                        body: None,
96                        content_type: None,
97                    },
98                )
99                .await
100            {
101                Ok(mut resp) => {
102                    card_programs.append(&mut resp.body.card_programs);
103                    status = resp.status;
104                    headers = resp.headers;
105
106                    page = if body.page.next != page {
107                        body.page.next.to_string()
108                    } else {
109                        "".to_string()
110                    };
111                }
112                Err(e) => {
113                    if e.to_string().contains("404 Not Found") {
114                        page = "".to_string();
115                    } else {
116                        return Err(e);
117                    }
118                }
119            }
120        }
121
122        // Return our response data.
123        Ok(crate::Response::new(status, headers, card_programs))
124    }
125    /**
126     * Create a card program.
127     *
128     * This function performs a `POST` to the `/card-programs` endpoint.
129     *
130     *
131     *
132     * **Parameters:**
133     *
134     * * `authorization: &str` -- The OAuth2 token header.
135     */
136    pub async fn post_resources(
137        &self,
138        body: &crate::types::PostResourcesCardProgramRequest,
139    ) -> ClientResult<crate::Response<crate::types::CardProgram>> {
140        let url = self.client.url("/card-programs", None);
141        self.client
142            .post(
143                &url,
144                crate::Message {
145                    body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
146                    content_type: Some("application/json".to_string()),
147                },
148            )
149            .await
150    }
151    /**
152     * GET a card program.
153     *
154     * This function performs a `GET` to the `/card-programs/{id}` endpoint.
155     *
156     * Retrieve a single card program.
157     *
158     * **Parameters:**
159     *
160     * * `authorization: &str` -- The OAuth2 token header.
161     */
162    pub async fn get_program(
163        &self,
164        id: &str,
165    ) -> ClientResult<crate::Response<crate::types::CardProgram>> {
166        let url = self.client.url(
167            &format!(
168                "/card-programs/{}",
169                crate::progenitor_support::encode_path(id),
170            ),
171            None,
172        );
173        self.client
174            .get(
175                &url,
176                crate::Message {
177                    body: None,
178                    content_type: None,
179                },
180            )
181            .await
182    }
183}