use std::{fmt::Display, io::stdout}; use nyquest_preset::nyquest::AsyncClient; use sdgb_api::{ ApiError, title::{ MaiVersionExt as _, Sdgb1_53, methods::APIMethod, model::{UserLoginApi, UserLoginApiResp, UserLogoutApi, UserLogoutApiResp}, }, }; use serde::Serialize; use spdlog::info; pub async fn login_action( client: &AsyncClient, user_id: u32, token: Option, action: impl AsyncFnOnce(UserLoginApiResp) -> R, ) -> Result { let login = UserLoginApi::new(user_id, true, token); let login_date_time = login.date_time; let login_resp: UserLoginApiResp = Sdgb1_53::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_53::request::<_, UserLogoutApiResp>( &client, APIMethod::UserLogoutApi, user_id, UserLogoutApi { user_id, login_date_time, ..Default::default() }, ) .await; info!("logout: {logout_resp:?}"); Ok(return_data) } pub fn json_display(value: impl Serialize) -> Result<(), Box> { let lock = stdout().lock(); serde_json::to_writer_pretty(lock, &value)?; Ok(()) } pub fn human_readable_display( value: impl Display + Serialize, human_readable: bool, ) -> Result<(), Box> { if human_readable { println!("{value}"); } else { json_display(value)?; } Ok(()) } #[cfg(feature = "fetchall")] pub mod helpers;