refactor: login-logout action
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use aes::cipher::{block_padding::UnpadError, inout::PadError};
|
||||
use snafu::Snafu;
|
||||
|
||||
use crate::title::model::LoginError;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum ApiError {
|
||||
JoinError,
|
||||
@@ -33,6 +35,12 @@ pub enum ApiError {
|
||||
Request {
|
||||
source: nyquest::Error,
|
||||
},
|
||||
|
||||
#[snafu(display("login error: {source}"))]
|
||||
#[snafu(context(false))]
|
||||
Login {
|
||||
source: LoginError,
|
||||
},
|
||||
}
|
||||
|
||||
impl From<UnpadError> for ApiError {
|
||||
|
||||
@@ -8,7 +8,7 @@ mod user_logout_api;
|
||||
pub use user_logout_api::{UserLogoutApi, UserLogoutApiResp};
|
||||
|
||||
mod user_login_api;
|
||||
pub use user_login_api::{UserLoginApi, UserLoginApiResp};
|
||||
pub use user_login_api::{LoginError, UserLoginApi, UserLoginApiResp};
|
||||
|
||||
mod get_user_data_api;
|
||||
pub use get_user_data_api::{GetUserDataApi, GetUserDataApiResp};
|
||||
|
||||
@@ -52,3 +52,28 @@ impl UserLoginApi {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UserLoginApiResp {
|
||||
pub fn error(&self) -> Option<LoginError> {
|
||||
match self.return_code {
|
||||
1 => None,
|
||||
100 => Some(LoginError::AlreadyLogged),
|
||||
102 => Some(LoginError::QRCodeExpired),
|
||||
103 => Some(LoginError::AccountUnregistered),
|
||||
error @ _ => Some(LoginError::Unknown { error }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, snafu::Snafu)]
|
||||
pub enum LoginError {
|
||||
#[snafu(display("QRCode was expired"))]
|
||||
QRCodeExpired,
|
||||
#[snafu(display("You did not logout last session"))]
|
||||
AlreadyLogged,
|
||||
#[snafu(display("userId does not exist"))]
|
||||
AccountUnregistered,
|
||||
|
||||
#[snafu(display("Unknown error: {error}"))]
|
||||
Unknown { error: i32 },
|
||||
}
|
||||
|
||||
@@ -18,17 +18,18 @@ use sdgb_api::{
|
||||
methods::APIMethod,
|
||||
model::{
|
||||
GetUserDataApi, GetUserDataApiResp, GetUserPreviewApi, GetUserPreviewApiResp, Ping,
|
||||
PingResp, UserLoginApi, UserLoginApiResp, UserLogoutApi, UserLogoutApiResp,
|
||||
PingResp, UserLogoutApi, UserLogoutApiResp,
|
||||
},
|
||||
},
|
||||
};
|
||||
use spdlog::{error, info, warn};
|
||||
|
||||
use crate::commands::Cli;
|
||||
use crate::{commands::Cli, utils::login_action};
|
||||
|
||||
#[cfg(feature = "cache")]
|
||||
mod cache;
|
||||
mod commands;
|
||||
mod utils;
|
||||
|
||||
static EARLY_QUIT: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
@@ -202,48 +203,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
|
||||
serde_json::to_writer_pretty(output, &players)?;
|
||||
}
|
||||
commands::Commands::Userdata { user_id } => {
|
||||
let login = UserLoginApi::new(user_id);
|
||||
let date_time = login.date_time;
|
||||
let Ok(login_resp): Result<UserLoginApiResp, _> =
|
||||
Sdgb1_50::request(&client, APIMethod::UserLoginApi, user_id, login).await
|
||||
else {
|
||||
let logout_resp: UserLogoutApiResp = Sdgb1_50::request(
|
||||
&client,
|
||||
APIMethod::UserLogoutApi,
|
||||
user_id,
|
||||
UserLogoutApi {
|
||||
user_id,
|
||||
date_time,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
info!("logout: {logout_resp:?}");
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
match login_resp.return_code {
|
||||
1 => info!("login succeed"),
|
||||
100 => {
|
||||
error!("user already logged");
|
||||
return Ok(());
|
||||
}
|
||||
102 => {
|
||||
error!("QRCode expired");
|
||||
return Ok(());
|
||||
}
|
||||
103 => {
|
||||
error!("Unregistered userId");
|
||||
return Ok(());
|
||||
}
|
||||
e @ _ => {
|
||||
error!("unknown login error: {e}");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
match Sdgb1_50::request::<_, GetUserDataApiResp>(
|
||||
let action = async |_| match Sdgb1_50::request::<_, GetUserDataApiResp>(
|
||||
&client,
|
||||
APIMethod::GetUserDataApi,
|
||||
user_id,
|
||||
@@ -257,21 +217,8 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
|
||||
Err(e) => {
|
||||
error!("failed to get userdata: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
let logout_resp: UserLogoutApiResp = Sdgb1_50::request(
|
||||
&client,
|
||||
APIMethod::UserLogoutApi,
|
||||
user_id,
|
||||
UserLogoutApi {
|
||||
user_id,
|
||||
date_time,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
info!("logout: {logout_resp:?}");
|
||||
};
|
||||
login_action(&client, user_id, action).await?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
46
sdgb-cli/src/utils/mod.rs
Normal file
46
sdgb-cli/src/utils/mod.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use nyquest_preset::nyquest::AsyncClient;
|
||||
use sdgb_api::{
|
||||
ApiError,
|
||||
title::{
|
||||
MaiVersionExt as _, Sdgb1_50,
|
||||
methods::APIMethod,
|
||||
model::{UserLoginApi, UserLoginApiResp, UserLogoutApi, UserLogoutApiResp},
|
||||
},
|
||||
};
|
||||
use spdlog::info;
|
||||
|
||||
pub async fn login_action<R>(
|
||||
client: &AsyncClient,
|
||||
user_id: u32,
|
||||
action: impl AsyncFn(UserLoginApiResp) -> R,
|
||||
) -> Result<R, ApiError> {
|
||||
let login = UserLoginApi::new(user_id);
|
||||
let date_time = login.date_time;
|
||||
|
||||
info!("login unix timestamp: {date_time}");
|
||||
|
||||
let login_resp: UserLoginApiResp =
|
||||
Sdgb1_50::request(&client, APIMethod::UserLoginApi, user_id, login).await?;
|
||||
|
||||
match login_resp.error() {
|
||||
None => info!("login succeed"),
|
||||
Some(e) => return Err(e)?,
|
||||
}
|
||||
|
||||
let return_data = action(login_resp).await;
|
||||
|
||||
let logout_resp = Sdgb1_50::request::<_, UserLogoutApiResp>(
|
||||
&client,
|
||||
APIMethod::UserLogoutApi,
|
||||
user_id,
|
||||
UserLogoutApi {
|
||||
user_id,
|
||||
date_time,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
info!("logout: {logout_resp:?}");
|
||||
Ok(return_data)
|
||||
}
|
||||
Reference in New Issue
Block a user