use chrono::{DateTime, Utc};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct Account {
id: String,
closed: bool,
created: DateTime<Utc>,
description: String,
r#type: Type,
currency: String,
country_code: String,
owners: Vec<Owner>,
account_number: String,
sort_code: String,
}
impl Account {
#[must_use]
pub fn id(&self) -> &String {
&self.id
}
#[must_use]
pub fn closed(&self) -> bool {
self.closed
}
#[must_use]
pub fn created(&self) -> &DateTime<Utc> {
&self.created
}
#[must_use]
pub fn description(&self) -> &String {
&self.description
}
#[must_use]
pub fn currency(&self) -> &String {
&self.currency
}
#[must_use]
pub fn country_code(&self) -> &String {
&self.country_code
}
#[must_use]
pub fn owners(&self) -> &Vec<Owner> {
&self.owners
}
#[must_use]
pub fn account_number(&self) -> &String {
&self.account_number
}
#[must_use]
pub fn sort_code(&self) -> &String {
&self.sort_code
}
}
#[derive(Deserialize, Debug)]
pub struct Owner {
user_id: String,
preferred_name: String,
preferred_first_name: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum Type {
UkRetail,
UkRetailJoint,
}
pub use list::Request as List;
mod list {
use super::Account;
use crate::{endpoints::handle_response, Result};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub(super) struct Accounts {
accounts: Vec<Account>,
}
pub struct Request {
request_builder: reqwest::RequestBuilder,
}
impl Request {
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 }
}
pub async fn send(self) -> Result<Vec<Account>> {
handle_response(self.request_builder)
.await
.map(|accounts: Accounts| accounts.accounts)
}
}
}
#[cfg(test)]
mod tests {
use super::list::Accounts;
#[test]
fn deserialise_account() {
let raw_account_string = r#"
{
"accounts": [
{
"id": "acc_XXXX",
"closed": false,
"created": "2019-06-12T17:44:35.266Z",
"description": "user_XXXX",
"type": "uk_retail",
"currency": "GBP",
"country_code": "GB",
"owners": [
{
"user_id": "user_XXXX",
"preferred_name": "Daniel Eades",
"preferred_first_name": "Daniel"
}
],
"account_number": "12345678",
"sort_code": "040004",
"payment_details": {
"locale_uk": {
"account_number": "12345678",
"sort_code": "040004"
}
}
},
{
"id": "acc_XXXX",
"closed": false,
"created": "2019-08-01T13:46:10.041Z",
"description": "Joint account between user_XXXX and user_YYYY",
"type": "uk_retail_joint",
"currency": "GBP",
"country_code": "GB",
"owners": [
{
"user_id": "user_XXXX",
"preferred_name": "Daniel Eades",
"preferred_first_name": "Daniel"
},
{
"user_id": "user_YYYY",
"preferred_name": "Holly Johnstone",
"preferred_first_name": "Holly"
}
],
"account_number": "87654321",
"sort_code": "040004",
"payment_details": {
"locale_uk": {
"account_number": "87654321",
"sort_code": "040004"
}
}
}
]
}"#;
serde_json::from_str::<Accounts>(&raw_account_string).unwrap();
}
}