mirror of
https://codeberg.org/demostf/api-client.git
synced 2026-06-03 08:34:15 +02:00
user search
This commit is contained in:
parent
9beb6c3627
commit
a90a20c622
2 changed files with 68 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::{ChatMessage, Demo, Error, ListParams, User};
|
use crate::{ChatMessage, Demo, Error, ListParams, User};
|
||||||
use reqwest::{multipart, Client, IntoUrl, Response, StatusCode, Url};
|
use reqwest::{multipart, Client, IntoUrl, Response, StatusCode, Url};
|
||||||
|
use std::borrow::Borrow;
|
||||||
use std::fmt::{self, Debug, Formatter};
|
use std::fmt::{self, Debug, Formatter};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
@ -92,6 +93,22 @@ impl ApiClient {
|
||||||
.map_err(|_| Error::InvalidBaseUrl)
|
.map_err(|_| Error::InvalidBaseUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn url_with_params<P, I, K, V>(&self, path: P, iter: I) -> Result<Url, Error>
|
||||||
|
where
|
||||||
|
P: AsRef<str>,
|
||||||
|
I: IntoIterator,
|
||||||
|
I::Item: Borrow<(K, V)>,
|
||||||
|
K: AsRef<str>,
|
||||||
|
V: AsRef<str>,
|
||||||
|
{
|
||||||
|
let mut url = self
|
||||||
|
.base_url
|
||||||
|
.join(path.as_ref())
|
||||||
|
.map_err(|_| Error::InvalidBaseUrl)?;
|
||||||
|
url.query_pairs_mut().extend_pairs(iter);
|
||||||
|
Ok(url)
|
||||||
|
}
|
||||||
|
|
||||||
/// List demos with the provided options
|
/// List demos with the provided options
|
||||||
///
|
///
|
||||||
/// note that the pages start counting at 1
|
/// note that the pages start counting at 1
|
||||||
|
|
@ -243,6 +260,36 @@ impl ApiClient {
|
||||||
Ok(response.error_for_status()?.json().await?)
|
Ok(response.error_for_status()?.json().await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Search for players by name
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use demostf_client::ApiClient;
|
||||||
|
/// #
|
||||||
|
/// # #[tokio::main]
|
||||||
|
/// # async fn main() -> Result<(), demostf_client::Error> {
|
||||||
|
/// let client = ApiClient::default();
|
||||||
|
/// #
|
||||||
|
/// let users = client.search_users("icewind").await?;
|
||||||
|
///
|
||||||
|
/// for user in users {
|
||||||
|
/// println!("{} ({})", user.name, user.steam_id.steam3());
|
||||||
|
/// }
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
#[instrument]
|
||||||
|
pub async fn search_users(&self, name: &str) -> Result<Vec<User>, Error> {
|
||||||
|
let response = self
|
||||||
|
.client
|
||||||
|
.get(self.url_with_params("/users/search", [("query", name)])?)
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(response.error_for_status()?.json().await?)
|
||||||
|
}
|
||||||
|
|
||||||
/// List demos with the provided options
|
/// List demos with the provided options
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,18 @@ async fn setup() {
|
||||||
let client = ApiClient::with_base_url(&api_root).unwrap();
|
let client = ApiClient::with_base_url(&api_root).unwrap();
|
||||||
|
|
||||||
upload(&client, "./tests/data/gully.dem", "test.dem", "R", "B").await;
|
upload(&client, "./tests/data/gully.dem", "test.dem", "R", "B").await;
|
||||||
|
|
||||||
|
let mut transaction = pool.begin().await.unwrap();
|
||||||
|
|
||||||
|
let views = ["map_list", "name_list", "users_named"];
|
||||||
|
for view in views {
|
||||||
|
sqlx::query(&format!("REFRESH MATERIALIZED VIEW {}", view))
|
||||||
|
.execute(&mut transaction)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction.commit().await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn test_client() -> ApiClient {
|
async fn test_client() -> ApiClient {
|
||||||
|
|
@ -328,6 +340,15 @@ async fn test_list_players() {
|
||||||
assert_eq!(demos.len(), 0);
|
assert_eq!(demos.len(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_search_players() {
|
||||||
|
let client = test_client().await;
|
||||||
|
|
||||||
|
let user = client.search_users("freak").await.unwrap();
|
||||||
|
assert_eq!(user.len(), 1);
|
||||||
|
assert_eq!(user[0].steam_id, SteamID::from(76561198010628997));
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_download_demo() {
|
async fn test_download_demo() {
|
||||||
let client = test_client().await;
|
let client = test_client().await;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue