chore: cleanups. nicer interactive message.

This commit is contained in:
gak 2023-08-29 12:16:07 +10:00
parent e4191c75f3
commit 9d020efa8c
No known key found for this signature in database
4 changed files with 31 additions and 11 deletions

View file

@ -32,9 +32,17 @@ impl Api {
/// Currently the only way to "authenticate" to an access token for this library.
pub async fn from_interactive_url() -> Result<Api, TeslatteError> {
let login_form = Self::get_login_url_for_user().await;
dbg!(&login_form);
let callback_url =
ask_input("Enter the URL of the 404 error page after you've logged in: ");
println!("{}", "-".repeat(80));
println!("{}", login_form.url);
println!("{}", "-".repeat(80));
println!(
r#"Visit the URL above, and log in to your Tesla account if not already logged in.
After you log in (or already logged in), it will redirect you to a 404 error
page, where the URL will start with https://auth.tesla.com/void/callback?code=...
"#
);
let callback_url = ask_input("Enter the whole URL of the 404 page: ");
println!(); // Newline to make the next output more separated and clear.
let callback = Self::extract_callback_from_url(&callback_url)?;
if callback.state != login_form.state {
@ -224,9 +232,17 @@ struct BearerTokenRequest {
struct BearerTokenResponse {
access_token: String,
refresh_token: String,
#[allow(dead_code)]
expires_in: u32,
#[allow(dead_code)]
state: String,
#[allow(dead_code)]
token_type: String,
#[allow(dead_code)]
id_token: String,
}

View file

@ -29,9 +29,9 @@ pub struct GatewayId(String);
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum EnergySite {
Vehicle(VehicleData),
Solar(SolarData),
Powerwall(PowerwallData),
Vehicle(Box<VehicleData>),
Solar(Box<SolarData>),
Powerwall(Box<PowerwallData>),
}
/// This is assumed from https://tesla-api.timdorr.com/api-basics/products

View file

@ -37,6 +37,7 @@ pub struct ExternalVehicleId(u64);
pub struct Api {
pub access_token: AccessToken,
// TODO: Why is this an Option?
pub refresh_token: Option<RefreshToken>,
client: Client,
}

View file

@ -76,7 +76,7 @@ async fn main() -> miette::Result<()> {
match args.command {
Command::Auth { save } => {
let api = Api::from_interactive_url().await?;
updated_tokens(save, &api);
print_or_save_tokens(save, &api);
}
Command::Refresh { refresh_token } => {
let (save, refresh_token) = match refresh_token {
@ -88,7 +88,7 @@ async fn main() -> miette::Result<()> {
};
let api = Api::from_refresh_token(&refresh_token).await?;
updated_tokens(save, &api);
print_or_save_tokens(save, &api);
}
Command::Api(api_args) => {
let (access_token, refresh_token) = match &api_args.access_token {
@ -125,17 +125,20 @@ async fn main() -> miette::Result<()> {
Ok(())
}
fn updated_tokens(save: bool, api: &Api) {
fn print_or_save_tokens(save: bool, api: &Api) {
let access_token = api.access_token.clone();
let refresh_token = api.refresh_token.clone().unwrap();
println!("Access token: {}", access_token);
println!("Refresh token: {}", refresh_token);
if save {
Config {
access_token,
refresh_token,
}
.save();
println!("Saved tokens to cli.json");
} else {
println!("Access token: {}", access_token);
println!("Refresh token: {}", refresh_token);
}
}