monzo/endpoints/transactions/
get.rs

1use super::Transaction;
2use crate::{client, endpoints::Endpoint, Result};
3
4/// A request to retrieve a list of transactions from the Monzo API
5///
6/// Use the builder-style methods to set optional fields on the request
7#[derive(Debug)]
8#[must_use]
9pub struct Request<'a, C>
10where
11    C: client::Inner,
12{
13    client: &'a C,
14    endpoint: String,
15    expand_merchant: bool,
16}
17
18impl<C> Endpoint for Request<'_, C>
19where
20    C: client::Inner,
21{
22    const METHOD: reqwest::Method = reqwest::Method::GET;
23
24    fn endpoint(&self) -> &str {
25        &self.endpoint
26    }
27
28    fn query(&self) -> Option<&dyn erased_serde::Serialize> {
29        if self.expand_merchant {
30            Some(&("expand[]", "merchant"))
31        } else {
32            None
33        }
34    }
35}
36
37impl<'a, C> Request<'a, C>
38where
39    C: client::Inner,
40{
41    pub(crate) fn new(client: &'a C, transaction_id: &'a str) -> Self {
42        let endpoint = format!("/transactions/{transaction_id}");
43        Self {
44            client,
45            endpoint,
46            expand_merchant: false,
47        }
48    }
49
50    /// Optionally expand the merchant field from an id string into a struct
51    /// container merchant details
52    pub const fn expand_merchant(mut self) -> Self {
53        self.expand_merchant = true;
54        self
55    }
56
57    /// Consume the request and return the [`Transaction`]
58    pub async fn send(self) -> Result<Transaction> {
59        self.client.handle_request(&self).await
60    }
61}