seamapi_rs/
events.rs

1use anyhow::{bail, Context, Result};
2use serde::{Deserialize, Serialize};
3
4pub struct Events(pub String, pub String);
5
6#[derive(Serialize, Deserialize)]
7pub struct Event {
8    pub event_id: String,
9    pub event_type: String,
10    pub created_at: String,
11    pub device_id: String,
12    pub workspace_id: String,
13    pub battery_level: Option<f64>,
14}
15
16impl Events {
17    pub fn list(self, since: String, device_id: Option<String>) -> Result<Vec<Event>> {
18        let url = format!("{}/events/list", self.1);
19        let header = format!("Bearer {}", self.0);
20
21        let mut map = std::collections::HashMap::new();
22        map.insert("since", since);
23        if device_id != None {
24            map.insert("device_id", device_id.unwrap());
25        }
26
27        let req = reqwest::blocking::Client::new()
28            .get(url)
29            .header("Authorization", header)
30            .json(&map)
31            .send()
32            .context("Failed to send get request")?;
33
34        if req.status() != reqwest::StatusCode::OK {
35            bail!("{}", req.text().context("Really bad API error")?);
36        }
37
38        let json: crate::Response = req.json().context("Failed to deserialize JSON")?;
39        Ok(json.events.unwrap())
40    }
41}