teslatte/examples/basic.rs
gak 8c059769ee
refactor!: Rename Api to OwnerApi. Remove ResponseData<T>. Add VehicleApi trait.
Significant refactor to progress towards different API access that Tesla
has introduced. See issues #6 and #7.

Removed `ResponseData` because it wasn't very ergonomic, forcing the user
to deref or call data(). Also it had specific fields for JSON output
which was only used for the CLI, so I introduced a field
`print_responses` in OwnerApi that the CLI can use.
2023-10-22 09:17:32 +11:00

63 lines
1.7 KiB
Rust

use std::env;
use teslatte::auth::AccessToken;
use teslatte::products::Product;
use teslatte::{OwnerApi, VehicleApi};
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let api = match env::var("TESLA_ACCESS_TOKEN") {
Ok(t) => OwnerApi::new(AccessToken(t), None),
Err(_) => {
let api = OwnerApi::from_interactive_url().await.unwrap();
println!("TOKEN: {:?}", api.access_token);
api
}
};
let vehicles = api.vehicles().await.unwrap();
dbg!(&*vehicles);
if !vehicles.is_empty() {
let vehicle_data = api.vehicle_data(&vehicles[0].id).await.unwrap();
dbg!(&vehicle_data);
} else {
println!("No vehicles found!");
}
let products = api.products().await.unwrap();
dbg!(&*products);
if !products.is_empty() {
for product in &*products {
match product {
Product::Vehicle(v) => {
dbg!(v);
}
Product::Solar(e) => {
let site_info = api.energy_sites_site_info(&e.energy_site_id).await.unwrap();
dbg!(&site_info);
let live_info = api
.energy_sites_live_status(&e.energy_site_id)
.await
.unwrap();
dbg!(&live_info);
}
Product::Powerwall(p) => {
let live_info = api
.energy_sites_live_status(&p.energy_site_id)
.await
.unwrap();
dbg!(&live_info);
}
}
}
} else {
println!("No products found!");
}
}