Skip to main content

beeper_desktop_api/client/
mod.rs

1/// Client module for Beeper Desktop API
2///
3/// Contains the main BeeperClient and method implementations for different API areas.
4
5pub mod chats;
6pub mod messages;
7pub mod accounts;
8pub mod search;
9pub mod app;
10mod utils;
11
12use reqwest::Client;
13use serde::Deserialize;
14use self::utils::handle_response;
15
16/// Main Beeper API client
17///
18/// Stores the bearer token and base URL for API requests.
19/// All requests to the API will include the token in the Authorization header.
20#[derive(Clone)]
21pub struct BeeperClient {
22    token: String,
23    base_url: String,
24    http_client: Client,
25}
26
27impl BeeperClient {
28    /// Creates a new Beeper API client
29    ///
30    /// # Arguments
31    ///
32    /// * `token` - Bearer token for authentication
33    /// * `base_url` - Base URL of the Beeper Desktop API server
34    pub fn new(token: impl Into<String>, base_url: impl Into<String>) -> Self {
35        Self {
36            token: token.into(),
37            base_url: base_url.into(),
38            http_client: Client::new(),
39        }
40    }
41
42    /// Creates a new Beeper API client with default base URL
43    ///
44    /// # Arguments
45    ///
46    /// * `token` - Bearer token for authentication
47    pub fn with_token(token: impl Into<String>) -> Self {
48        Self::new(token, DEFAULT_BASE_URL)
49    }
50
51    /// Updates the bearer token
52    pub fn set_token(&mut self, token: impl Into<String>) {
53        self.token = token.into();
54    }
55
56    /// Updates the base URL
57    pub fn set_base_url(&mut self, base_url: impl Into<String>) {
58        self.base_url = base_url.into();
59    }
60
61    /// Gets the current base URL
62    pub fn base_url(&self) -> &str {
63        &self.base_url
64    }
65
66    pub(crate) fn get_auth_header(&self) -> String {
67        format!("Bearer {}", self.token)
68    }
69
70    pub(crate) fn get_base_url(&self) -> &str {
71        &self.base_url
72    }
73
74    pub(crate) fn get_http_client(&self) -> &Client {
75        &self.http_client
76    }
77}
78
79const DEFAULT_BASE_URL: &str = "http://localhost:23373";
80
81#[derive(Debug, Deserialize)]
82pub(crate) struct ApiErrorResponse {
83    pub code: String,
84    pub message: String,
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn test_client_creation() {
93        let client = BeeperClient::new("test-token", "http://localhost:23373");
94        assert_eq!(client.base_url(), "http://localhost:23373");
95    }
96
97    #[test]
98    fn test_client_with_default_url() {
99        let client = BeeperClient::with_token("test-token");
100        assert_eq!(client.base_url(), DEFAULT_BASE_URL);
101    }
102
103    #[test]
104    fn test_client_set_token() {
105        let mut client = BeeperClient::new("old-token", "http://localhost:23373");
106        client.set_token("new-token");
107        assert_eq!(client.base_url(), "http://localhost:23373");
108    }
109
110    #[test]
111    fn test_client_set_base_url() {
112        let mut client = BeeperClient::new("test-token", "http://localhost:23373");
113        client.set_base_url("http://example.com:8080");
114        assert_eq!(client.base_url(), "http://example.com:8080");
115    }
116
117    #[test]
118    fn test_client_clone() {
119        let client1 = BeeperClient::new("test-token", "http://localhost:23373");
120        let client2 = client1.clone();
121        assert_eq!(client1.base_url(), client2.base_url());
122    }
123
124    #[test]
125    fn test_get_auth_header() {
126        let client = BeeperClient::new("my-secret-token", "http://localhost:23373");
127        let auth_header = client.get_auth_header();
128        assert_eq!(auth_header, "Bearer my-secret-token");
129    }
130}