Skip to main content

openlark_communication/common/
chain.rs

1//! openlark-communication 链式调用入口(meta 风格,偏"模块入口")
2//!
3//! 说明:
4//! - 本文件放在 `common/` 下,避免被 strict API 校验脚本计入"额外实现文件"。
5//! - communication 模块 API 规模较大(IM/Contact/Moments 等)。为避免为大量 API 手写方法,
6//!   这里先提供"bizTag 级入口 + Config 透传"。
7//! - 具体 API 调用仍使用各 `*RequestBuilder/*Request` 的 `new(config)` / `execute(...)`。
8
9use std::sync::Arc;
10
11use openlark_core::config::Config;
12
13/// Communication 链式入口:`communication.im` / `communication.contact` / `communication.moments`
14#[derive(Debug, Clone)]
15pub struct CommunicationClient {
16    config: Arc<Config>,
17
18    #[cfg(feature = "im")]
19    pub im: ImClient,
20
21    #[cfg(feature = "contact")]
22    pub contact: ContactClient,
23
24    #[cfg(feature = "moments")]
25    pub moments: MomentsClient,
26}
27
28impl CommunicationClient {
29    pub fn new(config: Config) -> Self {
30        let config = Arc::new(config);
31        Self {
32            config: config.clone(),
33            #[cfg(feature = "im")]
34            im: ImClient::new(config.clone()),
35            #[cfg(feature = "contact")]
36            contact: ContactClient::new(config.clone()),
37            #[cfg(feature = "moments")]
38            moments: MomentsClient::new(config),
39        }
40    }
41
42    pub fn config(&self) -> &Config {
43        &self.config
44    }
45}
46
47#[cfg(feature = "im")]
48#[derive(Debug, Clone)]
49pub struct ImClient {
50    config: Arc<Config>,
51}
52
53#[cfg(feature = "im")]
54impl ImClient {
55    fn new(config: Arc<Config>) -> Self {
56        Self { config }
57    }
58
59    pub fn config(&self) -> &Config {
60        &self.config
61    }
62}
63
64#[cfg(feature = "contact")]
65#[derive(Debug, Clone)]
66pub struct ContactClient {
67    config: Arc<Config>,
68}
69
70#[cfg(feature = "contact")]
71impl ContactClient {
72    fn new(config: Arc<Config>) -> Self {
73        Self { config }
74    }
75
76    pub fn config(&self) -> &Config {
77        &self.config
78    }
79}
80
81#[cfg(feature = "moments")]
82#[derive(Debug, Clone)]
83pub struct MomentsClient {
84    config: Arc<Config>,
85}
86
87#[cfg(feature = "moments")]
88impl MomentsClient {
89    fn new(config: Arc<Config>) -> Self {
90        Self { config }
91    }
92
93    pub fn config(&self) -> &Config {
94        &self.config
95    }
96}
97
98#[cfg(test)]
99#[allow(unused_imports)]
100mod tests {
101    use super::*;
102
103    fn create_test_config() -> Config {
104        Config::builder()
105            .app_id("test_app")
106            .app_secret("test_secret")
107            .build()
108    }
109
110    #[test]
111    fn test_communication_client_creation() {
112        let config = create_test_config();
113        let client = CommunicationClient::new(config);
114        assert_eq!(client.config().app_id(), "test_app");
115    }
116
117    #[test]
118    fn test_communication_client_debug() {
119        let config = create_test_config();
120        let client = CommunicationClient::new(config);
121        let debug_str = format!("{:?}", client);
122        assert!(debug_str.contains("CommunicationClient"));
123    }
124
125    #[test]
126    fn test_communication_client_clone() {
127        let config = create_test_config();
128        let client = CommunicationClient::new(config);
129        let cloned = client.clone();
130        assert_eq!(cloned.config().app_id(), "test_app");
131    }
132
133    #[cfg(feature = "im")]
134    #[test]
135    fn test_im_client_config() {
136        let config = create_test_config();
137        let client = CommunicationClient::new(config);
138        assert_eq!(client.im.config().app_id(), "test_app");
139    }
140
141    #[cfg(feature = "contact")]
142    #[test]
143    fn test_contact_client_config() {
144        let config = create_test_config();
145        let client = CommunicationClient::new(config);
146        assert_eq!(client.contact.config().app_id(), "test_app");
147    }
148}