TopStats.gg

Rust

Community maintained Rust client for the topstats.gg API.

A type-safe, ergonomic Rust client for the Topstats.gg API with async and blocking support.

Add the dependency to your Cargo.toml:

[dependencies]
topstats = "0.1"
tokio = { version = "1", features = ["full"] }

Initialize the Client

use topstats::Client;

let client = Client::new("YOUR_TOKEN")?;

Make Your First Request

// Get information about a bot
let bot = client.get_bot("583807014896140293").await?;
println!("{} has {:?} servers", bot.name, bot.server_count);

Usage Examples

use topstats::Client;

let client = Client::new("YOUR_TOKEN")?;

// Get detailed bot information
let bot = client.get_bot("583807014896140293").await?;
println!("{} has {:?} servers", bot.name, bot.server_count);

API Reference

Methods

async fn get_bot(&self, bot_id: &str) -> Result<Bot>

// Example
let bot = client.get_bot("583807014896140293").await?;

Response Types

pub struct Bot {
    pub id: String,
    pub name: String,
    pub server_count: Option<i64>,
    pub monthly_votes: i64,
    pub total_votes: i64,
    pub short_description: String,
    pub tags: Vec<String>,
    pub owners: Vec<String>,
    pub deleted: bool,
    pub timestamp: DateTime<Utc>,
    pub percentage_changes: Option<PercentageChanges>,
    // ... and more
}

Feature Flags

The SDK uses Cargo feature flags to control async/blocking mode and the HTTP backend.

FeatureDefaultDescription
asyncYesEnable async mode
blockingNoEnable blocking (synchronous) mode
reqwest-clientYesUse reqwest as the HTTP backend
ureq-clientNoUse ureq as the HTTP backend (for blocking mode)
rustls-tlsYesUse rustls for TLS
native-tlsNoUse the platform's native TLS
tracingNoEnable tracing crate integration

The async and blocking features are mutually exclusive. Enabling both will result in a compile error.

Error Handling

use topstats::Error;

match client.get_bot("invalid-id").await {
    Ok(bot) => println!("Found: {}", bot.name),
    Err(Error::NotFound { message }) => {
        println!("Bot not found: {message}");
    }
    Err(Error::RateLimited { retry_after, .. }) => {
        println!("Rate limited, retry after {retry_after}s");
    }
    Err(e) => eprintln!("Error: {e}"),
}

Rate Limits

The API implements rate limiting to ensure fair usage. The Rust SDK handles rate limits automatically by retrying requests when a 429 response is received. You can configure retry behavior using the ClientBuilder:

let client = Client::builder()
    .token("YOUR_TOKEN")
    .auto_retry(true)          // enabled by default
    .max_retries(3)            // default: 3
    .max_delay_threshold(10.0) // max seconds to wait before retrying
    .build()?;

For detailed information about rate limits, see the rate limit documentation.

GitHubtop-stats/rust-sdk

10

On this page