Fix API interaction and add transactions command
This commit is contained in:
+29
-2
@@ -80,6 +80,12 @@ impl Ynab {
|
||||
.send()
|
||||
.map_err(|e| format!("post error: {}", e))?;
|
||||
if res.status().is_success() {
|
||||
println!(
|
||||
"successful api response {}: {}",
|
||||
res.status(),
|
||||
res.text()
|
||||
.map_err(|e| format!("error retrieving api error: {}", e))?
|
||||
);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!(
|
||||
@@ -114,6 +120,28 @@ impl Ynab {
|
||||
Ok(res.data.accounts.into_iter().map(|p| p.name).collect())
|
||||
}
|
||||
|
||||
pub fn list_transactions(
|
||||
&self,
|
||||
plan: Lookup,
|
||||
account: Option<Lookup>,
|
||||
) -> Result<Vec<Transaction>, String> {
|
||||
let plan_id = self.resolve_plan(plan)?;
|
||||
let account_id = match account {
|
||||
Some(account) => Some(self.resolve_account(&plan_id, account)?),
|
||||
None => None,
|
||||
};
|
||||
let since_date = chrono::Local::now().date_naive() - chrono::Duration::days(30);
|
||||
let since_date = since_date.format("%Y-%m-%d");
|
||||
let url = format!("plans/{}/transactions?since_date={}", plan_id, since_date);
|
||||
let raw = self.get(&url).map_err(|e| format!("request error {}", e))?;
|
||||
let res: types::YnabResponse<types::YnabTransactionList> =
|
||||
serde_json::from_str(&raw).map_err(|e| format!("parse error: {}", e))?;
|
||||
Ok(transform::from_ynab_transactions(
|
||||
&res.data.transactions,
|
||||
account_id.as_deref(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn upload(
|
||||
&self,
|
||||
transactions: &[Transaction],
|
||||
@@ -124,10 +152,9 @@ impl Ynab {
|
||||
let account_id = self.resolve_account(&plan_id, account)?;
|
||||
let request = format!("plans/{}/transactions", plan_id);
|
||||
let body = serde_json::to_string(&types::YnabTransactionList {
|
||||
transactions: transform::ynab_transactions(transactions, &account_id),
|
||||
transactions: transform::to_ynab_transactions(transactions, &account_id),
|
||||
})
|
||||
.map_err(|e| format!("transaction format error: {}", e))?;
|
||||
println!("{}", body);
|
||||
self.post(&request, &body)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user