use anyhow::Result; use isahc::{RequestExt, ResponseExt}; use serde::Deserialize; use crate::{BASE_URL, BunqClient, MonetaryAccount, Pagination, Response}; pub(super) fn get_from_to(client: &BunqClient, acc: &MonetaryAccount, from: Option, to: Option) -> Result> { let next_page = |url: &str| -> Result<(_, _)> { let response = isahc::http::Request::get(url) .header("X-Bunq-Client-Authentication", &client.token) .body(())? .send()? .text()?; let Response { response, pagination, } = crate::deserialize_normal_response::>(&response)?; Ok(( response.into_iter().map(|p| p.payment).collect(), 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", client.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, url))?; dbg!(&payments); all.append(&mut payments); dbg!(&pag); if let Some(Pagination { older_url: Some(older_url), .. }) = pag { if let (Some(latest), Some(from)) = (all.last(), from) { if latest.id <= from { all.retain(|p| p.id >= from); break; } } url = older_url; } else { break; } } Ok(all) } #[derive(Deserialize, Debug)] pub struct LabelMonetaryAccount { pub iban: Option, pub display_name: String, pub merchant_category_code: Option, } #[derive(Deserialize, Debug)] pub struct Amount { pub value: String, pub currency: String, } #[derive(Deserialize, Debug)] #[serde(rename_all = "PascalCase")] struct PaymentPayment { payment: Payment, } #[derive(Deserialize, Debug)] pub struct Payment { pub alias: LabelMonetaryAccount, pub counterparty_alias: LabelMonetaryAccount, pub amount: Amount, pub balance_after_mutation: Amount, pub created: String, pub updated: String, pub description: String, pub id: i64, pub monetary_account_id: i64, #[serde(rename = "type")] pub type_: String, pub sub_type: String, }