A monetary acocunt can also be a savings account

So update the code to deal with that.

Signed-off-by: Jacob Kiers <code@kiers.eu>
This commit is contained in:
Jacob Kiers 2024-02-29 19:28:13 +01:00
parent 096ccec741
commit 95437f3549

View File

@ -20,6 +20,7 @@ struct Installation<'a> {
struct Token { struct Token {
token: String, token: String,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "PascalCase")] #[serde(rename_all = "PascalCase")]
struct InstallationResponse { struct InstallationResponse {
@ -106,16 +107,19 @@ struct AppState {
token: String, token: String,
pem_private: String, pem_private: String,
} }
#[derive(Serialize, Deserialize, Default)] #[derive(Serialize, Deserialize, Default)]
pub struct BunqConfig { pub struct BunqConfig {
api_key: String, api_key: String,
state: Option<AppState>, state: Option<AppState>,
} }
pub struct BunqConfigReady { pub struct BunqConfigReady {
token: String, token: String,
keypair: RsaPrivateKey, keypair: RsaPrivateKey,
user_id: i64, user_id: i64,
} }
impl BunqConfig { impl BunqConfig {
pub fn load() -> Result<BunqConfig> { pub fn load() -> Result<BunqConfig> {
Ok(confy::load("bunq-rs")?) Ok(confy::load("bunq-rs")?)
@ -144,7 +148,7 @@ impl BunqConfig {
format!("{}/v1/installation", BASE), format!("{}/v1/installation", BASE),
serde_json::to_string(&body)?, serde_json::to_string(&body)?,
)? )?
.text()?; .text()?;
let response: InstallationResponse = deserialize_retarded_response(&response)?.response; let response: InstallationResponse = deserialize_retarded_response(&response)?.response;
let token = response.token.token; let token = response.token.token;
@ -192,29 +196,26 @@ impl BunqConfig {
} }
impl BunqConfigReady { impl BunqConfigReady {
pub fn monetary_accounts(&self) -> Result<Vec<MonetaryAccountBank>> { pub fn monetary_accounts(&self) -> Result<Vec<MonetaryAccount>> {
let response = isahc::http::Request::get(format!( let response = isahc::http::Request::get(format!(
"{}/v1/user/{}/monetary-account", "{}/v1/user/{}/monetary-account",
BASE, self.user_id BASE, self.user_id
)) ))
.header("X-Bunq-Client-Authentication", &self.token) .header("X-Bunq-Client-Authentication", &self.token)
.body(())? .body(())?
.send()? .send()?
.text()?; .text()?;
Ok(
deserialize_normal_response::<Vec<MonetaryAccount>>(&response)? let accounts = deserialize_normal_response::<Vec<MonetaryAccount>>(&response)?;
.response
.into_iter() Ok(accounts.response)
.map(|m| m.monetary_account_bank)
.collect(),
)
} }
pub fn payments(&self, acc: &MonetaryAccountBank) -> Result<Vec<Payment>> { pub fn payments(&self, acc: &MonetaryAccount) -> Result<Vec<Payment>> {
self.payments_from_to(acc, None, None) self.payments_from_to(acc, None, None)
} }
pub fn payments_from_to( pub fn payments_from_to(
&self, &self,
acc: &MonetaryAccountBank, acc: &MonetaryAccount,
from: Option<i64>, from: Option<i64>,
to: Option<i64>, to: Option<i64>,
) -> Result<Vec<Payment>> { ) -> Result<Vec<Payment>> {
@ -233,22 +234,30 @@ impl BunqConfigReady {
pagination, pagination,
)) ))
}; };
let account_id = match acc {
MonetaryAccount::MonetaryAccountBank(bank) => bank.id,
MonetaryAccount::MonetaryAccountSavings(savings) => savings.id,
};
let mut url = format!( let mut url = format!(
"/v1/user/{}/monetary-account/{}/payment", "/v1/user/{}/monetary-account/{}/payment",
self.user_id, acc.id self.user_id, account_id
); );
if let Some(to) = to { if let Some(to) = to {
url = format!("{}?newer_id={}", url, to); url = format!("{}?newer_id={}", url, to);
} }
let mut all = Vec::new(); let mut all = Vec::new();
loop { loop {
let (mut payments, pag) = next_page(&format!("{}{}", BASE, url))?; let (mut payments, pag) = next_page(&format!("{}{}", BASE, url))?;
all.append(&mut payments); all.append(&mut payments);
dbg!(&pag); dbg!(&pag);
if let Some(Pagination { if let Some(Pagination {
older_url: Some(older_url), older_url: Some(older_url),
.. ..
}) = pag }) = pag
{ {
if let (Some(latest), Some(from)) = (all.last(), from) { if let (Some(latest), Some(from)) = (all.last(), from) {
if latest.id <= from { if latest.id <= from {
@ -309,11 +318,19 @@ pub struct Payment {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")] #[serde(rename_all = "PascalCase")]
struct MonetaryAccount { pub enum MonetaryAccount {
monetary_account_bank: MonetaryAccountBank, MonetaryAccountBank(MonetaryAccountBank),
MonetaryAccountSavings(MonetaryAccountSavings),
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct MonetaryAccountBank { pub struct MonetaryAccountBank {
pub id: i64, pub id: i64,
pub description: String, pub description: String,
} }
#[derive(Deserialize, Debug)]
pub struct MonetaryAccountSavings {
pub id: i64,
pub description: String,
}