#![allow(
clippy::all,
clippy::unwrap_used,
clippy::arithmetic_side_effects,
clippy::indexing_slicing,
clippy::string_slice,
clippy::else_if_without_else,
reason = "Don't care about lints in examples."
)]
use akahu_client::{AkahuClient, UserToken};
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(short = 'u', long, env = "AKAHU_USER_TOKEN")]
user_token: String,
#[arg(short = 'a', long, env = "AKAHU_APP_TOKEN")]
app_token: String,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand, Debug)]
enum Commands {
All {
#[arg(short = 'v', long)]
verbose: bool,
},
Account {
#[arg(short = 'i', long)]
id: String,
#[arg(short = 'v', long)]
verbose: bool,
},
}
#[tokio::main]
async fn main() -> Result<()> {
dotenvy::dotenv().unwrap();
let args = Args::parse();
let client = AkahuClient::new(reqwest::Client::new(), args.app_token, None);
let user_token = UserToken::new(args.user_token);
match args.command {
Commands::All { verbose } => {
if verbose {
eprintln!("Initiating refresh for all accounts...");
}
client
.refresh_all_accounts(&user_token)
.await
.context("Failed to initiate refresh for all accounts")?;
println!("✓ Refresh initiated for all accounts");
if verbose {
eprintln!("\nNote: Account data is refreshed and enriched asynchronously.");
eprintln!("The refresh process may take a few moments to complete.");
}
}
Commands::Account { id, verbose } => {
if verbose {
eprintln!("Initiating refresh for {}...", id);
}
client
.refresh_account_or_connection(&user_token, &id)
.await
.with_context(|| format!("Failed to initiate refresh for {}", id))?;
println!("✓ Refresh initiated for {}", id);
if verbose {
if id.starts_with("acc_") {
eprintln!("\nNote: Refreshing an account will also refresh other accounts");
eprintln!(" sharing the same login credentials.");
} else if id.starts_with("conn_") {
eprintln!("\nNote: Refreshing a connection will refresh all accounts");
eprintln!(" held at that financial institution.");
}
eprintln!("\nAccount data is refreshed and enriched asynchronously.");
eprintln!("The refresh process may take a few moments to complete.");
}
}
}
Ok(())
}