wip(tesla_api_coverage): rename keys based on similar urls
This commit is contained in:
parent
9d4d7804a2
commit
4f24db9bdb
|
@ -1,4 +1,6 @@
|
||||||
|
use crate::Restful;
|
||||||
use heck::ToKebabCase;
|
use heck::ToKebabCase;
|
||||||
|
use reqwest::Method;
|
||||||
use scraper::{Element, ElementRef, Html, Selector};
|
use scraper::{Element, ElementRef, Html, Selector};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
@ -69,7 +71,7 @@ pub struct Parameter {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct FleetEndpoint {
|
pub struct FleetEndpoint {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub method: reqwest::Method,
|
pub method: Method,
|
||||||
pub uri: String,
|
pub uri: String,
|
||||||
// description: String,
|
// description: String,
|
||||||
// category: Category,
|
// category: Category,
|
||||||
|
@ -79,6 +81,16 @@ pub struct FleetEndpoint {
|
||||||
// response_example: String,
|
// response_example: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Restful for FleetEndpoint {
|
||||||
|
fn method(&self) -> &Method {
|
||||||
|
&self.method
|
||||||
|
}
|
||||||
|
|
||||||
|
fn uri(&self) -> &str {
|
||||||
|
&self.uri
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse(html: &str) -> HashMap<String, FleetEndpoint> {
|
pub fn parse(html: &str) -> HashMap<String, FleetEndpoint> {
|
||||||
let document = Html::parse_document(html);
|
let document = Html::parse_document(html);
|
||||||
let content_selector = selector(".content h1");
|
let content_selector = selector(".content h1");
|
||||||
|
|
|
@ -10,6 +10,7 @@ use crate::teslatte::TeslatteEndpoint;
|
||||||
use crate::timdorr::TimdorrEndpoint;
|
use crate::timdorr::TimdorrEndpoint;
|
||||||
use crate::vehicle_command::VehicleCommandEndpoint;
|
use crate::vehicle_command::VehicleCommandEndpoint;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use reqwest::Method;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
@ -23,6 +24,15 @@ const VEHICLE_COMMAND_FILE: &str = "vehicle_command.go";
|
||||||
const FLEET_API_URL: &str = "https://developer.tesla.com/docs/fleet-api";
|
const FLEET_API_URL: &str = "https://developer.tesla.com/docs/fleet-api";
|
||||||
const FLEET_API_FILE: &str = "fleet.html";
|
const FLEET_API_FILE: &str = "fleet.html";
|
||||||
|
|
||||||
|
trait Restful {
|
||||||
|
fn method(&self) -> &Method;
|
||||||
|
fn uri(&self) -> &str;
|
||||||
|
}
|
||||||
|
|
||||||
|
trait EndpointGeneral {
|
||||||
|
fn name(&self) -> &str;
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[clap(author, version)]
|
#[clap(author, version)]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
|
@ -45,20 +55,25 @@ async fn main() {
|
||||||
let mut teslatte_project_path = PathBuf::from_str(env!("CARGO_MANIFEST_DIR")).unwrap();
|
let mut teslatte_project_path = PathBuf::from_str(env!("CARGO_MANIFEST_DIR")).unwrap();
|
||||||
teslatte_project_path.push("..");
|
teslatte_project_path.push("..");
|
||||||
let teslatte_endpoints = teslatte::parse(&teslatte_project_path).unwrap();
|
let teslatte_endpoints = teslatte::parse(&teslatte_project_path).unwrap();
|
||||||
let teslatte_endpoints: HashMap<String, TeslatteEndpoint> = teslatte_endpoints
|
|
||||||
.into_iter()
|
|
||||||
.map(|e| (e.name.clone(), e))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let fleet_endpoints = fleet::parse(&fleet_html);
|
let mut fleet_endpoints = fleet::parse(&fleet_html);
|
||||||
let command_endpoints = vehicle_command::parse(&command_golang);
|
let command_endpoints = vehicle_command::parse(&command_golang);
|
||||||
let timdorr_endpoints = timdorr::parse(&timdorr);
|
let mut timdorr_endpoints = timdorr::parse(&timdorr);
|
||||||
|
|
||||||
|
info!("TOTALS: (before filtering and merging)");
|
||||||
info!("{} endpoints in teslatte", teslatte_endpoints.len());
|
info!("{} endpoints in teslatte", teslatte_endpoints.len());
|
||||||
info!("{} endpoints in fleet", fleet_endpoints.len());
|
info!("{} endpoints in fleet", fleet_endpoints.len());
|
||||||
info!("{} endpoints in command", command_endpoints.len());
|
info!("{} endpoints in command", command_endpoints.len());
|
||||||
info!("{} endpoints in timdorr", timdorr_endpoints.len());
|
info!("{} endpoints in timdorr", timdorr_endpoints.len());
|
||||||
|
|
||||||
|
// Before we merge let's mangle the names so the URI's match.
|
||||||
|
info!("-- rename timdorr based on teslatte");
|
||||||
|
rename_keys_based_on_uri(&teslatte_endpoints, &mut timdorr_endpoints);
|
||||||
|
info!("-- rename fleet based on teslatte");
|
||||||
|
rename_keys_based_on_uri(&teslatte_endpoints, &mut fleet_endpoints);
|
||||||
|
info!("-- rename timdorr based on fleet");
|
||||||
|
rename_keys_based_on_uri(&fleet_endpoints, &mut timdorr_endpoints);
|
||||||
|
|
||||||
let mut merged = merge(
|
let mut merged = merge(
|
||||||
teslatte_endpoints,
|
teslatte_endpoints,
|
||||||
fleet_endpoints,
|
fleet_endpoints,
|
||||||
|
@ -67,10 +82,56 @@ async fn main() {
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
remove_unwanted_endpoints(&mut merged);
|
||||||
|
ensure_timdorr_matches_fleet(&merged);
|
||||||
|
|
||||||
|
dbg!(&merged);
|
||||||
|
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rename_keys_based_on_uri(
|
||||||
|
base: &HashMap<String, impl Restful>,
|
||||||
|
mut to_rename: &mut HashMap<String, impl Restful>,
|
||||||
|
) {
|
||||||
|
let mut renames = vec![];
|
||||||
|
for (base_key, base_endpoint) in base.iter() {
|
||||||
|
let mut seen = false;
|
||||||
|
for (rename_key, rename_endpoint) in to_rename.iter() {
|
||||||
|
if base_endpoint.uri() != rename_endpoint.uri() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if base_endpoint.method() != rename_endpoint.method() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if base_key == rename_key {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
info!(
|
||||||
|
"Rename {rename_key} -> {base_key} for {}",
|
||||||
|
base_endpoint.uri()
|
||||||
|
);
|
||||||
|
if seen {
|
||||||
|
panic!("Duplicate rename for {base_key} -> {rename_key}");
|
||||||
|
}
|
||||||
|
|
||||||
|
renames.push((base_key.to_string(), rename_key.to_string()));
|
||||||
|
seen = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (base_key, rename_key) in renames {
|
||||||
|
let endpoint = to_rename.remove(&rename_key).unwrap();
|
||||||
|
to_rename.insert(base_key.to_string(), endpoint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ensure_timdorr_matches_fleet(merged: &HashMap<String, Endpoint>) {
|
||||||
// Let's do a check to see if the fleet api matches the timdorr api.
|
// Let's do a check to see if the fleet api matches the timdorr api.
|
||||||
// If it doesn't, let's panic!
|
// If it doesn't, let's panic!
|
||||||
let mut perfect = true;
|
let mut perfect = true;
|
||||||
for (k, v) in &merged {
|
for (k, v) in merged {
|
||||||
if let Some(fleet) = &v.fleet {
|
if let Some(fleet) = &v.fleet {
|
||||||
if let Some(timdorr) = &v.timdorr {
|
if let Some(timdorr) = &v.timdorr {
|
||||||
if fleet.uri != timdorr.uri {
|
if fleet.uri != timdorr.uri {
|
||||||
|
@ -80,26 +141,26 @@ async fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !perfect {
|
if !perfect {
|
||||||
panic!("Fleet and Timdorr don't match. See errors above.");
|
panic!("Fleet and Timdorr don't match. See errors above.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter_interesting_endpoints(&mut merged);
|
|
||||||
todo!();
|
|
||||||
|
|
||||||
dbg!(&merged);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove endpoints that we're not interested in (yet) in place.
|
/// Remove endpoints that we're not interested in (yet) in place.
|
||||||
// pub fn filter_interesting_endpoints(mut endpoints: &mut HashMap<String, Endpoint>) {
|
///
|
||||||
// endpoints.retain(|_, e| {
|
/// Base it on timdorr's list.
|
||||||
// !e. starts_with("/api/1/directives")
|
pub fn remove_unwanted_endpoints(mut endpoints: &mut HashMap<String, Endpoint>) {
|
||||||
// && !e.starts_with("/api/1/subscriptions")
|
endpoints.retain(|_, e| {
|
||||||
// && !e.starts_with("/api/1/dx/")
|
let Some(timdorr) = &e.timdorr else {
|
||||||
// && !e.starts_with("/bff/v2/mobile-app")
|
return true;
|
||||||
// });
|
};
|
||||||
// }
|
let uri = &timdorr.uri;
|
||||||
|
!uri.starts_with("/api/1/directives")
|
||||||
|
&& !uri.starts_with("/api/1/subscriptions")
|
||||||
|
&& !uri.starts_with("/api/1/dx/")
|
||||||
|
&& !uri.starts_with("/bff/v2/mobile-app")
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Endpoint {
|
pub struct Endpoint {
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
//! Parse the whole teslatte project and find any get*! post*! macros.
|
//! Parse the whole teslatte project and find any get*! post*! macros.
|
||||||
|
|
||||||
use crate::nom_help::{ignore_whitespace, quoted_string, short_trace};
|
use crate::nom_help::{ignore_whitespace, quoted_string, short_trace};
|
||||||
|
use crate::Restful;
|
||||||
use heck::ToKebabCase;
|
use heck::ToKebabCase;
|
||||||
use nom::branch::alt;
|
use nom::branch::alt;
|
||||||
use nom::bytes::complete::{tag, take_while1};
|
use nom::bytes::complete::{tag, take_while1};
|
||||||
use nom::combinator::opt;
|
use nom::combinator::opt;
|
||||||
use nom::IResult;
|
use nom::IResult;
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use tracing::{debug, trace};
|
use tracing::{debug, trace};
|
||||||
|
@ -20,7 +22,17 @@ pub struct TeslatteEndpoint {
|
||||||
// pub post_struct: Option<String>,
|
// pub post_struct: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(path: &Path) -> anyhow::Result<Vec<TeslatteEndpoint>> {
|
impl Restful for TeslatteEndpoint {
|
||||||
|
fn method(&self) -> &Method {
|
||||||
|
&self.method
|
||||||
|
}
|
||||||
|
|
||||||
|
fn uri(&self) -> &str {
|
||||||
|
&self.uri
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(path: &Path) -> anyhow::Result<HashMap<String, TeslatteEndpoint>> {
|
||||||
let mut path = PathBuf::from(path);
|
let mut path = PathBuf::from(path);
|
||||||
path.push("src");
|
path.push("src");
|
||||||
path.push("**");
|
path.push("**");
|
||||||
|
@ -40,8 +52,13 @@ pub fn parse(path: &Path) -> anyhow::Result<Vec<TeslatteEndpoint>> {
|
||||||
// Change the name to be kebab-case.
|
// Change the name to be kebab-case.
|
||||||
for endpoint in &mut endpoints {
|
for endpoint in &mut endpoints {
|
||||||
endpoint.name = endpoint.name.to_kebab_case();
|
endpoint.name = endpoint.name.to_kebab_case();
|
||||||
|
// Insert "/api/1" at the beginning of the URI.
|
||||||
|
endpoint.uri = format!("/api/1{}", endpoint.uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let endpoints: HashMap<String, TeslatteEndpoint> =
|
||||||
|
endpoints.into_iter().map(|e| (e.name.clone(), e)).collect();
|
||||||
|
|
||||||
Ok(endpoints)
|
Ok(endpoints)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
use crate::Restful;
|
||||||
use heck::ToKebabCase;
|
use heck::ToKebabCase;
|
||||||
|
use reqwest::Method;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
@ -11,6 +13,22 @@ pub struct TimdorrEndpoint {
|
||||||
pub auth: bool,
|
pub auth: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Restful for TimdorrEndpoint {
|
||||||
|
fn method(&self) -> &Method {
|
||||||
|
match self.method.as_str() {
|
||||||
|
"GET" => &Method::GET,
|
||||||
|
"POST" => &Method::POST,
|
||||||
|
"PUT" => &Method::PUT,
|
||||||
|
"DELETE" => &Method::DELETE,
|
||||||
|
_ => panic!("Unknown method {}", self.method),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn uri(&self) -> &str {
|
||||||
|
&self.uri
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse(json_str: &str) -> HashMap<String, TimdorrEndpoint> {
|
pub fn parse(json_str: &str) -> HashMap<String, TimdorrEndpoint> {
|
||||||
let map: HashMap<String, TimdorrEndpoint> = serde_json::from_str(json_str).unwrap();
|
let map: HashMap<String, TimdorrEndpoint> = serde_json::from_str(json_str).unwrap();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue