ramp_api/
card_programs.rs1use 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 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 Ok(crate::Response::new(
54 resp.status,
55 resp.headers,
56 resp.body.card_programs.to_vec(),
57 ))
58 }
59 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 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 Ok(crate::Response::new(status, headers, card_programs))
124 }
125 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 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}