use crate::client::api::RingApi;
use crate::client::api::error::ApiError;
use crate::client::authentication::Tokens;
use crate::helper;
use crate::helper::url::Url;
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::fmt;
use std::fmt::{Debug, Formatter};
#[derive(Deserialize, Debug)]
#[serde(tag = "kind")]
#[serde(rename_all = "snake_case")]
pub enum DeviceData {
CocoaCamera {
id: usize,
location_id: String,
description: String,
#[serde(flatten)]
#[allow(missing_docs)]
extra: HashMap<String, Value>,
},
DoorbellGrahamCracker {
id: usize,
location_id: String,
description: String,
#[serde(flatten)]
#[allow(missing_docs)]
extra: HashMap<String, Value>,
},
BaseStationV1 {
id: usize,
location_id: String,
description: String,
#[serde(flatten)]
#[allow(missing_docs)]
extra: HashMap<String, Value>,
},
#[serde(other)]
Other,
}
pub struct Device {
#[allow(missing_docs)]
pub data: DeviceData,
}
impl Debug for Device {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Device").field("data", &self.data).finish()
}
}
impl Device {
pub(crate) const fn new(data: DeviceData) -> Self {
Self { data }
}
}
#[derive(Deserialize)]
struct Response {
doorbots: Vec<DeviceData>,
authorized_doorbots: Vec<DeviceData>,
chimes: Vec<DeviceData>,
stickup_cams: Vec<DeviceData>,
base_stations: Vec<DeviceData>,
beams: Vec<DeviceData>,
beams_bridges: Vec<DeviceData>,
other: Vec<DeviceData>,
}
impl RingApi {
pub async fn get_device_data(&self, tokens: &Tokens) -> Result<Vec<DeviceData>, ApiError> {
let response = self
.client
.get(helper::url::get_base_url(&Url::Devices))
.header("User-Agent", self.operating_system.get_user_agent())
.bearer_auth(&tokens.access_token)
.send()
.await?
.json::<Response>()
.await?;
Ok(Vec::new()
.into_iter()
.chain(response.doorbots)
.chain(response.authorized_doorbots)
.chain(response.chimes)
.chain(response.stickup_cams)
.chain(response.base_stations)
.chain(response.beams)
.chain(response.beams_bridges)
.chain(response.other)
.collect())
}
}