mirror of
https://codeberg.org/demostf/api-client.git
synced 2026-06-04 00:54:11 +02:00
get_players
This commit is contained in:
parent
4145f1a58e
commit
3b4c2b9965
1 changed files with 24 additions and 2 deletions
26
src/lib.rs
26
src/lib.rs
|
|
@ -14,8 +14,6 @@ pub enum Error {
|
||||||
Request(#[from] reqwest::Error),
|
Request(#[from] reqwest::Error),
|
||||||
#[error("Invalid page requested")]
|
#[error("Invalid page requested")]
|
||||||
InvalidPage,
|
InvalidPage,
|
||||||
#[error("MD5 digest mismatch for downloaded demo, expected {expected:?}, received {got:?}")]
|
|
||||||
DigestMismatch { expected: [u8; 16], got: [u8; 16] },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
|
|
@ -41,9 +39,22 @@ pub struct Demo {
|
||||||
pub backend: String,
|
pub backend: String,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
/// Demos listed using `ApiClient::list` will not have any players set
|
||||||
pub players: Vec<Player>,
|
pub players: Vec<Player>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Demo {
|
||||||
|
/// Return either the stored players info or get the players from the api
|
||||||
|
pub async fn get_players<'a>(&'a self, client: &ApiClient) -> Result<Cow<'a, [Player]>, Error> {
|
||||||
|
if self.players.len() > 0 {
|
||||||
|
Ok(Cow::Borrowed(self.players.as_slice()))
|
||||||
|
} else {
|
||||||
|
let demo = client.get(self.id).await?;
|
||||||
|
Ok(Cow::Owned(demo.players))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Reference to a user, either contains the full user information or only the user id
|
/// Reference to a user, either contains the full user information or only the user id
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
|
|
@ -214,6 +225,7 @@ impl ListParams {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct ApiClient {
|
pub struct ApiClient {
|
||||||
client: Client,
|
client: Client,
|
||||||
base_url: Url,
|
base_url: Url,
|
||||||
|
|
@ -431,4 +443,14 @@ mod tests {
|
||||||
assert_eq!(chat[0].time, 5);
|
assert_eq!(chat[0].time, 5);
|
||||||
assert_eq!(chat[0].message, "gl hf :)))))");
|
assert_eq!(chat[0].message, "gl hf :)))))");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_get_players() {
|
||||||
|
let client = ApiClient::default();
|
||||||
|
|
||||||
|
let demos = client.list(ListParams::default().with_order(ListOrder::Ascending), 1).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(demos[0].players.len(), 0);
|
||||||
|
assert_eq!(demos[0].get_players(&client).await.unwrap().len(), 12);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue