aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-08-09 05:01:41 -0400
committerJesse Luehrs <doy@tozt.net>2019-08-09 05:01:41 -0400
commit53f34a6d47c43d96e5a3f5b368387776e9db7b02 (patch)
tree7c3073d1db4eeca0bfc7ada5d78001dd738335e4 /src
parent50c1255b06b9053ce26afbccbb5ad26831423f73 (diff)
downloadynab-api-53f34a6d47c43d96e5a3f5b368387776e9db7b02.tar.gz
ynab-api-53f34a6d47c43d96e5a3f5b368387776e9db7b02.zip
start sketching out some stuff using the api
Diffstat (limited to 'src')
-rw-r--r--src/main.rs75
1 files changed, 74 insertions, 1 deletions
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))
+ }
}