1use crate::api::locations::get_locations;
2use crate::api::login::{login, LoggedIn, LoginError};
3use crate::api::zones::set_zone_temperature;
4use crate::api::USER_AGENT;
5use reqwest::Client;
6
7mod api;
8
9pub use api::locations::{Location, Zone};
10
11pub struct TccApi {
13 client: Client,
14 login: LoggedIn,
15}
16
17impl TccApi {
18 pub async fn new_with_login<S1: AsRef<str>, S2: AsRef<str>>(
24 email: S1,
25 password: S2,
26 ) -> Result<Self, LoginError> {
27 let client = Client::builder()
28 .user_agent(USER_AGENT)
29 .cookie_store(true)
30 .build()?;
31
32 let login = login(&client, email, password).await?;
33
34 Ok(Self { client, login })
35 }
36
37 pub fn display_name(&self) -> &str {
39 &self.login.display_name
40 }
41
42 pub fn user_id(&self) -> &str {
44 &self.login.user_id
45 }
46
47 pub async fn get_locations(&self) -> Result<Vec<Location>, reqwest::Error> {
53 get_locations(&self.client).await
54 }
55
56 pub async fn set_zone_temperature(
63 &self,
64 zone_id: impl Into<String>,
65 temperature: f32,
66 ) -> Result<(), reqwest::Error> {
67 set_zone_temperature(&self.client, zone_id, temperature).await
68 }
69}