rte_tempo/
lib.rs

1#[derive(Debug, serde::Deserialize)]
2pub enum TempoDayType {
3    Bleu,
4    Blanc,
5    Rouge,
6}
7
8#[derive(Debug, serde::Deserialize)]
9pub struct TempoDay {
10    #[serde(rename = "dateJour")]
11    pub day: String,
12    #[serde(rename = "libCouleur")]
13    pub day_type: TempoDayType,
14}
15
16#[derive(Debug, serde::Deserialize)]
17pub struct TempoPeriodStats {
18    #[serde(rename = "periode")]
19    pub period: String,
20
21    /// Is the year a leap year? if so,
22    /// it contains an extra blue day
23    #[serde(rename = "bissextile")]
24    pub is_bissextile: bool,
25
26    #[serde(rename = "joursBleusRestants")]
27    pub remaining_bleu_days: u16,
28    #[serde(rename = "joursBlancsRestants")]
29    pub remaining_blanc_days: u16,
30    #[serde(rename = "joursRougesRestants")]
31    pub remaining_rouge_days: u16,
32
33    #[serde(rename = "joursBleusConsommes")]
34    pub done_bleu_days: u16,
35    #[serde(rename = "joursBlancsConsommes")]
36    pub done_blanc_days: u16,
37    #[serde(rename = "joursRougesConsommes")]
38    pub done_rouge_days: u16,
39}
40
41pub struct TempoApiClient {
42    base_url: String,
43}
44
45const DEFAULT_BASE_URL: &str = "https://www.api-couleur-tempo.fr/api";
46
47impl TempoApiClient {
48    pub fn default() -> Self {
49        Self {
50            base_url: DEFAULT_BASE_URL.to_string(),
51        }
52    }
53    pub fn new(base_url: &str) -> Self {
54        Self {
55            base_url: base_url.to_string(),
56        }
57    }
58
59    async fn get(&self, endpoint: &str) -> Result<String, Box<dyn std::error::Error>> {
60        let url = format!("{}/{}", self.base_url, endpoint);
61        println!("Fetching URL: {}", url);
62        let client = reqwest::Client::new();
63        let resp = client
64            .get(&url)
65            .header("Content-Type", "application/json")
66            .send()
67            .await?
68            .text()
69            .await?;
70        Ok(resp)
71    }
72
73    pub async fn get_tempo_today(&self) -> Result<TempoDay, Box<dyn std::error::Error>> {
74        let resp = self.get("jourTempo/today").await?;
75        let tempo_today = serde_json::from_str(&resp)?;
76
77        Ok(tempo_today)
78    }
79
80    pub async fn get_tempo_tomorrow(&self) -> Result<TempoDay, Box<dyn std::error::Error>> {
81        let resp = self.get("jourTempo/tomorrow").await?;
82        let tempo_tomorrow = serde_json::from_str(&resp)?;
83
84        Ok(tempo_tomorrow)
85    }
86
87    pub async fn get_period_stats(&self) -> Result<TempoPeriodStats, Box<dyn std::error::Error>> {
88        let resp = self.get("stats").await?;
89        println!("Response: {}", resp);
90        let tempo_stats = serde_json::from_str(&resp)?;
91        Ok(tempo_stats)
92    }
93}