From 53f34a6d47c43d96e5a3f5b368387776e9db7b02 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 9 Aug 2019 05:01:41 -0400 Subject: start sketching out some stuff using the api --- src/main.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index e7a11a9..c3fa324 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,76 @@ +fn client(key: &str) -> ynab_api::apis::client::APIClient { + let mut ynab_config = ynab_api::apis::configuration::Configuration::new(); + ynab_config.api_key = Some(ynab_api::apis::configuration::ApiKey { + prefix: Some("Bearer".to_string()), + key: key.to_string(), + }); + ynab_api::apis::client::APIClient::new(ynab_config) +} + +fn format_amount(amount: i64) -> String { + let dollars = amount.abs() / 1000; + let cents = (amount.abs() % 1000) / 10; + let sign = if amount < 0 { "-" } else { "" }; + format!("{}{}.{:02}", sign, dollars, cents) +} + fn main() { - println!("Hello, world!"); + let key = std::env::args().nth(1).unwrap(); + let client = client(&key); + + let budgets = client.budgets_api().get_budgets().unwrap().data.budgets; + let budget = budgets.iter().next().unwrap(); + println!("budget is {} ({})", budget.name, budget.id); + + let reimbursables_id = client + .categories_api() + .get_categories(&budget.id, 0) + .unwrap() + .data + .category_groups + .iter() + .map(|group| { + group + .categories + .iter() + .map(|c| (c.id.clone(), c.name.clone())) + }) + .flat_map(|cs| cs) + .find(|(_, name)| name == "Reimbursables") + .map(|(id, _)| id) + .unwrap(); + println!("found reimbursables category: {}", reimbursables_id); + + let full_budget = client + .budgets_api() + .get_budget_by_id(&budget.id, 0) + .unwrap() + .data + .budget; + println!("got full budget"); + + let transactions = full_budget.transactions.unwrap(); + let payees = full_budget.payees.unwrap(); + + for t in transactions { + if let Some(category_id) = t.category_id { + if category_id != reimbursables_id { + continue; + } + } else { + continue; + } + + if t.flag_color.is_some() { + continue; + } + + let payee = t + .payee_id + .map(|id| { + payees.iter().find(|p| p.id == id).unwrap().name.clone() + }) + .unwrap_or_else(|| "(none)".to_string()); + println!("{} | {} | {}", t.date, payee, format_amount(t.amount)) + } } -- cgit v1.2.3-54-g00ecf