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]
|
[package]
|
||||||
name = "demostf-client"
|
name = "demostf-client"
|
||||||
version = "0.4.7"
|
version = "0.5.0"
|
||||||
authors = ["Robin Appelman <robin@icewind.nl>"]
|
authors = ["Robin Appelman <robin@icewind.nl>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "Api client for demos.tf"
|
description = "Api client for demos.tf"
|
||||||
|
|
@ -14,15 +14,17 @@ rust-version = "1.88.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = { version = "1.0.228", features = ["derive"] }
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
time = { version = "0.3.44", features = ["serde"] }
|
time = { version = "0.3.47", features = ["serde"] }
|
||||||
reqwest = { version = "0.12.15", default-features = false, features = [
|
reqwest = { version = "0.13.3", default-features = false, features = [
|
||||||
"json",
|
"json",
|
||||||
"multipart",
|
"multipart",
|
||||||
"stream",
|
"stream",
|
||||||
|
"form",
|
||||||
|
"query"
|
||||||
] }
|
] }
|
||||||
thiserror = "2.0.18"
|
thiserror = "2.0.18"
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
steamid-ng = "1.0.0"
|
steamid-ng = "3.0.0"
|
||||||
bytes = "1.11.1"
|
bytes = "1.11.1"
|
||||||
futures-util = "0.3.32"
|
futures-util = "0.3.32"
|
||||||
tracing = "0.1.44"
|
tracing = "0.1.44"
|
||||||
|
|
@ -33,8 +35,3 @@ md5 = "0.8.0"
|
||||||
tokio = { version = "1.52.3", features = ["macros"] }
|
tokio = { version = "1.52.3", features = ["macros"] }
|
||||||
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio-rustls"] }
|
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio-rustls"] }
|
||||||
tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
|
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 futures_util::{Stream, StreamExt};
|
||||||
use md5::Context;
|
use md5::Context;
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::fmt::{self, Debug, Display, Formatter};
|
use std::fmt::{self, Debug, Display, Formatter};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
use std::str::FromStr;
|
||||||
pub use steamid_ng::SteamID;
|
pub use steamid_ng::SteamID;
|
||||||
|
use steamid_ng::{Instance, InstanceFlags, InstanceType};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
use tinyvec::TinyVec;
|
use tinyvec::TinyVec;
|
||||||
|
|
@ -180,7 +182,7 @@ impl UserRef {
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
#[serde(rename = "steamid")]
|
#[serde(rename = "steamid", deserialize_with = "deserialize_steamid")]
|
||||||
pub steam_id: SteamID,
|
pub steam_id: SteamID,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
@ -204,7 +206,7 @@ pub struct Player {
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
struct NestedPlayerUser {
|
struct NestedPlayerUser {
|
||||||
user_id: u32,
|
user_id: u32,
|
||||||
#[serde(rename = "steamid")]
|
#[serde(rename = "steamid", deserialize_with = "deserialize_steamid")]
|
||||||
steam_id: SteamID,
|
steam_id: SteamID,
|
||||||
name: String,
|
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
|
/// Player team, red or blue
|
||||||
#[derive(Clone, Copy, Debug, Deserialize, PartialOrd, PartialEq)]
|
#[derive(Clone, Copy, Debug, Deserialize, PartialOrd, PartialEq)]
|
||||||
#[serde(rename_all = "lowercase")]
|
#[serde(rename_all = "lowercase")]
|
||||||
|
|
@ -333,24 +343,30 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Debug, Default)]
|
||||||
struct PlayerList(TinyVec<[SteamID; 2]>);
|
struct PlayerList(TinyVec<[Option<SteamID>; 2]>);
|
||||||
|
|
||||||
impl PlayerList {
|
impl PlayerList {
|
||||||
fn new<T: Into<SteamID>, I: IntoIterator<Item = T>>(players: I) -> Self {
|
fn new<T: IntoSteamId, I: IntoIterator<Item = T>>(players: I) -> Self {
|
||||||
PlayerList(players.into_iter().map(Into::into).collect())
|
PlayerList(
|
||||||
|
players
|
||||||
|
.into_iter()
|
||||||
|
.map(IntoSteamId::into_steam_id)
|
||||||
|
.map(Some)
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for PlayerList {
|
impl Display for PlayerList {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for steam_id in &self.0 {
|
for steam_id in self.0.iter().flatten() {
|
||||||
if first {
|
if first {
|
||||||
first = false;
|
first = false;
|
||||||
write!(f, "{}", u64::from(*steam_id))?;
|
write!(f, "{}", steam_id.steam64())?;
|
||||||
} else {
|
} else {
|
||||||
write!(f, ",{}", u64::from(*steam_id))?;
|
write!(f, ",{}", steam_id.steam64())?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -369,20 +385,50 @@ impl Serialize for PlayerList {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_player_list() {
|
fn test_serialize_player_list() {
|
||||||
|
fn id(id: u64) -> SteamID {
|
||||||
|
SteamID::from_steam64(id).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"76561198024494988",
|
"76561198024494988",
|
||||||
PlayerList::new([76561198024494988]).to_string()
|
PlayerList::new([id(76561198024494988)]).to_string()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"76561198024494988,76561197963701107",
|
"76561198024494988,76561197963701107",
|
||||||
PlayerList::new([76561198024494988, 76561197963701107]).to_string()
|
PlayerList::new([id(76561198024494988), id(76561197963701107)]).to_string()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"76561198024494988,76561197963701107,76561197963701106",
|
"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 {
|
impl ListParams {
|
||||||
/// Specify the backend name to filter demos with
|
/// Specify the backend name to filter demos with
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
|
@ -404,7 +450,7 @@ impl ListParams {
|
||||||
|
|
||||||
/// Specify the player steam ids to filter demos with
|
/// Specify the player steam ids to filter demos with
|
||||||
#[must_use]
|
#[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 {
|
ListParams {
|
||||||
players: PlayerList::new(players),
|
players: PlayerList::new(players),
|
||||||
..self
|
..self
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,10 @@ fn test_demo_path() -> String {
|
||||||
std::env::var("TEST_DEMO").unwrap_or_else(|_| "./tests/data/gully.dem".to_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() {
|
async fn setup() {
|
||||||
let _ = tracing_subscriber::fmt()
|
let _ = tracing_subscriber::fmt()
|
||||||
.with_env_filter(EnvFilter::from_default_env())
|
.with_env_filter(EnvFilter::from_default_env())
|
||||||
|
|
@ -141,7 +145,7 @@ async fn test_list_demos() {
|
||||||
assert!(demos[0].uploader.user().is_none());
|
assert!(demos[0].uploader.user().is_none());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
demos[0].uploader.resolve(&client).await.unwrap().steam_id,
|
demos[0].uploader.resolve(&client).await.unwrap().steam_id,
|
||||||
SteamID::from(76561198024494988)
|
id(76561198024494988)
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(demos[0].player_count, 12);
|
assert_eq!(demos[0].player_count, 12);
|
||||||
|
|
@ -160,11 +164,11 @@ async fn test_get_demo() {
|
||||||
assert!(demo.uploader.user().is_some());
|
assert!(demo.uploader.user().is_some());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
demo.uploader.user().unwrap().steam_id,
|
demo.uploader.user().unwrap().steam_id,
|
||||||
SteamID::from(76561198024494988)
|
id(76561198024494988)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
demo.uploader.resolve(&client).await.unwrap().steam_id,
|
demo.uploader.resolve(&client).await.unwrap().steam_id,
|
||||||
SteamID::from(76561198024494988)
|
id(76561198024494988)
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut players = demo.players.unwrap();
|
let mut players = demo.players.unwrap();
|
||||||
|
|
@ -175,7 +179,7 @@ async fn test_get_demo() {
|
||||||
.cmp(&b.user.steam_id.account_id())
|
.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 ___");
|
assert_eq!(players[0].user.name, "freak u ___");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -297,7 +301,7 @@ async fn test_list_upload() {
|
||||||
|
|
||||||
let demos = client
|
let demos = client
|
||||||
.list_uploads(
|
.list_uploads(
|
||||||
SteamID::from(76561198024494987),
|
id(76561198024494987),
|
||||||
ListParams::default().with_order(ListOrder::Ascending),
|
ListParams::default().with_order(ListOrder::Ascending),
|
||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
|
|
@ -307,7 +311,7 @@ async fn test_list_upload() {
|
||||||
|
|
||||||
let demos = client
|
let demos = client
|
||||||
.list_uploads(
|
.list_uploads(
|
||||||
SteamID::from(76561198024494988),
|
id(76561198024494988),
|
||||||
ListParams::default().with_order(ListOrder::Ascending),
|
ListParams::default().with_order(ListOrder::Ascending),
|
||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
|
|
@ -350,7 +354,7 @@ async fn test_search_players() {
|
||||||
|
|
||||||
let user = client.search_users("freak").await.unwrap();
|
let user = client.search_users("freak").await.unwrap();
|
||||||
assert_eq!(user.len(), 1);
|
assert_eq!(user.len(), 1);
|
||||||
assert_eq!(user[0].steam_id, SteamID::from(76561198010628997));
|
assert_eq!(user[0].steam_id, id(76561198010628997));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue