openlark-client 0.15.0

OpenLark 高级客户端 - 统一入口点和轻量级服务注册表
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! OpenLark Client 核心类型定义
//!
//! 包含客户端相关的核心类型

use serde::Deserialize;
use std::time::Duration;

/// 📄 API响应特征
///
/// 所有API响应都应该实现此特征
pub trait ApiResponse: for<'de> Deserialize<'de> + Send + Sync + 'static {
    /// 🔍 检查响应是否成功
    fn is_success(&self) -> bool;

    /// 📝 获取错误消息
    fn error_message(&self) -> Option<&String>;

    /// 🔄 转换为Result类型
    fn into_result(self) -> Result<Self, crate::Error>;
}

/// 📄 API响应包装器
///
/// 统一的API响应格式
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct ApiResponseData<T> {
    /// 📊 响应数据
    pub data: T,
    /// ✅ 请求是否成功
    pub success: bool,
    /// 📝 响应消息
    pub message: Option<String>,
    /// 🏷️ 请求ID
    pub request_id: String,
    /// ⏱️ 响应时间戳
    pub timestamp: Option<i64>,
    /// 📋 额外的元数据
    pub extra: std::collections::HashMap<String, serde_json::Value>,
}

impl<T> ApiResponseData<T> {
    /// 🆕 创建成功响应
    pub fn success(data: T) -> Self {
        Self {
            data,
            success: true,
            message: None,
            request_id: uuid::Uuid::new_v4().to_string(),
            timestamp: Some(chrono::Utc::now().timestamp()),
            extra: std::collections::HashMap::new(),
        }
    }

    /// 🆕 创建错误响应(需要 `T: Default`)
    ///
    /// 注意:
    /// - 旧实现曾使用 `mem::zeroed()` 为泛型 `T` 构造占位值,这是不安全且可能导致 UB 的。
    /// - 若 `T` 无法提供默认值,请使用 `error_with_data` 显式传入 `data`。
    pub fn error(message: impl Into<String>) -> Self
    where
        T: Default,
    {
        Self {
            data: T::default(),
            success: false,
            message: Some(message.into()),
            request_id: uuid::Uuid::new_v4().to_string(),
            timestamp: Some(chrono::Utc::now().timestamp()),
            extra: std::collections::HashMap::new(),
        }
    }

    /// 🆕 创建错误响应(显式传入 `data`,避免对 `T` 施加额外约束)
    pub fn error_with_data(data: T, message: impl Into<String>) -> Self {
        Self {
            data,
            success: false,
            message: Some(message.into()),
            request_id: uuid::Uuid::new_v4().to_string(),
            timestamp: Some(chrono::Utc::now().timestamp()),
            extra: std::collections::HashMap::new(),
        }
    }

    /// 🔍 检查响应是否成功
    pub fn is_success(&self) -> bool {
        self.success
    }

    /// 📝 获取错误消息
    pub fn error_message(&self) -> Option<&String> {
        self.message.as_ref()
    }

    /// 🔄 转换为Result类型
    pub fn into_result(self) -> Result<T, crate::Error> {
        if self.success {
            Ok(self.data)
        } else {
            Err(crate::error::api_error(
                500,
                "response",
                self.message.unwrap_or_default(),
                None,
            ))
        }
    }
}

impl<T: serde::de::DeserializeOwned + Send + Sync + 'static> ApiResponse for ApiResponseData<T> {
    fn is_success(&self) -> bool {
        self.success
    }

    fn error_message(&self) -> Option<&String> {
        self.message.as_ref()
    }

    fn into_result(self) -> Result<Self, crate::Error> {
        if self.success {
            Ok(self)
        } else {
            Err(crate::error::api_error(
                500,
                "response",
                self.message.unwrap_or_default(),
                None,
            ))
        }
    }
}

/// 📋 分页响应
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct PaginatedResponse<T> {
    /// 📄 数据项
    pub items: Vec<T>,
    /// 🔄 是否有更多数据
    pub has_more: bool,
    /// 📄 分页token
    pub page_token: Option<String>,
    /// 📊 总数(如果可用)
    pub total: Option<u64>,
}

impl<T> PaginatedResponse<T> {
    /// 🆕 创建新的分页响应
    pub fn new(items: Vec<T>) -> Self {
        Self {
            items,
            has_more: false,
            page_token: None,
            total: None,
        }
    }

    /// 🆕 创建带分页的响应
    pub fn with_pagination(items: Vec<T>, has_more: bool, page_token: Option<String>) -> Self {
        Self {
            items,
            has_more,
            page_token,
            total: None,
        }
    }

    /// 📊 获取项目数量
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// 🔍 检查是否为空
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }
}

/// 📋 请求选项
#[derive(Debug, Clone, Default)]
pub struct RequestOptions {
    /// ⏱️ 超时时间
    pub timeout: Option<Duration>,
    /// 🔄 重试次数
    pub retry_count: Option<u32>,
    /// 📝 自定义头部
    pub headers: Option<std::collections::HashMap<String, String>>,
}

impl RequestOptions {
    /// 🆕 创建默认请求选项
    pub fn new() -> Self {
        Self::default()
    }

    /// ⏱️ 设置超时时间
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// 🔄 设置重试次数
    pub fn retry_count(mut self, count: u32) -> Self {
        self.retry_count = Some(count);
        self
    }

    /// 📝 添加自定义头部
    pub fn header(mut self, key: String, value: String) -> Self {
        self.headers
            .get_or_insert_with(std::collections::HashMap::new)
            .insert(key, value);
        self
    }
}

#[cfg(test)]
#[allow(unused_imports)]
mod tests {
    use super::*;

    #[test]
    fn test_api_response_data() {
        // 直接构造响应数据,避免使用可能有问题的方法
        let response = ApiResponseData {
            data: "test data".to_string(),
            success: true,
            message: None,
            request_id: "test-request-123".to_string(),
            timestamp: Some(1640995200),
            extra: std::collections::HashMap::new(),
        };

        assert!(response.is_success());
        assert_eq!(response.data, "test data");

        let error_response = ApiResponseData {
            data: String::new(),
            success: false,
            message: Some("测试错误".to_string()),
            request_id: "test-request-456".to_string(),
            timestamp: Some(1640995200),
            extra: std::collections::HashMap::new(),
        };

        assert!(!error_response.is_success());
        assert_eq!(
            error_response.error_message(),
            Some(&"测试错误".to_string())
        );
    }

    #[test]
    fn test_paginated_response() {
        let items = vec!["item1", "item2", "item3"];
        let response = PaginatedResponse::new(items.clone());

        assert_eq!(response.len(), 3);
        assert!(!response.has_more);
        assert!(response.page_token.is_none());

        let paginated =
            PaginatedResponse::with_pagination(items.clone(), true, Some("next_token".to_string()));
        assert!(paginated.has_more);
        assert_eq!(paginated.page_token, Some("next_token".to_string()));
    }

    #[test]
    fn test_request_options() {
        let options = RequestOptions::new()
            .timeout(Duration::from_secs(30))
            .retry_count(3)
            .header("User-Agent".to_string(), "test-client".to_string());

        assert_eq!(options.timeout, Some(Duration::from_secs(30)));
        assert_eq!(options.retry_count, Some(3));
        assert!(options.headers.is_some());
    }

    #[test]
    fn test_api_response_data_success() {
        let response: ApiResponseData<i32> = ApiResponseData::success(42);
        assert!(response.is_success());
        assert_eq!(response.data, 42);
        assert!(response.error_message().is_none());
        assert!(!response.request_id.is_empty());
    }

    #[test]
    fn test_api_response_data_error() {
        let response: ApiResponseData<String> = ApiResponseData::error("发生错误");
        assert!(!response.is_success());
        assert_eq!(response.error_message(), Some(&"发生错误".to_string()));
        assert!(!response.request_id.is_empty());
    }

    #[test]
    fn test_api_response_data_error_with_data() {
        let response = ApiResponseData::error_with_data(123, "操作失败");
        assert!(!response.is_success());
        assert_eq!(response.data, 123);
        assert_eq!(response.error_message(), Some(&"操作失败".to_string()));
    }

    #[test]
    fn test_api_response_data_into_result_success() {
        let response: ApiResponseData<i32> = ApiResponseData {
            data: 42,
            success: true,
            message: None,
            request_id: "test".to_string(),
            timestamp: None,
            extra: std::collections::HashMap::new(),
        };
        let result = response.into_result();
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 42);
    }

    #[test]
    fn test_api_response_data_into_result_error() {
        let response: ApiResponseData<i32> = ApiResponseData {
            data: 0,
            success: false,
            message: Some("出错了".to_string()),
            request_id: "test".to_string(),
            timestamp: None,
            extra: std::collections::HashMap::new(),
        };
        let result = response.into_result();
        assert!(result.is_err());
    }

    #[test]
    fn test_api_response_trait() {
        let response: ApiResponseData<String> = ApiResponseData {
            data: "test".to_string(),
            success: true,
            message: None,
            request_id: "req-123".to_string(),
            timestamp: None,
            extra: std::collections::HashMap::new(),
        };
        assert!(response.is_success());
        assert!(response.error_message().is_none());
    }

    #[test]
    fn test_api_response_trait_error() {
        let response: ApiResponseData<String> = ApiResponseData {
            data: String::new(),
            success: false,
            message: Some("错误信息".to_string()),
            request_id: "req-456".to_string(),
            timestamp: None,
            extra: std::collections::HashMap::new(),
        };
        assert!(!response.is_success());
        assert_eq!(response.error_message(), Some(&"错误信息".to_string()));
    }

    #[test]
    fn test_paginated_response_empty() {
        let response: PaginatedResponse<String> = PaginatedResponse::new(vec![]);
        assert!(response.is_empty());
        assert_eq!(response.len(), 0);
        assert!(!response.has_more);
    }

    #[test]
    fn test_paginated_response_with_total() {
        let items = vec!["a", "b", "c"];
        let response = PaginatedResponse {
            items: items.clone(),
            has_more: true,
            page_token: Some("next".to_string()),
            total: Some(100),
        };
        assert_eq!(response.len(), 3);
        assert!(response.has_more);
        assert_eq!(response.total, Some(100));
    }

    #[test]
    fn test_request_options_default() {
        let options: RequestOptions = Default::default();
        assert!(options.timeout.is_none());
        assert!(options.retry_count.is_none());
        assert!(options.headers.is_none());
    }

    #[test]
    fn test_request_options_new() {
        let options = RequestOptions::new();
        assert!(options.timeout.is_none());
        assert!(options.retry_count.is_none());
    }

    #[test]
    fn test_request_options_multiple_headers() {
        let options = RequestOptions::new()
            .header("Authorization".to_string(), "Bearer token".to_string())
            .header("Content-Type".to_string(), "application/json".to_string());

        let headers = options.headers.unwrap();
        assert_eq!(
            headers.get("Authorization"),
            Some(&"Bearer token".to_string())
        );
        assert_eq!(
            headers.get("Content-Type"),
            Some(&"application/json".to_string())
        );
    }

    #[test]
    fn test_request_options_only_timeout() {
        let options = RequestOptions::new().timeout(Duration::from_secs(60));
        assert_eq!(options.timeout, Some(Duration::from_secs(60)));
        assert!(options.retry_count.is_none());
    }

    #[test]
    fn test_request_options_only_retry() {
        let options = RequestOptions::new().retry_count(5);
        assert_eq!(options.retry_count, Some(5));
        assert!(options.timeout.is_none());
    }

    #[test]
    fn test_api_response_data_clone() {
        let response = ApiResponseData {
            data: 42,
            success: true,
            message: Some("test".to_string()),
            request_id: "req".to_string(),
            timestamp: Some(1234567890),
            extra: std::collections::HashMap::new(),
        };
        let cloned = response.clone();
        assert_eq!(cloned.data, 42);
        assert!(cloned.success);
    }

    #[test]
    fn test_api_response_data_serialize() {
        let response: ApiResponseData<i32> = ApiResponseData::success(42);
        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("42"));
        assert!(json.contains("true"));
    }

    #[test]
    fn test_api_response_data_deserialize() {
        let json = r#"{"data":42,"success":true,"message":null,"request_id":"req-123","timestamp":1234567890,"extra":{}}"#;
        let response: ApiResponseData<i32> = serde_json::from_str(json).unwrap();
        assert_eq!(response.data, 42);
        assert!(response.success);
    }
}