Skip to main content

ramp_api/
transactions.rs

1use crate::Client;
2use crate::ClientResult;
3
4pub struct Transactions {
5    pub client: Client,
6}
7
8impl Transactions {
9    #[doc(hidden)]
10    pub fn new(client: Client) -> Self {
11        Transactions { client }
12    }
13
14    /**
15     * List transactions.
16     *
17     * This function performs a `GET` to the `/transactions` endpoint.
18     *
19     * Retrieves all transactions for the business. This endpoint supports filtering and ordering. NOTE: only one ordering param is supported.
20     *
21     * **Parameters:**
22     *
23     * * `authorization: &str` -- The OAuth2 token header.
24     * * `department_id: &str` -- The OAuth2 token header.
25     * * `location_id: &str` -- The OAuth2 token header.
26     * * `from_date: chrono::DateTime<chrono::Utc>`
27     * * `to_date: chrono::DateTime<chrono::Utc>`
28     * * `merchant_id: &str` -- The OAuth2 token header.
29     * * `sk_category_id: &str` -- The OAuth2 token header.
30     * * `order_by_date_desc: bool`
31     * * `order_by_date_asc: bool`
32     * * `order_by_amount_desc: bool`
33     * * `order_by_amount_asc: bool`
34     * * `state: &str` -- The OAuth2 token header.
35     * * `min_amount: 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.
36     * * `max_amount: 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.
37     * * `start: &str` -- The ID of the last entity of the previous page, used for pagination to get the next page.
38     * * `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.
39     * * `requires_memo: bool` -- Filters for transactions which require a memo, but do not have one. This can only be set to true.
40     */
41    pub async fn get_page(
42        &self,
43        department_id: &str,
44        location_id: &str,
45        from_date: Option<chrono::DateTime<chrono::Utc>>,
46        to_date: Option<chrono::DateTime<chrono::Utc>>,
47        merchant_id: &str,
48        sk_category_id: &str,
49        order_by_date_desc: bool,
50        order_by_date_asc: bool,
51        order_by_amount_desc: bool,
52        order_by_amount_asc: bool,
53        state: &str,
54        min_amount: f64,
55        max_amount: f64,
56        start: &str,
57        page_size: f64,
58        requires_memo: bool,
59    ) -> ClientResult<crate::Response<Vec<crate::types::Data>>> {
60        let mut query_args: Vec<(String, String)> = Default::default();
61        if !department_id.is_empty() {
62            query_args.push(("department_id".to_string(), department_id.to_string()));
63        }
64        if let Some(date) = from_date {
65            query_args.push(("from_date".to_string(), date.to_rfc3339()));
66        }
67        if !location_id.is_empty() {
68            query_args.push(("location_id".to_string(), location_id.to_string()));
69        }
70        if !max_amount.to_string().is_empty() {
71            query_args.push(("max_amount".to_string(), max_amount.to_string()));
72        }
73        if !merchant_id.is_empty() {
74            query_args.push(("merchant_id".to_string(), merchant_id.to_string()));
75        }
76        if !min_amount.to_string().is_empty() {
77            query_args.push(("min_amount".to_string(), min_amount.to_string()));
78        }
79        if order_by_amount_asc {
80            query_args.push((
81                "order_by_amount_asc".to_string(),
82                order_by_amount_asc.to_string(),
83            ));
84        }
85        if order_by_amount_desc {
86            query_args.push((
87                "order_by_amount_desc".to_string(),
88                order_by_amount_desc.to_string(),
89            ));
90        }
91        if order_by_date_asc {
92            query_args.push((
93                "order_by_date_asc".to_string(),
94                order_by_date_asc.to_string(),
95            ));
96        }
97        if order_by_date_desc {
98            query_args.push((
99                "order_by_date_desc".to_string(),
100                order_by_date_desc.to_string(),
101            ));
102        }
103        if !page_size.to_string().is_empty() {
104            query_args.push(("page_size".to_string(), page_size.to_string()));
105        }
106        if requires_memo {
107            query_args.push(("requires_memo".to_string(), requires_memo.to_string()));
108        }
109        if !sk_category_id.is_empty() {
110            query_args.push(("sk_category_id".to_string(), sk_category_id.to_string()));
111        }
112        if !start.is_empty() {
113            query_args.push(("start".to_string(), start.to_string()));
114        }
115        if !state.is_empty() {
116            query_args.push(("state".to_string(), state.to_string()));
117        }
118        if let Some(date) = to_date {
119            query_args.push(("to_date".to_string(), date.to_rfc3339()));
120        }
121        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
122        let url = self.client.url(&format!("/transactions?{}", query_), None);
123        let resp: crate::Response<crate::types::GetTransactionResponse> = self
124            .client
125            .get(
126                &url,
127                crate::Message {
128                    body: None,
129                    content_type: None,
130                },
131            )
132            .await?;
133
134        // Return our response data.
135        Ok(crate::Response::new(
136            resp.status,
137            resp.headers,
138            resp.body.data.to_vec(),
139        ))
140    }
141    /**
142     * List transactions.
143     *
144     * This function performs a `GET` to the `/transactions` endpoint.
145     *
146     * As opposed to `get`, this function returns all the pages of the request at once.
147     *
148     * Retrieves all transactions for the business. This endpoint supports filtering and ordering. NOTE: only one ordering param is supported.
149     */
150    pub async fn get_all(
151        &self,
152        department_id: &str,
153        location_id: &str,
154        from_date: Option<chrono::DateTime<chrono::Utc>>,
155        to_date: Option<chrono::DateTime<chrono::Utc>>,
156        merchant_id: &str,
157        sk_category_id: &str,
158        order_by_date_desc: bool,
159        order_by_date_asc: bool,
160        order_by_amount_desc: bool,
161        order_by_amount_asc: bool,
162        state: &str,
163        min_amount: f64,
164        max_amount: f64,
165        requires_memo: bool,
166    ) -> ClientResult<crate::Response<Vec<crate::types::Data>>> {
167        let mut query_args: Vec<(String, String)> = Default::default();
168        if !department_id.is_empty() {
169            query_args.push(("department_id".to_string(), department_id.to_string()));
170        }
171        if let Some(date) = from_date {
172            query_args.push(("from_date".to_string(), date.to_rfc3339()));
173        }
174        if !location_id.is_empty() {
175            query_args.push(("location_id".to_string(), location_id.to_string()));
176        }
177        if !max_amount.to_string().is_empty() {
178            query_args.push(("max_amount".to_string(), max_amount.to_string()));
179        }
180        if !merchant_id.is_empty() {
181            query_args.push(("merchant_id".to_string(), merchant_id.to_string()));
182        }
183        if !min_amount.to_string().is_empty() {
184            query_args.push(("min_amount".to_string(), min_amount.to_string()));
185        }
186        if order_by_amount_asc {
187            query_args.push((
188                "order_by_amount_asc".to_string(),
189                order_by_amount_asc.to_string(),
190            ));
191        }
192        if order_by_amount_desc {
193            query_args.push((
194                "order_by_amount_desc".to_string(),
195                order_by_amount_desc.to_string(),
196            ));
197        }
198        if order_by_date_asc {
199            query_args.push((
200                "order_by_date_asc".to_string(),
201                order_by_date_asc.to_string(),
202            ));
203        }
204        if order_by_date_desc {
205            query_args.push((
206                "order_by_date_desc".to_string(),
207                order_by_date_desc.to_string(),
208            ));
209        }
210        if requires_memo {
211            query_args.push(("requires_memo".to_string(), requires_memo.to_string()));
212        }
213        if !sk_category_id.is_empty() {
214            query_args.push(("sk_category_id".to_string(), sk_category_id.to_string()));
215        }
216        if !state.is_empty() {
217            query_args.push(("state".to_string(), state.to_string()));
218        }
219        if let Some(date) = to_date {
220            query_args.push(("to_date".to_string(), date.to_rfc3339()));
221        }
222        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
223        let url = self.client.url(&format!("/transactions?{}", query_), None);
224        let crate::Response::<crate::types::GetTransactionResponse> {
225            mut status,
226            mut headers,
227            body,
228        } = self
229            .client
230            .get(
231                &url,
232                crate::Message {
233                    body: None,
234                    content_type: None,
235                },
236            )
237            .await?;
238
239        let mut data = body.data;
240        let mut page = body.page.next.to_string();
241
242        // Paginate if we should.
243        while !page.is_empty() {
244            match self
245                .client
246                .get::<crate::types::GetTransactionResponse>(
247                    page.trim_start_matches(&self.client.host),
248                    crate::Message {
249                        body: None,
250                        content_type: None,
251                    },
252                )
253                .await
254            {
255                Ok(mut resp) => {
256                    data.append(&mut resp.body.data);
257                    status = resp.status;
258                    headers = resp.headers;
259
260                    page = if body.page.next != page {
261                        body.page.next.to_string()
262                    } else {
263                        "".to_string()
264                    };
265                }
266                Err(e) => {
267                    if e.to_string().contains("404 Not Found") {
268                        page = "".to_string();
269                    } else {
270                        return Err(e);
271                    }
272                }
273            }
274        }
275
276        // Return our response data.
277        Ok(crate::Response::new(status, headers, data))
278    }
279    /**
280     * GET a transaction.
281     *
282     * This function performs a `GET` to the `/transactions/{id}` endpoint.
283     *
284     *
285     *
286     * **Parameters:**
287     *
288     * * `authorization: &str` -- The OAuth2 token header.
289     */
290    pub async fn get_resource(
291        &self,
292        id: &str,
293    ) -> ClientResult<crate::Response<crate::types::Data>> {
294        let url = self.client.url(
295            &format!(
296                "/transactions/{}",
297                crate::progenitor_support::encode_path(id),
298            ),
299            None,
300        );
301        self.client
302            .get(
303                &url,
304                crate::Message {
305                    body: None,
306                    content_type: None,
307                },
308            )
309            .await
310    }
311}