From 95437f3549ae5e735ac00b1026b53dd2ea543c41 Mon Sep 17 00:00:00 2001 From: Jacob Kiers Date: Thu, 29 Feb 2024 19:28:13 +0100 Subject: [PATCH] A monetary acocunt can also be a savings account So update the code to deal with that. Signed-off-by: Jacob Kiers --- src/lib.rs | 59 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a72d2bd..b678a28 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ struct Installation<'a> { struct Token { token: String, } + #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] struct InstallationResponse { @@ -106,16 +107,19 @@ struct AppState { token: String, pem_private: String, } + #[derive(Serialize, Deserialize, Default)] pub struct BunqConfig { api_key: String, state: Option, } + pub struct BunqConfigReady { token: String, keypair: RsaPrivateKey, user_id: i64, } + impl BunqConfig { pub fn load() -> Result { Ok(confy::load("bunq-rs")?) @@ -144,7 +148,7 @@ impl BunqConfig { format!("{}/v1/installation", BASE), serde_json::to_string(&body)?, )? - .text()?; + .text()?; let response: InstallationResponse = deserialize_retarded_response(&response)?.response; let token = response.token.token; @@ -192,29 +196,26 @@ impl BunqConfig { } impl BunqConfigReady { - pub fn monetary_accounts(&self) -> Result> { + pub fn monetary_accounts(&self) -> Result> { let response = isahc::http::Request::get(format!( "{}/v1/user/{}/monetary-account", BASE, self.user_id )) - .header("X-Bunq-Client-Authentication", &self.token) - .body(())? - .send()? - .text()?; - Ok( - deserialize_normal_response::>(&response)? - .response - .into_iter() - .map(|m| m.monetary_account_bank) - .collect(), - ) + .header("X-Bunq-Client-Authentication", &self.token) + .body(())? + .send()? + .text()?; + + let accounts = deserialize_normal_response::>(&response)?; + + Ok(accounts.response) } - pub fn payments(&self, acc: &MonetaryAccountBank) -> Result> { + pub fn payments(&self, acc: &MonetaryAccount) -> Result> { self.payments_from_to(acc, None, None) } pub fn payments_from_to( &self, - acc: &MonetaryAccountBank, + acc: &MonetaryAccount, from: Option, to: Option, ) -> Result> { @@ -233,22 +234,30 @@ impl BunqConfigReady { pagination, )) }; + + let account_id = match acc { + MonetaryAccount::MonetaryAccountBank(bank) => bank.id, + MonetaryAccount::MonetaryAccountSavings(savings) => savings.id, + }; + let mut url = format!( "/v1/user/{}/monetary-account/{}/payment", - self.user_id, acc.id + self.user_id, account_id ); + if let Some(to) = to { url = format!("{}?newer_id={}", url, to); } + let mut all = Vec::new(); loop { let (mut payments, pag) = next_page(&format!("{}{}", BASE, url))?; all.append(&mut payments); dbg!(&pag); if let Some(Pagination { - older_url: Some(older_url), - .. - }) = pag + older_url: Some(older_url), + .. + }) = pag { if let (Some(latest), Some(from)) = (all.last(), from) { if latest.id <= from { @@ -309,11 +318,19 @@ pub struct Payment { #[derive(Deserialize, Debug)] #[serde(rename_all = "PascalCase")] -struct MonetaryAccount { - monetary_account_bank: MonetaryAccountBank, +pub enum MonetaryAccount { + MonetaryAccountBank(MonetaryAccountBank), + MonetaryAccountSavings(MonetaryAccountSavings), } + #[derive(Deserialize, Debug)] pub struct MonetaryAccountBank { pub id: i64, pub description: String, } + +#[derive(Deserialize, Debug)] +pub struct MonetaryAccountSavings { + pub id: i64, + pub description: String, +}