ring_client/client/api/
ticket.rs

1use crate::client::api::error::ApiError;
2use crate::client::api::RingApi;
3use crate::client::authentication::Tokens;
4use crate::helper::url::Url;
5use crate::{helper, Client};
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Serialize, Deserialize)]
9#[serde(rename_all = "lowercase")]
10#[allow(missing_docs)]
11pub enum AssetStatus {
12    Online,
13    Offline,
14}
15
16/// An Ring host.
17///
18/// This is effectively a Ring host which can be connected to for real-time streaming of Location events
19/// ([`crate::location::Location`]).
20#[derive(Debug, Serialize, Deserialize)]
21#[serde(rename_all = "camelCase")]
22pub struct Asset {
23    uuid: String,
24    doorbot_id: i64,
25    kind: String,
26    status: AssetStatus,
27    broker_host: String,
28    on_battery: bool,
29}
30
31/// A ticket for a Ring host.
32///
33/// This is effectively a session which allows for connections to Ring WebSocket servers.
34#[derive(Debug, Serialize, Deserialize)]
35#[serde(rename_all = "camelCase")]
36pub struct Ticket {
37    /// The ID of the ticket.
38    #[serde(rename = "ticket")]
39    pub id: String,
40
41    /// The URI of the host to connect to.
42    pub host: String,
43
44    subscription_topics: Vec<String>,
45    assets: Vec<Asset>,
46}
47
48impl RingApi {
49    pub async fn get_ticket(&self, location_id: &str, tokens: &Tokens) -> Result<Ticket, ApiError> {
50        let response = self
51            .client
52            .get(helper::url::get_base_url(&Url::Ticket))
53            .query(&[("locationID", location_id)])
54            .header("User-Agent", self.operating_system.get_user_agent())
55            .bearer_auth(&tokens.access_token)
56            .send()
57            .await?;
58
59        Ok(response.json::<Ticket>().await?)
60    }
61}
62
63impl Client {
64    pub(crate) async fn get_ticket(&self, location_id: &str) -> Result<Ticket, ApiError> {
65        self.api
66            .get_ticket(
67                location_id,
68                &*self
69                    .refresh_tokens_if_needed()
70                    .await
71                    .map_err(ApiError::AuthenticationRefreshFailed)?,
72            )
73            .await
74    }
75}