ring_client/client/api/
session.rs1use 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#[derive(Deserialize, Debug)]
12pub struct Profile {
13 pub id: usize,
15
16 pub email: String,
18
19 pub first_name: String,
21
22 pub last_name: String,
24
25 #[serde(flatten)]
26 #[allow(missing_docs)]
27 pub extra: HashMap<String, Value>,
28}
29
30#[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}