aboutsummaryrefslogtreecommitdiffstats
path: root/src/ynab/client.rs
blob: 4b935c3c6eebff882cb99447bb23d34670b51e98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
pub struct Client {
    api: ynab_api::apis::client::APIClient,
}

impl Client {
    pub fn new(key: &str) -> Self {
        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(),
        });
        Self {
            api: ynab_api::apis::client::APIClient::new(ynab_config),
        }
    }

    pub fn default_budget(&self) -> ynab_api::models::BudgetDetail {
        let budgets =
            self.api.budgets_api().get_budgets().unwrap().data.budgets;
        let budget = budgets.iter().next().unwrap();
        self.api
            .budgets_api()
            .get_budget_by_id(&budget.id, 0)
            .unwrap()
            .data
            .budget
    }

    pub fn update_transactions(
        &self,
        budget_id: &str,
        transactions: ynab_api::models::UpdateTransactionsWrapper,
    ) -> Option<String> {
        let res = self
            .api
            .transactions_api()
            .update_transactions(budget_id, transactions);
        if let Err(e) = res {
            Some(format!("{:?}", e))
        } else {
            None
        }
    }
}