Skip to content

Commit

Permalink
implement account balance
Browse files Browse the repository at this point in the history
  • Loading branch information
richardjlyon committed Jun 6, 2023
1 parent 325d687 commit 8a1ab1b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 24 deletions.
33 changes: 27 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ anyhow = "1.0.71"
sea-orm-migration = "0.11.3"
migration = {path = "migration"}
colored = "2.0.0"
format_num = "0.1.0"
# futures = "0.3.28"
4 changes: 2 additions & 2 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ async fn main() -> Result<()> {
let account_command = sub_matches.subcommand().unwrap();
match account_command {
("list", _) => {
commands::account::list().await;
commands::account::list().await?;
}
("balance", _) => {
println!("Accoount balances")
commands::account::balance().await?;
}

(name, _) => {
Expand Down
17 changes: 13 additions & 4 deletions src/commands/account.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
//! Command Line Interface `Accounts` commands
//!
use crate::db;
use crate::{db, starling::client::StarlingApiClient};
use anyhow::Result;

pub async fn list() -> Result<()> {
println!("Account list");

println!("Account list:");
for account in db::account::list().await? {
println!("{:#?}", account.name);
println!("- {:#?}", account.name);
}

Ok(())
}

pub async fn balance() -> Result<()> {
println!("Account balances:");
for account in db::account::list().await? {
let client = StarlingApiClient::new(&account.token);
let balance = client.balance(&account.uid).await?;
println!("- {}: {}", account.name, balance.effective.as_string());
}
Ok(())
}
37 changes: 25 additions & 12 deletions src/starling/account.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Structures and methods for processing `/api/v2/accounts/` endpoints
use super::client::StarlingApiClient;
use anyhow::Result;
use chrono::{DateTime, Utc};
use format_num::NumberFormat;
use serde::Deserialize;

use super::client::StarlingApiClient;

/// Represents a list of Starling accounts
#[derive(Deserialize, Debug)]
pub struct Accounts {
Expand All @@ -26,30 +27,42 @@ pub struct Account {
#[derive(Deserialize, Debug)]
pub struct Balance {
#[serde(rename = "clearedBalance")]
cleared: SignedCurrencyAndAmount,
pub cleared: SignedCurrencyAndAmount,
#[serde(rename = "effectiveBalance")]
effective: SignedCurrencyAndAmount,
pub effective: SignedCurrencyAndAmount,
#[serde(rename = "pendingTransactions")]
pending: SignedCurrencyAndAmount,
pub pending: SignedCurrencyAndAmount,
#[serde(rename = "totalClearedBalance")]
total_cleared: SignedCurrencyAndAmount,
pub total_cleared: SignedCurrencyAndAmount,
#[serde(rename = "acceptedOverdraft")]
overdraft: SignedCurrencyAndAmount,
pub overdraft: SignedCurrencyAndAmount,
#[serde(rename = "totalEffectiveBalance")]
total_effective: SignedCurrencyAndAmount,
pub total_effective: SignedCurrencyAndAmount,
}

#[derive(Deserialize, Debug)]
struct SignedCurrencyAndAmount {
pub struct SignedCurrencyAndAmount {
currency: String,
#[serde(rename = "minurUnits")]
#[serde(rename = "minorUnits")]
minor_units: i64,
}

impl SignedCurrencyAndAmount {
pub fn as_float(&self) -> f32 {
self.minor_units as f32 / 100.0
}

pub fn as_string(&self) -> String {
let num = NumberFormat::new();
let amount = num.format(" >10,.2f", self.as_float());
format!("{} {}", self.currency, amount)
}
}

// Implement `/api/v2/accounts/{accountUid}/balance`
//
impl StarlingApiClient {
async fn balance(&self, account_uid: &String) -> Balance {
pub async fn balance(&self, account_uid: &String) -> Result<Balance> {
let mut resp = surf::get(format!(
"{}/accounts/{}/balance",
&self.base_url, &account_uid
Expand All @@ -59,6 +72,6 @@ impl StarlingApiClient {
.await
.unwrap();

resp.body_json::<Balance>().await.unwrap()
Ok(resp.body_json::<Balance>().await.unwrap())
}
}

0 comments on commit 8a1ab1b

Please sign in to comment.