ring_client/client/api/
session.rs

1use crate::client::api::error::ApiError;
2use crate::client::api::RingApi;
3use crate::client::authentication::Tokens;
4use crate::helper::url::Url;
5use crate::{constant, helper};
6use serde::Deserialize;
7use serde_json::{json, Value};
8use std::collections::HashMap;
9
10/// The profile data for the logged in user.
11#[derive(Deserialize, Debug)]
12pub struct Profile {
13    /// The ID of the user.
14    pub id: usize,
15
16    /// The email address of the user.
17    pub email: String,
18
19    /// The first name of the user.
20    pub first_name: String,
21
22    /// The last name of the user.
23    pub last_name: String,
24
25    #[serde(flatten)]
26    #[allow(missing_docs)]
27    pub extra: HashMap<String, Value>,
28}
29
30/// An active session
31#[derive(Deserialize, Debug)]
32#[allow(missing_docs)]
33pub struct Session {
34    pub profile: Profile,
35}
36
37impl RingApi {
38    pub async fn set_session(
39        &self,
40        display_name: &str,
41        system_id: &str,
42        tokens: &Tokens,
43    ) -> Result<Session, ApiError> {
44        Ok(self
45            .client
46            .post(helper::url::get_base_url(&Url::Session))
47            .header("User-Agent", self.operating_system.get_user_agent())
48            .bearer_auth(&tokens.access_token)
49            .json(&json!({
50                "device": {
51                    "hardware_id": helper::hardware::generate_hardware_id(system_id),
52                    "os": self.operating_system.to_string(),
53                    "metadata": {
54                        "api_version": constant::API_VERSION,
55                        "device_model": display_name
56                    }
57                }
58            }))
59            .send()
60            .await?
61            .json::<Session>()
62            .await?)
63    }
64}