1use anyhow::{Context, Result};
2use reqwest::Client;
3use serde::de::DeserializeOwned;
4
5#[derive(Debug, Clone, Default)]
6pub struct DDApi {
7 client: Client,
8}
9
10impl DDApi {
11 pub fn new() -> Self {
12 DDApi {
13 client: Client::new(),
14 }
15 }
16
17 pub fn new_with_client(client: Client) -> Self {
18 DDApi { client }
19 }
20
21 async fn send_request(&self, uri: &str) -> Result<String> {
22 let response = self
23 .client
24 .get(uri)
25 .send()
26 .await
27 .context("Failed to send request")?
28 .error_for_status()
29 .context("Server returned error status")?;
30
31 let text = response
32 .text()
33 .await
34 .context("Failed to read response body")?;
35
36 if text.is_empty() {
37 anyhow::bail!("API returned empty response");
38 }
39
40 Ok(text)
41 }
42
43 async fn _generator<T>(&self, uri: &str) -> Result<T>
44 where
45 T: DeserializeOwned,
46 {
47 let response_text = self.send_request(uri).await?;
48
49 serde_json::from_str(&response_text).context("Failed to parse API response")
50 }
51}
52
53#[cfg(feature = "ddnet")]
54pub mod ddnet;
55
56#[cfg(feature = "ddstats")]
57pub mod ddstats;