seamapi_rs/
lib.rs

1#![allow(unused)]
2
3use anyhow::{Context, Result};
4use serde::{Deserialize, Serialize};
5
6pub mod access_codes;
7pub mod action_attempts;
8pub mod connect_accounts;
9pub mod connect_webviews;
10pub mod devices;
11pub mod events;
12pub mod locks;
13pub mod workspaces;
14
15/// This struct isn't intended to be used directly
16#[derive(Serialize, Deserialize)]
17pub struct Response {
18    pub lock: Option<crate::devices::Device>,
19    pub locks: Option<Vec<crate::devices::Device>>,
20    pub workspace: Option<crate::workspaces::Workspace>,
21    pub workspaces: Option<Vec<crate::workspaces::Workspace>>,
22    pub action_attempt: Option<crate::action_attempts::ActionAttempt>,
23    pub access_code: Option<crate::access_codes::AccessCode>,
24    pub access_codes: Option<Vec<crate::access_codes::AccessCode>>,
25    pub device: Option<crate::devices::Device>,
26    pub devices: Option<Vec<crate::devices::Device>>,
27    pub events: Option<Vec<crate::events::Event>>,
28    pub connected_account: Option<crate::connect_accounts::ConnectedAccount>,
29    pub connected_accounts: Option<Vec<crate::connect_accounts::ConnectedAccount>>,
30    pub connect_webview: Option<crate::connect_webviews::ConnectWebview>,
31    pub connect_webviews: Option<Vec<crate::connect_webviews::ConnectWebview>>,
32    pub ok: bool,
33}
34
35#[derive(Clone, Copy)]
36pub struct Seam {
37    api_key: &'static str,
38    api_url: &'static str,
39}
40
41impl Seam {
42    pub fn new(api_key: Option<&str>, api_url: Option<&str>) -> Result<Self> {
43        let mut key = String::new();
44        let mut url = String::new();
45
46        if api_key == None {
47            key = std::env::var("SEAM_API_KEY")
48                .context("SEAM_API_KEY not found in environment, and api_key not provided")?;
49        } else {
50            key = api_key.context("invalid API key")?.to_string();
51        }
52
53        if api_url == None {
54            url = match std::env::var("SEAM_API_URL") {
55                Ok(a) => a.to_string(),
56                _ => "https://connect.getseam.com".to_string(),
57            };
58        } else {
59            url = api_url.context("invalid URL")?.to_string();
60        }
61
62        let leak_key: &'static str = Box::leak(key.into_boxed_str());
63        let leak_url: &'static str = Box::leak(url.into_boxed_str());
64        Ok(Self {
65            api_key: leak_key,
66            api_url: leak_url,
67        })
68    }
69
70    pub fn workspace(self) -> workspaces::Workspaces {
71        workspaces::Workspaces(self.api_key.to_string(), self.api_url.to_string())
72    }
73
74    pub fn connected_accounts(self) -> connect_accounts::ConnectedAccounts {
75        connect_accounts::ConnectedAccounts(self.api_key.to_string(), self.api_url.to_string())
76    }
77
78    pub fn connect_webviews(self) -> connect_webviews::ConnectWebviews {
79        connect_webviews::ConnectWebviews(self.api_key.to_string(), self.api_url.to_string())
80    }
81
82    pub fn devices(self) -> devices::Devices {
83        devices::Devices(self.api_key.to_string(), self.api_url.to_string())
84    }
85
86    pub fn locks(self) -> locks::Locks {
87        locks::Locks(self.api_key.to_string(), self.api_url.to_string())
88    }
89
90    pub fn access_codes(self) -> access_codes::AccessCodes {
91        access_codes::AccessCodes(self.api_key.to_string(), self.api_url.to_string())
92    }
93
94    pub fn action_attempts(self) -> action_attempts::ActionAttempts {
95        action_attempts::ActionAttempts(self.api_key.to_string(), self.api_url.to_string())
96    }
97
98    pub fn events(self) -> events::Events {
99        events::Events(self.api_key.to_string(), self.api_url.to_string())
100    }
101}