Skip to main content

openlark_platform/common/
api_utils.rs

1//! API 通用工具函数
2//!
3//! 提供 API 实现的通用工具和辅助函数,减少重复代码,提高一致性。
4
5use openlark_core::{SDKResult, error};
6
7/// 标准化 API 响应数据提取
8pub fn extract_response_data<T>(
9    response: openlark_core::api::ApiResponse<T>,
10    context: &str,
11) -> SDKResult<T> {
12    response.data.ok_or_else(|| {
13        error::validation_error(format!("{context}响应数据为空"), "服务器没有返回有效的数据")
14    })
15}
16
17/// 标准化参数序列化错误处理
18pub fn serialize_params<T: serde::Serialize>(
19    params: &T,
20    context: &str,
21) -> SDKResult<serde_json::Value> {
22    serde_json::to_value(params).map_err(|e| {
23        error::validation_error(
24            format!("{context}参数序列化失败"),
25            format!("无法序列化请求参数: {e}"),
26        )
27    })
28}