mirror of
https://codeberg.org/demostf/api-client.git
synced 2026-06-03 08:34:15 +02:00
incompatible updates for 0.5
This commit is contained in:
parent
216ae4540e
commit
d2fee4d3eb
4 changed files with 413 additions and 953 deletions
1265
Cargo.lock
generated
1265
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
15
Cargo.toml
15
Cargo.toml
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "demostf-client"
|
||||
version = "0.4.7"
|
||||
version = "0.5.0"
|
||||
authors = ["Robin Appelman <robin@icewind.nl>"]
|
||||
edition = "2021"
|
||||
description = "Api client for demos.tf"
|
||||
|
|
@ -14,15 +14,17 @@ rust-version = "1.88.0"
|
|||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
time = { version = "0.3.44", features = ["serde"] }
|
||||
reqwest = { version = "0.12.15", default-features = false, features = [
|
||||
time = { version = "0.3.47", features = ["serde"] }
|
||||
reqwest = { version = "0.13.3", default-features = false, features = [
|
||||
"json",
|
||||
"multipart",
|
||||
"stream",
|
||||
"form",
|
||||
"query"
|
||||
] }
|
||||
thiserror = "2.0.18"
|
||||
hex = "0.4.3"
|
||||
steamid-ng = "1.0.0"
|
||||
steamid-ng = "3.0.0"
|
||||
bytes = "1.11.1"
|
||||
futures-util = "0.3.32"
|
||||
tracing = "0.1.44"
|
||||
|
|
@ -33,8 +35,3 @@ md5 = "0.8.0"
|
|||
tokio = { version = "1.52.3", features = ["macros"] }
|
||||
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio-rustls"] }
|
||||
tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
|
||||
|
||||
[features]
|
||||
default = ["default-tls"]
|
||||
default-tls = ["reqwest/default-tls"]
|
||||
rustls-tls = ["reqwest/rustls-tls"]
|
||||
|
|
|
|||
74
src/lib.rs
74
src/lib.rs
|
|
@ -3,11 +3,13 @@ pub use client::ApiClient;
|
|||
use futures_util::{Stream, StreamExt};
|
||||
use md5::Context;
|
||||
use reqwest::StatusCode;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
|
||||
use std::borrow::Cow;
|
||||
use std::fmt::{self, Debug, Display, Formatter};
|
||||
use std::io::Write;
|
||||
use std::str::FromStr;
|
||||
pub use steamid_ng::SteamID;
|
||||
use steamid_ng::{Instance, InstanceFlags, InstanceType};
|
||||
use thiserror::Error;
|
||||
use time::OffsetDateTime;
|
||||
use tinyvec::TinyVec;
|
||||
|
|
@ -180,7 +182,7 @@ impl UserRef {
|
|||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct User {
|
||||
pub id: u32,
|
||||
#[serde(rename = "steamid")]
|
||||
#[serde(rename = "steamid", deserialize_with = "deserialize_steamid")]
|
||||
pub steam_id: SteamID,
|
||||
pub name: String,
|
||||
}
|
||||
|
|
@ -204,7 +206,7 @@ pub struct Player {
|
|||
#[derive(Clone, Debug, Deserialize)]
|
||||
struct NestedPlayerUser {
|
||||
user_id: u32,
|
||||
#[serde(rename = "steamid")]
|
||||
#[serde(rename = "steamid", deserialize_with = "deserialize_steamid")]
|
||||
steam_id: SteamID,
|
||||
name: String,
|
||||
}
|
||||
|
|
@ -221,6 +223,14 @@ where
|
|||
})
|
||||
}
|
||||
|
||||
fn deserialize_steamid<'de, D>(deserializer: D) -> Result<SteamID, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = <Cow<'static, str>>::deserialize(deserializer)?;
|
||||
SteamID::from_str(&s).map_err(D::Error::custom)
|
||||
}
|
||||
|
||||
/// Player team, red or blue
|
||||
#[derive(Clone, Copy, Debug, Deserialize, PartialOrd, PartialEq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
|
|
@ -333,24 +343,30 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
struct PlayerList(TinyVec<[SteamID; 2]>);
|
||||
#[derive(Debug, Default)]
|
||||
struct PlayerList(TinyVec<[Option<SteamID>; 2]>);
|
||||
|
||||
impl PlayerList {
|
||||
fn new<T: Into<SteamID>, I: IntoIterator<Item = T>>(players: I) -> Self {
|
||||
PlayerList(players.into_iter().map(Into::into).collect())
|
||||
fn new<T: IntoSteamId, I: IntoIterator<Item = T>>(players: I) -> Self {
|
||||
PlayerList(
|
||||
players
|
||||
.into_iter()
|
||||
.map(IntoSteamId::into_steam_id)
|
||||
.map(Some)
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for PlayerList {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
let mut first = true;
|
||||
for steam_id in &self.0 {
|
||||
for steam_id in self.0.iter().flatten() {
|
||||
if first {
|
||||
first = false;
|
||||
write!(f, "{}", u64::from(*steam_id))?;
|
||||
write!(f, "{}", steam_id.steam64())?;
|
||||
} else {
|
||||
write!(f, ",{}", u64::from(*steam_id))?;
|
||||
write!(f, ",{}", steam_id.steam64())?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -369,20 +385,50 @@ impl Serialize for PlayerList {
|
|||
|
||||
#[test]
|
||||
fn test_serialize_player_list() {
|
||||
fn id(id: u64) -> SteamID {
|
||||
SteamID::from_steam64(id).unwrap()
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
"76561198024494988",
|
||||
PlayerList::new([76561198024494988]).to_string()
|
||||
PlayerList::new([id(76561198024494988)]).to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
"76561198024494988,76561197963701107",
|
||||
PlayerList::new([76561198024494988, 76561197963701107]).to_string()
|
||||
PlayerList::new([id(76561198024494988), id(76561197963701107)]).to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
"76561198024494988,76561197963701107,76561197963701106",
|
||||
PlayerList::new([76561198024494988, 76561197963701107, 76561197963701106]).to_string()
|
||||
PlayerList::new([
|
||||
id(76561198024494988),
|
||||
id(76561197963701107),
|
||||
id(76561197963701106)
|
||||
])
|
||||
.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
pub trait IntoSteamId {
|
||||
fn into_steam_id(self) -> SteamID;
|
||||
}
|
||||
|
||||
impl IntoSteamId for SteamID {
|
||||
fn into_steam_id(self) -> SteamID {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoSteamId for u64 {
|
||||
fn into_steam_id(self) -> SteamID {
|
||||
SteamID::from_steam64(self).unwrap_or(SteamID::new(
|
||||
0,
|
||||
Instance::new(InstanceType::All, InstanceFlags::None),
|
||||
steamid_ng::AccountType::Invalid,
|
||||
steamid_ng::Universe::Invalid,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl ListParams {
|
||||
/// Specify the backend name to filter demos with
|
||||
#[must_use]
|
||||
|
|
@ -404,7 +450,7 @@ impl ListParams {
|
|||
|
||||
/// Specify the player steam ids to filter demos with
|
||||
#[must_use]
|
||||
pub fn with_players<T: Into<SteamID>, I: IntoIterator<Item = T>>(self, players: I) -> Self {
|
||||
pub fn with_players<T: IntoSteamId, I: IntoIterator<Item = T>>(self, players: I) -> Self {
|
||||
ListParams {
|
||||
players: PlayerList::new(players),
|
||||
..self
|
||||
|
|
|
|||
|
|
@ -11,6 +11,10 @@ fn test_demo_path() -> String {
|
|||
std::env::var("TEST_DEMO").unwrap_or_else(|_| "./tests/data/gully.dem".to_string())
|
||||
}
|
||||
|
||||
fn id(id: u64) -> SteamID {
|
||||
SteamID::from_steam64(id).unwrap()
|
||||
}
|
||||
|
||||
async fn setup() {
|
||||
let _ = tracing_subscriber::fmt()
|
||||
.with_env_filter(EnvFilter::from_default_env())
|
||||
|
|
@ -141,7 +145,7 @@ async fn test_list_demos() {
|
|||
assert!(demos[0].uploader.user().is_none());
|
||||
assert_eq!(
|
||||
demos[0].uploader.resolve(&client).await.unwrap().steam_id,
|
||||
SteamID::from(76561198024494988)
|
||||
id(76561198024494988)
|
||||
);
|
||||
|
||||
assert_eq!(demos[0].player_count, 12);
|
||||
|
|
@ -160,11 +164,11 @@ async fn test_get_demo() {
|
|||
assert!(demo.uploader.user().is_some());
|
||||
assert_eq!(
|
||||
demo.uploader.user().unwrap().steam_id,
|
||||
SteamID::from(76561198024494988)
|
||||
id(76561198024494988)
|
||||
);
|
||||
assert_eq!(
|
||||
demo.uploader.resolve(&client).await.unwrap().steam_id,
|
||||
SteamID::from(76561198024494988)
|
||||
id(76561198024494988)
|
||||
);
|
||||
|
||||
let mut players = demo.players.unwrap();
|
||||
|
|
@ -175,7 +179,7 @@ async fn test_get_demo() {
|
|||
.cmp(&b.user.steam_id.account_id())
|
||||
});
|
||||
|
||||
assert_eq!(players[0].user.steam_id, SteamID::from(76561198010628997));
|
||||
assert_eq!(players[0].user.steam_id, id(76561198010628997));
|
||||
assert_eq!(players[0].user.name, "freak u ___");
|
||||
}
|
||||
|
||||
|
|
@ -297,7 +301,7 @@ async fn test_list_upload() {
|
|||
|
||||
let demos = client
|
||||
.list_uploads(
|
||||
SteamID::from(76561198024494987),
|
||||
id(76561198024494987),
|
||||
ListParams::default().with_order(ListOrder::Ascending),
|
||||
1,
|
||||
)
|
||||
|
|
@ -307,7 +311,7 @@ async fn test_list_upload() {
|
|||
|
||||
let demos = client
|
||||
.list_uploads(
|
||||
SteamID::from(76561198024494988),
|
||||
id(76561198024494988),
|
||||
ListParams::default().with_order(ListOrder::Ascending),
|
||||
1,
|
||||
)
|
||||
|
|
@ -350,7 +354,7 @@ async fn test_search_players() {
|
|||
|
||||
let user = client.search_users("freak").await.unwrap();
|
||||
assert_eq!(user.len(), 1);
|
||||
assert_eq!(user[0].steam_id, SteamID::from(76561198010628997));
|
||||
assert_eq!(user[0].steam_id, id(76561198010628997));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue