seamapi_rs/
locks.rs

1use anyhow::{bail, Context, Result};
2use serde::{Deserialize, Serialize};
3
4pub struct Locks(pub String, pub String);
5
6impl Locks {
7    pub fn list(self) -> Result<Vec<crate::devices::Device>> {
8        let url = format!("{}/locks/list", self.1);
9        let header = format!("Bearer {}", self.0);
10
11        let req = reqwest::blocking::Client::new()
12            .get(url)
13            .header("Authorization", header)
14            .send()
15            .context("Failed to send get request")?;
16
17        if req.status() != reqwest::StatusCode::OK {
18            bail!("{}", req.text().context("Really bad API failure")?);
19        }
20
21        let json: crate::Response = req.json().context("Failed to deserailize")?;
22        Ok(json.locks.unwrap())
23    }
24
25    pub fn get(self, device_id: String) -> Result<crate::devices::Device> {
26        let url = format!("{}/locks/get?device_id={}", self.1, device_id);
27        let header = format!("Bearer {}", self.0);
28
29        let req = reqwest::blocking::Client::new()
30            .get(url)
31            .header("Authorization", header)
32            .send()
33            .context("Failed to send get request")?;
34
35        if req.status() == reqwest::StatusCode::NOT_FOUND {
36            bail!("device not found");
37        } else if req.status() != reqwest::StatusCode::OK {
38            bail!("{}", req.text().context("Really bad API failure")?);
39        }
40
41        let json: crate::Response = req.json().context("Failed to deserialize JSON")?;
42        Ok(json.lock.unwrap())
43    }
44
45    pub fn lock_door(self, device_id: String) -> Result<crate::action_attempts::ActionAttempt> {
46        let url = format!("{}/locks/lock_door", self.1);
47        let header = format!("Bearer {}", self.0);
48
49        let mut map = std::collections::HashMap::new();
50        map.insert("device_id", device_id);
51
52        let req = reqwest::blocking::Client::new()
53            .post(url)
54            .header("Authorization", header)
55            .json(&map)
56            .send()
57            .context("Failed to send post request")?;
58
59        if req.status() == reqwest::StatusCode::NOT_FOUND {
60            bail!("device not found");
61        } else if req.status() != reqwest::StatusCode::OK {
62            bail!("{}", req.text().context("Really bad API failure")?);
63        }
64
65        let json: crate::Response = req.json().context("Failed to deserialize JSON")?;
66        Ok(json.action_attempt.unwrap())
67    }
68
69    pub fn unlock_door(self, device_id: String) -> Result<crate::action_attempts::ActionAttempt> {
70        let url = format!("{}/locks/unlock_door", self.1);
71        let header = format!("Bearer {}", self.0);
72
73        let mut map = std::collections::HashMap::new();
74        map.insert("device_id", device_id);
75
76        let req = reqwest::blocking::Client::new()
77            .post(url)
78            .header("Authorization", header)
79            .json(&map)
80            .send()
81            .context("Failed to send post request")?;
82
83        if req.status() == reqwest::StatusCode::NOT_FOUND {
84            bail!("device not found");
85        } else if req.status() != reqwest::StatusCode::OK {
86            bail!("{}", req.text().context("Really bad API failure")?);
87        }
88
89        let json: crate::Response = req.json().context("Failed to deserialize JSON")?;
90        Ok(json.action_attempt.unwrap())
91    }
92}