Skip to main content

hive_client/client/wrapper/
weather.rs

1use crate::weather::Weather;
2use crate::{ApiError, Client};
3
4impl Client {
5    /// Get the current weather according to Hive, for a given postcode.
6    ///
7    /// # Examples
8    ///
9    /// ```no_run
10    /// use hive_client::authentication::{TrustedDevice, User};
11    /// use hive_client::{weather::WeatherData};
12    /// use hive_client::weather::Temperature::Celsius;
13    /// use hive_client::weather::Weather;
14    ///
15    /// # tokio_test::block_on(async {
16    /// let client = hive_client::Client::new("Home Automation");
17    ///
18    /// let trusted_device = Some(TrustedDevice::new(
19    ///     "device_password",
20    ///     "device_group_key",
21    ///     "device_key"
22    /// ));
23    ///
24    /// client.login(User::new("example@example.com", "example"), trusted_device)
25    ///     .await
26    ///     .expect("Login should succeed");
27    ///
28    /// let Weather { data: WeatherData { temperature, ..}} = client.get_weather("SW1A 1AA")
29    ///     .await
30    ///     .expect("Weather should be retrieved");
31    ///
32    /// println!("The current temperature is: {temperature}");
33    /// # })
34    /// ```
35    ///
36    /// # Errors
37    ///
38    /// Returns an error if the whether data could not be retrieved.
39    pub async fn get_weather(&self, postcode: &str) -> Result<Weather, ApiError> {
40        self.api
41            .get_weather(&*self.refresh_tokens_if_needed().await?, postcode)
42            .await
43    }
44}