Formatting

Signed-off-by: Jacob Kiers <code@kiers.eu>
This commit is contained in:
Jacob Kiers 2024-02-29 22:07:37 +01:00
parent 6255dc1c2f
commit 3de7ca94a2
4 changed files with 290 additions and 278 deletions

View File

@ -1,12 +1,12 @@
use anyhow::anyhow;
use isahc::{ReadResponseExt, RequestExt};
use isahc::http::StatusCode;
use isahc::{ReadResponseExt, RequestExt};
use rsa::pkcs1::{DecodeRsaPrivateKey, EncodeRsaPrivateKey, LineEnding};
use rsa::pkcs8::EncodePublicKey;
use rsa::RsaPrivateKey;
use serde::{Deserialize, Serialize};
use crate::{BASE_URL, BunqClient, deserialize_retarded_response, sign};
use crate::{deserialize_retarded_response, sign, BunqClient, BASE_URL};
#[derive(Serialize, Deserialize, Default)]
struct AppState {
@ -22,11 +22,17 @@ pub struct BunqConfig {
impl BunqConfig {
pub fn load() -> anyhow::Result<BunqConfig> {
println!("Loading config file from {}", confy::get_configuration_file_path("bunq-rs", "bunq-rs")?.to_string_lossy());
println!(
"Loading config file from {}",
confy::get_configuration_file_path("bunq-rs", "bunq-rs")?.to_string_lossy()
);
Ok(confy::load("bunq-rs", "bunq-rs")?)
}
pub fn save(&self) -> anyhow::Result<()> {
println!("Storing config file in {}", confy::get_configuration_file_path("bunq-rs", None)?.to_string_lossy());
println!(
"Storing config file in {}",
confy::get_configuration_file_path("bunq-rs", None)?.to_string_lossy()
);
confy::store("bunq-rs", "bunq-rs", self)?;
Ok(())
}
@ -41,7 +47,9 @@ impl BunqConfig {
let bits = 2048;
let keypair = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
let pem_public = keypair.to_public_key().to_public_key_pem(LineEnding::CRLF)?;
let pem_public = keypair
.to_public_key()
.to_public_key_pem(LineEnding::CRLF)?;
let body = Installation {
client_public_key: &pem_public,
@ -50,7 +58,7 @@ impl BunqConfig {
format!("{}/v1/installation", BASE_URL),
serde_json::to_string(&body)?,
)?
.text()?;
.text()?;
let response: InstallationResponse = deserialize_retarded_response(&response)?.response;
let token = response.token.token;
@ -72,7 +80,10 @@ impl BunqConfig {
return Err(anyhow!(response_text));
}
self.state = Some(AppState { pem_private: keypair.to_pkcs1_pem(LineEnding::CRLF)?.to_string(), token });
self.state = Some(AppState {
pem_private: keypair.to_pkcs1_pem(LineEnding::CRLF)?.to_string(),
token,
});
self.save()?;
keypair

View File

@ -1,8 +1,8 @@
use anyhow::Result;
use rsa::pkcs1v15::SigningKey;
use rsa::RsaPrivateKey;
use rsa::sha2::Sha256;
use rsa::signature::RandomizedSigner;
use rsa::RsaPrivateKey;
use serde::{de::DeserializeOwned, Deserialize};
pub use config::BunqConfig;
@ -94,7 +94,7 @@ fn deserialize_normal_response<T: DeserializeOwned>(r: &str) -> Result<Response<
}
fn sign(body: &str, key: &RsaPrivateKey) -> Result<String> {
use base64::prelude::{BASE64_STANDARD, Engine};
use base64::prelude::{Engine, BASE64_STANDARD};
let signing_key = SigningKey::<Sha256>::new(key.clone());
let mut rng = rand::thread_rng();

View File

@ -1,17 +1,17 @@
use isahc::{ReadResponseExt, RequestExt};
use serde::Deserialize;
use crate::{BASE_URL, BunqClient};
use crate::{BunqClient, BASE_URL};
pub(super) fn get(client: &BunqClient) -> anyhow::Result<Vec<MonetaryAccount>> {
let response = isahc::http::Request::get(format!(
"{}/v1/user/{}/monetary-account",
BASE_URL, client.user_id
))
.header("X-Bunq-Client-Authentication", &client.token)
.body(())?
.send()?
.text()?;
.header("X-Bunq-Client-Authentication", &client.token)
.body(())?
.send()?
.text()?;
let accounts = crate::deserialize_normal_response::<Vec<MonetaryAccount>>(&response)?;

View File

@ -2,13 +2,14 @@ use anyhow::Result;
use isahc::{ReadResponseExt, RequestExt};
use serde::Deserialize;
use crate::{BASE_URL, BunqClient, MonetaryAccount, Pagination, Response};
use crate::{BunqClient, MonetaryAccount, Pagination, Response, BASE_URL};
pub(super) fn get_from_to(client: &BunqClient,
acc: &MonetaryAccount,
from: Option<i64>,
to: Option<i64>) -> Result<Vec<Payment>>
{
pub(super) fn get_from_to(
client: &BunqClient,
acc: &MonetaryAccount,
from: Option<i64>,
to: Option<i64>,
) -> Result<Vec<Payment>> {
let next_page = |url: &str| -> Result<(_, _)> {
let response = isahc::http::Request::get(url)
.header("X-Bunq-Client-Authentication", &client.token)
@ -46,9 +47,9 @@ pub(super) fn get_from_to(client: &BunqClient,
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 {