use crate::client::api::{ApiError, HiveApi};
use crate::client::authentication::Tokens;
use crate::helper::url::{Url, get_base_url};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::Debug;
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "unit")]
pub enum Temperature {
#[serde(rename = "C")]
#[allow(missing_docs)]
Celsius { value: f32 },
}
impl fmt::Display for Temperature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Celsius { value } => write!(f, "{value}°C"),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
#[allow(missing_docs)]
pub struct WeatherData {
#[serde(rename = "icon")]
pub r#type: String,
pub temperature: Temperature,
pub description: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Weather {
#[allow(missing_docs)]
#[serde(rename = "weather")]
pub data: WeatherData,
}
impl HiveApi {
pub(crate) async fn get_weather(
&self,
tokens: &Tokens,
postcode: &str,
) -> Result<Weather, ApiError> {
let response = self
.client
.get(get_base_url(&Url::Weather))
.query(&[("postcode", postcode.replace(' ', ""))])
.header("Authorization", &tokens.id_token)
.send()
.await;
Ok(response?.json::<Weather>().await?)
}
}