use webull_rs::{WebullClient, WebullError};
use std::time::{Duration, Instant};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = WebullClient::builder()
.with_api_key("your-api-key")
.with_api_secret("your-api-secret")
.with_timeout(Duration::from_secs(30))
.build()?;
println!("Logging in...");
match client.login("username", "password").await {
Ok(_) => {
println!("Logged in successfully");
}
Err(WebullError::InvalidRequest(msg)) => {
println!("API not yet implemented: {}", msg);
}
Err(e) => {
return Err(e.into());
}
}
println!("\nGetting account information (first request)...");
let start = Instant::now();
match client.accounts().get_accounts().await {
Ok(accounts) => {
let elapsed = start.elapsed();
println!("Found {} accounts in {:?}", accounts.len(), elapsed);
}
Err(WebullError::InvalidRequest(msg)) => {
println!("API not yet implemented: {}", msg);
}
Err(e) => {
println!("Error: {}", e);
}
}
println!("\nGetting account information (second request, should be cached)...");
let start = Instant::now();
match client.accounts().get_accounts().await {
Ok(accounts) => {
let elapsed = start.elapsed();
println!("Found {} accounts in {:?} (should be faster)", accounts.len(), elapsed);
}
Err(WebullError::InvalidRequest(msg)) => {
println!("API not yet implemented: {}", msg);
}
Err(e) => {
println!("Error: {}", e);
}
}
println!("\nGetting quote for AAPL (first request)...");
let start = Instant::now();
match client.market_data().get_quote("AAPL").await {
Ok(quote) => {
let elapsed = start.elapsed();
println!("Got quote for AAPL in {:?}", elapsed);
println!(" Last price: ${}", quote.last_price);
}
Err(WebullError::InvalidRequest(msg)) => {
println!("API not yet implemented: {}", msg);
}
Err(e) => {
println!("Error: {}", e);
}
}
println!("\nGetting quote for AAPL (second request, should be cached)...");
let start = Instant::now();
match client.market_data().get_quote("AAPL").await {
Ok(quote) => {
let elapsed = start.elapsed();
println!("Got quote for AAPL in {:?} (should be faster)", elapsed);
println!(" Last price: ${}", quote.last_price);
}
Err(WebullError::InvalidRequest(msg)) => {
println!("API not yet implemented: {}", msg);
}
Err(e) => {
println!("Error: {}", e);
}
}
println!("\nLogging out...");
match client.logout().await {
Ok(_) => {
println!("Logged out successfully");
}
Err(WebullError::InvalidRequest(msg)) => {
println!("API not yet implemented: {}", msg);
}
Err(e) => {
return Err(e.into());
}
}
Ok(())
}