use std::process::ExitCode;
use fleetwire::api::users::{GetFeatureConfig, GetMe, GetRegion, ListOrders};
use fleetwire::api::vehicles::{
DataEndpoint, GetFleetTelemetryConfig, GetFleetTelemetryErrors, GetMobileEnabled,
GetNearbyChargingSites, GetRecentAlerts, GetReleaseNotes, GetServiceData, GetVehicle,
GetVehicleData, ListDrivers, ListInvitations, ListVehicles,
};
use fleetwire::{FleetGet, Pricing, Scope, ScopeExpr};
const PLACEHOLDER_VIN: &str = "00000000000000000";
fn main() -> ExitCode {
match run() {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("error: {e}");
ExitCode::FAILURE
}
}
}
fn run() -> Result<(), Box<dyn std::error::Error>> {
let vin = PLACEHOLDER_VIN;
let mut rows = vec![
row(&ListVehicles {
page: None,
per_page: None,
}),
row(&GetVehicle::new(vin)?),
row(&GetVehicleData::new(vin, vec![DataEndpoint::ChargeState])?),
row(&GetNearbyChargingSites::new(vin, None, None, None)?),
row(&ListInvitations::new(vin, None, None)?),
row(&GetMobileEnabled::new(vin)?),
row(&GetRecentAlerts::new(vin)?),
row(&GetReleaseNotes::new(vin)?),
row(&GetServiceData::new(vin)?),
row(&ListDrivers::new(vin)?),
row(&GetFleetTelemetryConfig::new(vin)?),
row(&GetFleetTelemetryErrors::new(vin)?),
row(&GetRegion),
row(&GetMe),
row(&ListOrders),
row(&GetFeatureConfig),
];
rows.sort_by_key(|r| !matches!(r.cost, Cost::Billed(_)));
let width = rows.iter().map(|r| r.path.len()).max().unwrap_or(0);
println!("{:<width$} {:<12} scopes", "GET", "cost");
for r in &rows {
let cost = match r.cost {
Cost::Billed(usd) => format!("${usd}/call"),
Cost::Free => "free".to_owned(),
Cost::Unknown => "unknown".to_owned(),
};
println!("{:<width$} {cost:<12} {}", r.path, r.scopes);
}
let billed = rows
.iter()
.filter(|r| matches!(r.cost, Cost::Billed(_)))
.count();
let free = rows.iter().filter(|r| matches!(r.cost, Cost::Free)).count();
println!();
println!(
"{billed} of {} endpoints are billed; {free} are free.",
rows.len()
);
println!(
"Use Client::send_guarded for the billed ones: it checks for free that the car is online first."
);
println!(
"These are the prices the request types carry, read without any network call; they are not a live tariff."
);
Ok(())
}
struct Row {
path: String,
cost: Cost,
scopes: String,
}
enum Cost {
Billed(f64),
Free,
Unknown,
}
fn row<R: FleetGet>(req: &R) -> Row {
let cost = match req.pricing() {
Pricing::Data { usd_per_request } => Cost::Billed(usd_per_request),
Pricing::NotCharged => Cost::Free,
_ => Cost::Unknown,
};
Row {
path: req.path().replace(PLACEHOLDER_VIN, "{vin}"),
cost,
scopes: scopes(&req.required_scopes()),
}
}
fn scopes(expr: &ScopeExpr) -> String {
let join = |v: &[ScopeExpr], sep: &str| -> String {
v.iter().map(scopes).collect::<Vec<_>>().join(sep)
};
match expr {
ScopeExpr::One(s) => scope_name(*s).to_owned(),
ScopeExpr::All(v) => join(v, " + "),
ScopeExpr::Any(v) => format!("any of ({})", join(v, ", ")),
_ => "?".to_owned(),
}
}
fn scope_name(scope: Scope) -> &'static str {
match scope {
Scope::VehicleDeviceData => "vehicle_device_data",
Scope::VehicleLocation => "vehicle_location",
Scope::UserData => "user_data",
Scope::Openid => "openid",
_ => "?",
}
}