#[derive(Debug, serde::Deserialize)]
pub enum TempoDayType {
Bleu,
Blanc,
Rouge,
}
#[derive(Debug, serde::Deserialize)]
pub struct TempoDay {
#[serde(rename = "dateJour")]
pub day: String,
#[serde(rename = "libCouleur")]
pub day_type: TempoDayType,
}
#[derive(Debug, serde::Deserialize)]
pub struct TempoPeriodStats {
#[serde(rename = "periode")]
pub period: String,
#[serde(rename = "bissextile")]
pub is_bissextile: bool,
#[serde(rename = "joursBleusRestants")]
pub remaining_bleu_days: u16,
#[serde(rename = "joursBlancsRestants")]
pub remaining_blanc_days: u16,
#[serde(rename = "joursRougesRestants")]
pub remaining_rouge_days: u16,
#[serde(rename = "joursBleusConsommes")]
pub done_bleu_days: u16,
#[serde(rename = "joursBlancsConsommes")]
pub done_blanc_days: u16,
#[serde(rename = "joursRougesConsommes")]
pub done_rouge_days: u16,
}
pub struct TempoApiClient {
base_url: String,
}
const DEFAULT_BASE_URL: &str = "https://www.api-couleur-tempo.fr/api";
impl TempoApiClient {
pub fn default() -> Self {
Self {
base_url: DEFAULT_BASE_URL.to_string(),
}
}
pub fn new(base_url: &str) -> Self {
Self {
base_url: base_url.to_string(),
}
}
async fn get(&self, endpoint: &str) -> Result<String, Box<dyn std::error::Error>> {
let url = format!("{}/{}", self.base_url, endpoint);
println!("Fetching URL: {}", url);
let client = reqwest::Client::new();
let resp = client
.get(&url)
.header("Content-Type", "application/json")
.send()
.await?
.text()
.await?;
Ok(resp)
}
pub async fn get_tempo_today(&self) -> Result<TempoDay, Box<dyn std::error::Error>> {
let resp = self.get("jourTempo/today").await?;
let tempo_today = serde_json::from_str(&resp)?;
Ok(tempo_today)
}
pub async fn get_tempo_tomorrow(&self) -> Result<TempoDay, Box<dyn std::error::Error>> {
let resp = self.get("jourTempo/tomorrow").await?;
let tempo_tomorrow = serde_json::from_str(&resp)?;
Ok(tempo_tomorrow)
}
pub async fn get_period_stats(&self) -> Result<TempoPeriodStats, Box<dyn std::error::Error>> {
let resp = self.get("stats").await?;
println!("Response: {}", resp);
let tempo_stats = serde_json::from_str(&resp)?;
Ok(tempo_stats)
}
}