pub struct DDApi { /* private fields */ }Implementations§
Source§impl DDApi
impl DDApi
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new DDApi instance with default settings
§Examples
use ddapi_rs::prelude::*;
let api = DDApi::new();Sourcepub fn new_with_client(client: Client) -> Self
pub fn new_with_client(client: Client) -> Self
Creates a new DDApi instance with a custom HTTP client
This allows you to configure your own client with custom timeouts, headers, or other settings.
§Arguments
client- A pre-configuredreqwest::Clientinstance
§Examples
use ddapi_rs::prelude::*;
use reqwest::Client;
let client = Client::builder()
.timeout(std::time::Duration::from_secs(10))
.build()
.unwrap();
let api = DDApi::new_with_client(client);Sourcepub fn set_cache(&mut self, capacity: u64, time_to_live: Duration)
pub fn set_cache(&mut self, capacity: u64, time_to_live: Duration)
Configures caching for API responses
When the cache feature is enabled, this method allows you to set up
an in-memory cache to reduce API calls and improve performance.
§Arguments
capacity- Maximum number of entries to store in the cachetime_to_live- Time in seconds before cached entries expire
§Examples
use ddapi_rs::prelude::*;
use std::time::Duration;
let mut api = DDApi::new();
api.set_cache(1000, Duration::from_secs(60 * 5)); // Cache 1000 items for 5 minutesSourcepub async fn generator<T>(&self, url: &str) -> Result<T>
pub async fn generator<T>(&self, url: &str) -> Result<T>
Executes an API request and deserializes the JSON response
This method handles API requests and automatically deserializes the JSON response
into the specified type. The caching behavior is determined by the cache feature flag.
§Type Parameters
T- The type to deserialize the response into. Must implementDeserializeOwned + Send + Sync + 'static
§Arguments
url- The API endpoint URL to request
§Returns
Result<T> containing the deserialized data on success, or an error on failure
§Errors
Returns an error if the request fails, the response is empty or has a
non-success status, or the body cannot be deserialized into T.
Sourcepub async fn generator_no_cache<T>(&self, url: &str) -> Result<T>where
T: DeserializeOwned,
pub async fn generator_no_cache<T>(&self, url: &str) -> Result<T>where
T: DeserializeOwned,
Executes an API request without caching
Always fetches fresh data from the API, bypassing any cache.
§Type Parameters
T- The type to deserialize the response into
§Arguments
url- The API endpoint URL to request
§Returns
Returns Result<T> with freshly fetched deserialized data
§Errors
Returns an error if the request fails, the response is empty or has a
non-success status, or the body cannot be deserialized into T.
Trait Implementations§
Source§impl DDnetApi for DDApi
impl DDnetApi for DDApi
Source§async fn custom_master(&self, master: MasterServer) -> Result<Master>
async fn custom_master(&self, master: MasterServer) -> Result<Master>
Fetches server list from a specific master server
Allows selecting which master server to query. DDNet has multiple
master servers for redundancy and load distribution.
§Arguments
master- The master server to query (MasterServer::One,MasterServer::Two, etc.)
§Examples
use ddapi_rs::prelude::*;
use ddapi_rs::prelude::ddnet::*;
let api = DDApi::new();
// Use secondary master server as fallback
let master = api.custom_master(MasterServer::Two).await?;