Skip to main content

ramp_api/
receipts.rs

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    /**
15     * List receipts.
16     *
17     * This function performs a `GET` to the `/receipts` endpoint.
18     *
19     * Returns description of all receipts of a business.
20     *
21     * **Parameters:**
22     *
23     * * `from_date: chrono::DateTime<chrono::Utc>` -- Filter for receipts related to transactions which occurred after the specified date.
24     * * `to_date: chrono::DateTime<chrono::Utc>` -- Filter for receipts related to transactions which occurred before the specified date.
25     * * `created_after: chrono::DateTime<chrono::Utc>` -- Filter for receipts that were created after the specified date.
26     * * `created_before: chrono::DateTime<chrono::Utc>` -- Filter for receipts that were created before the specified date.
27     * * `start: &str` -- The ID of the last entity of the previous page, used for pagination to get the next page.
28     * * `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.
29     */
30    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        // Return our response data.
72        Ok(crate::Response::new(
73            resp.status,
74            resp.headers,
75            resp.body.data.to_vec(),
76        ))
77    }
78    /**
79     * List receipts.
80     *
81     * This function performs a `GET` to the `/receipts` endpoint.
82     *
83     * As opposed to `get`, this function returns all the pages of the request at once.
84     *
85     * Returns description of all receipts of a business.
86     */
87    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        // Paginate if we should.
128        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        // Return our response data.
162        Ok(crate::Response::new(status, headers, data))
163    }
164    /**
165     * Get details for one receipt.
166     *
167     * This function performs a `GET` to the `/receipts/{id}` endpoint.
168     *
169     *
170     */
171    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}