1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! Accounts API endpoint

use crate::{endpoints::handle_response, Result};
use chrono::{DateTime, Utc};
use serde::Deserialize;

/// A struct representing a collection of accounts
#[derive(Deserialize, Debug)]
struct Accounts {
    accounts: Vec<Account>,
}

/// A struct representing a Monzo Account
#[derive(Deserialize, Debug)]
pub struct Account {
    /// the unique ID of the accounts
    pub id: String,

    closed: bool,

    /// the date-time that the account was created
    pub created: DateTime<Utc>,

    /// account description
    pub description: String,

    r#type: String,

    currency: String,

    country_code: String,

    owners: Vec<Owner>,

    account_number: String,

    sort_code: String,
}

#[derive(Deserialize, Debug)]
struct Owner {
    user_id: String,
    preferred_name: String,
    preferred_first_name: String,
}

/// An object representing a request to the Monzo API for a list of accounts
pub struct ListAccounts {
    request_builder: reqwest::RequestBuilder,
}

impl ListAccounts {
    pub(crate) fn new(http_client: &reqwest::Client, access_token: impl AsRef<str>) -> Self {
        let request_builder = http_client
            .get("https://api.monzo.com/accounts")
            .bearer_auth(access_token.as_ref());

        Self { request_builder }
    }

    /// Consume the request and return a future that will resolve to a list of
    /// accounts
    pub async fn send(self) -> Result<Vec<Account>> {
        handle_response(self.request_builder)
            .await
            .map(|accounts: Accounts| accounts.accounts)
    }
}

/*
use std::future::Future;
use crate::into_future::IntoFuture;

impl IntoFuture for ListAccounts {
    type Output = Result<Vec<Account>>;
    type Future = impl Future<Output = Self::Output>;

    fn into_future(self) -> Self::Future {
        self.send()
    }
} */