From 53e6f72cbbe55ede1d07c8935020f09c86f57640 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sat, 13 Jan 2024 16:16:34 +0100 Subject: [PATCH] Add Login Error Text Display and Logout callback --- src/jnovel.rs | 28 +++++++++++++++++++--------- src/main.rs | 17 +++++++++-------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/jnovel.rs b/src/jnovel.rs index 3702dbb..893c52d 100644 --- a/src/jnovel.rs +++ b/src/jnovel.rs @@ -24,7 +24,7 @@ pub(crate) struct State { } impl State { - pub(crate) async fn login(&self) -> LoginState { + pub(crate) async fn login() -> LoginState { let client = reqwest::Client::new(); let res = match client.get(api!() + "app/v1/auth/otp4app/generate?format=json") @@ -49,7 +49,15 @@ impl State { } } - pub(crate) async fn load_library(&self) -> LibraryState { + pub(crate) async fn logout(state: &Self) { + let client = reqwest::Client::new(); + + let url = api!() + format!("app/v1/auth/logout?{:?}", state.token).as_str(); + + let _ = client.post(url).send().await; + } + + pub(crate) async fn load_library(state: State) -> LibraryState { /* 1: lib = https://labs.j-novel.club/app/v1/me/library 2: every vol in lib.books serie = https://labs.j-novel.club/app/v1/volumes/{vol.volume.legacyId}/serie?format=json @@ -58,7 +66,9 @@ impl State { */ let client = reqwest::Client::new(); - let url = api!() + format!("app/v1/me/library?format=json&{:?}", self.token).as_str(); + let url = api!() + format!("app/v1/me/library?format=json&{:?}", state.token).as_str(); + + println!("{url}"); let res = match client.get(url) .send() @@ -68,9 +78,9 @@ impl State { }; match res.text().await { - Ok(otp_data) => { - let otp = match serde_json::from_str(&otp_data) { - Ok(otp) => otp, + Ok(text) => { + let library = match serde_json::from_str(&text) { + Ok(library) => library, Err(e) => return LibraryState::Error(e.to_string()), }; @@ -144,11 +154,11 @@ impl Otp { } } -#[derive(Debug, Copy, Clone, Eq, PartialEq, Deserialize)] +#[derive(Debug, Clone, Eq, PartialEq, Deserialize)] pub(crate) struct OtpToken { pub(crate) id: ArrayString<36>, - pub(crate) ttl: ArrayString<16>, - pub(crate) created: ArrayString<16> + pub(crate) ttl: String, + pub(crate) created: String } #[derive(Debug, Clone, Eq, PartialEq)] diff --git a/src/main.rs b/src/main.rs index e80a21d..9ec230b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,6 @@ use chrono::{DateTime, Local, Utc}; use reqwest::StatusCode; use serde::{Deserialize}; -use crate::Menu::JNovel; - #[derive(Debug, Clone)] struct CalibreWebImporter { jnc: jnovel::State, @@ -24,8 +22,8 @@ impl Default for CalibreWebImporter { Self { jnc: jnovel::State::default(), settings: AppSettings::default(), - current_menu: JNovel, - previous_menu: JNovel, + current_menu: Menu::JNovel, + previous_menu: Menu::JNovel, } } } @@ -117,13 +115,13 @@ impl Application for CalibreWebImporter { jnovel::Message::Login => { self.jnc.login_state = jnovel::LoginState::LoggingIn; let _ = open::that("https://j-novel.club/user/otp"); - Command::perform(self.jnc.login(), |status| Message::JncAction(jnovel::Message::LoginState(status))) + Command::perform(jnovel::State::login(), |status| Message::JncAction(jnovel::Message::LoginState(status))) } jnovel::Message::Logout => { println!("Logging out from J-Novel Club"); self.jnc.token = None; self.jnc.login_state = jnovel::LoginState::LoggedOut; - Command::none() + Command::perform(jnovel::State::logout(self.jnc.clone()), || Message::JncAction(jnovel::Message::LoginState(jnovel::LoginState::LoggedOut))) } jnovel::Message::LoginState(status) => { if let jnovel::LoginState::Success(token) = status { @@ -131,7 +129,7 @@ impl Application for CalibreWebImporter { self.jnc.login_state = status; if self.jnc.library_state == jnovel::LibraryState::Unloaded { - Command::perform(self.jnc.load_library(), |status| Message::JncAction(jnovel::Message::LibraryState(status))) + Command::perform(jnovel::State::load_library(self.jnc.clone()), |status| Message::JncAction(jnovel::Message::LibraryState(status))) } else { Command::none() @@ -190,7 +188,10 @@ impl Application for CalibreWebImporter { jnovel::LoginState::LoggedOut => Element::from(button("Login").on_press(Message::JncAction(jnovel::Message::Login))), jnovel::LoginState::LoggingIn => Element::from(text("Logging in")), jnovel::LoginState::AwaitingConfirmation(code, start) => Element::from(text(format!("Login Code: {}. Expiring: {}s", code.otp, 600 - (Local::now() - start).num_seconds()))), - jnovel::LoginState::Error(_) => Element::from(button("Error").on_press(Message::JncAction(jnovel::Message::LoginState(jnovel::LoginState::LoggedOut)))), + jnovel::LoginState::Error(err) => Element::from(row![ + button("Error").on_press(Message::JncAction(jnovel::Message::LoginState(jnovel::LoginState::LoggedOut))), + text(err), + ]), }; column(vec![Element::from(