feat: initial support for GetUserRating

This commit is contained in:
mokurin000
2025-08-01 01:15:13 +08:00
parent 0b8de2b4bc
commit 9b046036c9
12 changed files with 139 additions and 15 deletions

View File

@@ -16,11 +16,20 @@ fetchall = ["dep:redb", "dep:futures-util"]
[dependencies]
sdgb-api = { workspace = true, features = ["bincode"] }
spdlog-rs = { workspace = true }
snafu = { workspace = true }
# (de)serialization
serde = { workspace = true }
serde_json = { workspace = true }
strum = { workspace = true }
# logging / errors
spdlog-rs = { workspace = true }
snafu = { workspace = true }
# kv database
redb = { workspace = true, optional = true }
# async runtime
tokio = { workspace = true, features = ["macros"], optional = true }
compio = { workspace = true, features = ["macros"], optional = true }

View File

@@ -5,6 +5,9 @@ use strum::EnumString;
#[derive(Parser)]
#[command(about = "SDGB api tool", long_about = env!("CARGO_PKG_DESCRIPTION"))]
pub struct Cli {
/// try to generate human readable output.
#[arg(short = 'M', long)]
pub machine_readable: bool,
#[command(subcommand)]
pub command: Commands,
}
@@ -37,6 +40,10 @@ pub enum Commands {
#[arg(short, long)]
user_id: u32,
},
Rating {
#[arg(short, long)]
user_id: u32,
},
// below requires login
Userdata {

View File

@@ -11,14 +11,18 @@ use sdgb_api::{
MaiVersionExt, Sdgb1_40, Sdgb1_50,
methods::APIMethod,
model::{
GetUserDataApi, GetUserDataApiResp, GetUserPreviewApi, GetUserPreviewApiResp, Ping,
PingResp, UserLogoutApi, UserLogoutApiResp,
GetUserDataApi, GetUserDataApiResp, GetUserPreviewApi, GetUserPreviewApiResp,
GetUserRatingApi, GetUserRatingApiResp, Ping, PingResp, UserLogoutApi,
UserLogoutApiResp,
},
},
};
use spdlog::{error, info, warn};
use crate::{commands::Cli, utils::login_action};
use crate::{
commands::Cli,
utils::{human_readable_display, login_action},
};
#[cfg(feature = "fetchall")]
mod cache;
@@ -48,12 +52,27 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
}
})?;
let Cli { command } = <Cli as Parser>::parse();
let Cli {
command,
machine_readable,
} = <Cli as Parser>::parse();
let human_readable = !machine_readable;
let client = ClientBuilder::default().build_async().await?;
// TODO: refactor via enum_dispatch
match command {
commands::Commands::Rating { user_id } => {
let rating: GetUserRatingApiResp = Sdgb1_50::request(
&client,
APIMethod::GetUserRatingApi,
user_id,
GetUserRatingApi { user_id },
)
.await?;
human_readable_display(rating, human_readable)?;
}
commands::Commands::Logout { user_id, timestamp } => {
let logout: UserLogoutApiResp = Sdgb1_50::request(
&client,
@@ -77,7 +96,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
)
.await?;
println!("{preview}");
human_readable_display(preview, human_readable)?;
}
commands::Commands::Ping => {
let decoded: PingResp = Sdgb1_40::request(

View File

@@ -1,3 +1,5 @@
use std::{fmt::Display, io::stdout};
use nyquest_preset::nyquest::AsyncClient;
use sdgb_api::{
ApiError,
@@ -7,6 +9,7 @@ use sdgb_api::{
model::{UserLoginApi, UserLoginApiResp, UserLogoutApi, UserLogoutApiResp},
},
};
use serde::Serialize;
use spdlog::info;
pub async fn login_action<R>(
@@ -44,3 +47,17 @@ pub async fn login_action<R>(
info!("logout: {logout_resp:?}");
Ok(return_data)
}
pub fn human_readable_display(
value: impl Display + Serialize,
human_readable: bool,
) -> Result<(), Box<dyn snafu::Error>> {
if human_readable {
println!("{value}");
} else {
let lock = stdout().lock();
serde_json::to_writer_pretty(lock, &value)?;
}
Ok(())
}