ddapi-rs
A small async Rust library for working with the DDNet and DDStats public APIs.
Features
ddnet (default) - DDNet API (ddnet.org)
ddstats - DDStats API (ddstats.tw)
cache - in-memory cache for responses (uses moka)
full - enables ddnet, ddstats, cache
Installation
Default (DDNet only):
cargo add ddapi-rs
Enable DDStats too:
cargo add ddapi-rs -F ddstats
Enable caching:
cargo add ddapi-rs -F cache
Everything:
cargo add ddapi-rs -F full
Usage
The crate is async and uses tokio.
DDNet example (enabled by default):
use ddapi_rs::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
let api = DDApi::new();
let player = api.player("nameless tee").await?;
println!("{}: {}", player.player, player.points.points.unwrap_or(0));
Ok(())
}
DDStats example (requires -F ddstats):
use ddapi_rs::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
let api = DDApi::new();
let profile = api.profile("ByFox").await?;
println!("{} ({})", profile.name, profile.clan.unwrap_or_default());
Ok(())
}
Optional: only use one API
If you do not want a combined client, you can use DDnetClient / DDstatsClient.
use ddapi_rs::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
let ddnet = DDnetClient::new();
let p = ddnet.player("nameless tee").await?;
println!("{}: {}", p.player, p.points.points.unwrap_or(0));
Ok(())
}
Caching (feature cache)
use ddapi_rs::prelude::*;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<()> {
let mut api = DDApi::new();
api.set_cache(1000, Duration::from_secs(60 * 5));
let _ = api.status().await?;
Ok(())
}
Custom reqwest client
use ddapi_rs::prelude::*;
use reqwest::Client;
fn main() {
let client = Client::builder()
.timeout(std::time::Duration::from_secs(10))
.build()
.unwrap();
let _api = DDApi::new_with_client(client);
}