1use crate::Client;
2use crate::ClientResult;
3
4pub struct Receipts {
5 pub client: Client,
6}
7
8impl Receipts {
9 #[doc(hidden)]
10 pub fn new(client: Client) -> Self {
11 Receipts { client }
12 }
13
14 pub async fn get_page(
31 &self,
32 from_date: Option<chrono::DateTime<chrono::Utc>>,
33 to_date: Option<chrono::DateTime<chrono::Utc>>,
34 created_after: Option<chrono::DateTime<chrono::Utc>>,
35 created_before: Option<chrono::DateTime<chrono::Utc>>,
36 start: &str,
37 page_size: f64,
38 ) -> ClientResult<crate::Response<Vec<crate::types::Receipt>>> {
39 let mut query_args: Vec<(String, String)> = Default::default();
40 if let Some(date) = created_after {
41 query_args.push(("created_after".to_string(), date.to_rfc3339()));
42 }
43 if let Some(date) = created_before {
44 query_args.push(("created_before".to_string(), date.to_rfc3339()));
45 }
46 if let Some(date) = from_date {
47 query_args.push(("from_date".to_string(), date.to_rfc3339()));
48 }
49 if !page_size.to_string().is_empty() {
50 query_args.push(("page_size".to_string(), page_size.to_string()));
51 }
52 if !start.is_empty() {
53 query_args.push(("start".to_string(), start.to_string()));
54 }
55 if let Some(date) = to_date {
56 query_args.push(("to_date".to_string(), date.to_rfc3339()));
57 }
58 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
59 let url = self.client.url(&format!("/receipts?{}", query_), None);
60 let resp: crate::Response<crate::types::GetReceiptsResponse> = self
61 .client
62 .get(
63 &url,
64 crate::Message {
65 body: None,
66 content_type: None,
67 },
68 )
69 .await?;
70
71 Ok(crate::Response::new(
73 resp.status,
74 resp.headers,
75 resp.body.data.to_vec(),
76 ))
77 }
78 pub async fn get_all(
88 &self,
89 from_date: Option<chrono::DateTime<chrono::Utc>>,
90 to_date: Option<chrono::DateTime<chrono::Utc>>,
91 created_after: Option<chrono::DateTime<chrono::Utc>>,
92 created_before: Option<chrono::DateTime<chrono::Utc>>,
93 ) -> ClientResult<crate::Response<Vec<crate::types::Receipt>>> {
94 let mut query_args: Vec<(String, String)> = Default::default();
95 if let Some(date) = created_after {
96 query_args.push(("created_after".to_string(), date.to_rfc3339()));
97 }
98 if let Some(date) = created_before {
99 query_args.push(("created_before".to_string(), date.to_rfc3339()));
100 }
101 if let Some(date) = from_date {
102 query_args.push(("from_date".to_string(), date.to_rfc3339()));
103 }
104 if let Some(date) = to_date {
105 query_args.push(("to_date".to_string(), date.to_rfc3339()));
106 }
107 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
108 let url = self.client.url(&format!("/receipts?{}", query_), None);
109 let crate::Response::<crate::types::GetReceiptsResponse> {
110 mut status,
111 mut headers,
112 body,
113 } = self
114 .client
115 .get(
116 &url,
117 crate::Message {
118 body: None,
119 content_type: None,
120 },
121 )
122 .await?;
123
124 let mut data = body.data;
125 let mut page = body.page.next.to_string();
126
127 while !page.is_empty() {
129 match self
130 .client
131 .get::<crate::types::GetReceiptsResponse>(
132 page.trim_start_matches(&self.client.host),
133 crate::Message {
134 body: None,
135 content_type: None,
136 },
137 )
138 .await
139 {
140 Ok(mut resp) => {
141 data.append(&mut resp.body.data);
142 status = resp.status;
143 headers = resp.headers;
144
145 page = if body.page.next != page {
146 body.page.next.to_string()
147 } else {
148 "".to_string()
149 };
150 }
151 Err(e) => {
152 if e.to_string().contains("404 Not Found") {
153 page = "".to_string();
154 } else {
155 return Err(e);
156 }
157 }
158 }
159 }
160
161 Ok(crate::Response::new(status, headers, data))
163 }
164 pub async fn get(&self, id: &str) -> ClientResult<crate::Response<crate::types::Receipt>> {
172 let url = self.client.url(
173 &format!("/receipts/{}", crate::progenitor_support::encode_path(id),),
174 None,
175 );
176 self.client
177 .get(
178 &url,
179 crate::Message {
180 body: None,
181 content_type: None,
182 },
183 )
184 .await
185 }
186}