mirror of
https://codeberg.org/demostf/api-client.git
synced 2026-06-03 16:44:09 +02:00
add set url endpoint
This commit is contained in:
parent
3b4c2b9965
commit
7776c0ac81
3 changed files with 63 additions and 4 deletions
|
|
@ -3,8 +3,9 @@ name = "demostf-client"
|
|||
version = "0.1.0"
|
||||
authors = ["Robin Appelman <robin@icewind.nl>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
description = "Api client for demos.tf"
|
||||
repository = "https://github.com/demostf/api-client"
|
||||
readme = "README.md"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
|
|
|||
21
README.md
Normal file
21
README.md
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# api-client
|
||||
|
||||
Rust api client for demos.tf
|
||||
|
||||
## Example
|
||||
|
||||
```rust
|
||||
use demostf_client::{ListOrder, ListParams, ApiClient};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), demostf_client::Error> {
|
||||
let client = ApiClient::new();
|
||||
|
||||
let demos = client.list(ListParams::default().with_order(ListOrder::Ascending), 1).await?;
|
||||
|
||||
for demo in demos {
|
||||
println!("{}: {}", demo.id, demo.name);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
41
src/lib.rs
41
src/lib.rs
|
|
@ -1,7 +1,7 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use std::fmt;
|
||||
use reqwest::{Client, IntoUrl, Url};
|
||||
use reqwest::{Client, IntoUrl, Url, StatusCode};
|
||||
use thiserror::Error;
|
||||
use steamid_ng::SteamID;
|
||||
use std::borrow::Cow;
|
||||
|
|
@ -14,6 +14,12 @@ pub enum Error {
|
|||
Request(#[from] reqwest::Error),
|
||||
#[error("Invalid page requested")]
|
||||
InvalidPage,
|
||||
#[error("Invalid api key")]
|
||||
InvalidApiKey,
|
||||
#[error("Hash mismatch")]
|
||||
HashMisMatch,
|
||||
#[error("Unknown server error")]
|
||||
ServerError(u16),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
|
|
@ -398,11 +404,34 @@ impl ApiClient {
|
|||
.json()
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn set_url(&self, demo_id: u32, backend: &str, path: &str, url: &str, hash: &str, key: &str) -> Result<(), Error> {
|
||||
let mut api_url = self.base_url.clone();
|
||||
api_url.set_path(&format!("/demos/{}/url", demo_id));
|
||||
|
||||
let respose = self.client.post(api_url)
|
||||
.form(&[
|
||||
("hash", hash),
|
||||
("backend", backend),
|
||||
("url", url),
|
||||
("path", path),
|
||||
("key", key)
|
||||
])
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
match respose.status() {
|
||||
StatusCode::UNAUTHORIZED => Err(Error::InvalidApiKey),
|
||||
StatusCode::PRECONDITION_FAILED => Err(Error::HashMisMatch),
|
||||
_ if respose.status().is_server_error() => Err(Error::ServerError(respose.status().as_u16())),
|
||||
_ => Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{ApiClient, ListParams, ListOrder};
|
||||
use crate::{ApiClient, ListParams, ListOrder, Error};
|
||||
use steamid_ng::SteamID;
|
||||
|
||||
#[tokio::test]
|
||||
|
|
@ -453,4 +482,12 @@ mod tests {
|
|||
assert_eq!(demos[0].players.len(), 0);
|
||||
assert_eq!(demos[0].get_players(&client).await.unwrap().len(), 12);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_set_url_invalid_key() {
|
||||
let client = ApiClient::default();
|
||||
|
||||
let res = client.set_url(9, "test", "test", "http://example.com/test", "dummy", "wrong").await;
|
||||
assert!(matches!(res.unwrap_err(), Error::InvalidApiKey));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue