hive_client/client/api/
weather.rs1use crate::client::api::{ApiError, HiveApi};
2use crate::client::authentication::Tokens;
3use crate::helper::url::{Url, get_base_url};
4use serde::{Deserialize, Serialize};
5use std::fmt;
6use std::fmt::Debug;
7
8#[derive(Serialize, Deserialize, Debug)]
9#[serde(tag = "unit")]
10pub enum Temperature {
12 #[serde(rename = "C")]
13 #[allow(missing_docs)]
14 Celsius { value: f32 },
15}
16
17impl fmt::Display for Temperature {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 match self {
20 Self::Celsius { value } => write!(f, "{value}°C"),
21 }
22 }
23}
24
25#[derive(Serialize, Deserialize, Debug)]
26#[allow(missing_docs)]
27pub struct WeatherData {
28 #[serde(rename = "icon")]
30 pub r#type: String,
31
32 pub temperature: Temperature,
34
35 pub description: String,
37}
38
39#[derive(Serialize, Deserialize, Debug)]
41pub struct Weather {
42 #[allow(missing_docs)]
43 #[serde(rename = "weather")]
44 pub data: WeatherData,
45}
46
47impl HiveApi {
48 pub(crate) async fn get_weather(
49 &self,
50 tokens: &Tokens,
51 postcode: &str,
52 ) -> Result<Weather, ApiError> {
53 let response = self
54 .client
55 .get(get_base_url(&Url::Weather))
56 .query(&[("postcode", postcode.replace(' ', ""))])
57 .header("Authorization", &tokens.id_token)
58 .send()
59 .await;
60
61 Ok(response?.json::<Weather>().await?)
62 }
63}