up_bank_api/
accounts.rs

1use serde::{Deserialize, Serialize};
2use restson::{RestPath};
3
4use crate::general::{Pagination, MoneyObject, TransactionsLinks};
5
6#[derive(Serialize, Deserialize, Debug)]
7pub struct AccountAttributes {
8    #[serde(alias = "displayName")]
9    pub display_name:   String,
10    #[serde(alias = "accountType")]
11    pub account_type:   String,
12    pub balance:        MoneyObject,
13    #[serde(alias = "createdAt")]
14    pub created_at:     String
15}
16
17
18#[derive(Serialize, Deserialize, Debug)]
19pub struct AccountRelationships {
20    pub transactions:   TransactionsLinks
21}
22
23#[derive(Serialize, Debug)]
24pub struct AccountId {
25    id:             String
26}
27
28impl AccountId {
29    pub fn new(id: &str) -> AccountId {
30        AccountId {
31            id:     id.to_string()
32        }
33    }
34
35    pub fn id(&self) -> &str {
36        &self.id
37    }
38}
39
40impl<'de> Deserialize<'de> for AccountId {
41    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
42    where
43        D: serde::Deserializer<'de> {
44        let s: String = Deserialize::deserialize(deserializer)?;
45        Ok(AccountId {
46            id:     s
47        })
48    }
49}
50
51#[derive(Serialize, Deserialize, Debug)]
52pub struct AccountData {
53    pub id:             AccountId,
54    pub attributes:     AccountAttributes,
55    pub relationships:  AccountRelationships
56}
57
58/// Can be used to get the Accounts list, with the use of a RestClient
59/// 
60/// Example:
61/// ```
62/// let mut client = get_new_blocking_client(String::from("Bearer up:demo:rtHR7D3eBEqKPiIT"))
63///     .unwrap();
64/// let accounts: AccountsListResponse = client.get(()).unwrap();
65/// ```
66#[derive(Serialize, Deserialize, Debug)]
67pub struct AccountsListResponse {
68    pub data:   Vec<AccountData>,
69    pub links:  Pagination<AccountsListResponse>
70}
71
72#[derive(Serialize, Deserialize, Debug)]
73pub struct AccountResponse {
74    pub data:   AccountData
75}
76
77impl RestPath<()> for AccountsListResponse {
78    fn get_path(_: ()) -> Result<String, restson::Error> {
79        Ok(String::from("accounts"))
80    }
81}
82
83impl RestPath<AccountId> for AccountResponse {
84    fn get_path(id: AccountId) -> Result<String, restson::Error> {
85        Ok(String::from("accounts/") + &id.id)
86    }
87}
88
89// impl RestPath<AccountId> for AccountsListResponse {
90//     fn get_path(query: AccountId) -> Result<String, restson::Error> {
91//         Ok(String::from("accounts")+"?"+)
92//     }
93// }
94