add set url endpoint

This commit is contained in:
Robin Appelman 2020-07-25 14:53:59 +02:00
commit 7776c0ac81
3 changed files with 63 additions and 4 deletions

View file

@ -3,8 +3,9 @@ name = "demostf-client"
version = "0.1.0" version = "0.1.0"
authors = ["Robin Appelman <robin@icewind.nl>"] authors = ["Robin Appelman <robin@icewind.nl>"]
edition = "2018" edition = "2018"
description = "Api client for demos.tf"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html repository = "https://github.com/demostf/api-client"
readme = "README.md"
[dependencies] [dependencies]
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

21
README.md Normal file
View 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(())
}
```

View file

@ -1,7 +1,7 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
use std::fmt; use std::fmt;
use reqwest::{Client, IntoUrl, Url}; use reqwest::{Client, IntoUrl, Url, StatusCode};
use thiserror::Error; use thiserror::Error;
use steamid_ng::SteamID; use steamid_ng::SteamID;
use std::borrow::Cow; use std::borrow::Cow;
@ -14,6 +14,12 @@ pub enum Error {
Request(#[from] reqwest::Error), Request(#[from] reqwest::Error),
#[error("Invalid page requested")] #[error("Invalid page requested")]
InvalidPage, InvalidPage,
#[error("Invalid api key")]
InvalidApiKey,
#[error("Hash mismatch")]
HashMisMatch,
#[error("Unknown server error")]
ServerError(u16),
} }
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
@ -398,11 +404,34 @@ impl ApiClient {
.json() .json()
.await?) .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)] #[cfg(test)]
mod tests { mod tests {
use crate::{ApiClient, ListParams, ListOrder}; use crate::{ApiClient, ListParams, ListOrder, Error};
use steamid_ng::SteamID; use steamid_ng::SteamID;
#[tokio::test] #[tokio::test]
@ -453,4 +482,12 @@ mod tests {
assert_eq!(demos[0].players.len(), 0); assert_eq!(demos[0].players.len(), 0);
assert_eq!(demos[0].get_players(&client).await.unwrap().len(), 12); 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));
}
} }