refactor: login-logout action
This commit is contained in:
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