Skip to main content

openlark_platform/admin/v1/badge/
create.rs

1//! 创建勋章 API
2//!
3//! API文档: <https://open.feishu.cn/document/server-docs/admin-v1/badge/badge/create>
4//! docPath: <https://open.feishu.cn/document/server-docs/admin-v1/badge/badge/create-2>
5
6use crate::common::api_endpoints::AdminApiV1;
7use openlark_core::{
8    SDKResult,
9    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
10    config::Config,
11    http::Transport,
12    req_option::RequestOption,
13    validate_required,
14};
15use serde::{Deserialize, Serialize};
16
17/// 创建勋章请求
18pub struct CreateBadgeRequestBuilder {
19    name: String,
20    description: Option<String>,
21    icon_url: Option<String>,
22    config: Config,
23}
24
25impl CreateBadgeRequestBuilder {
26    /// 创建新的请求构建器。
27    pub fn new(config: Config) -> Self {
28        Self {
29            name: String::new(),
30            description: None,
31            icon_url: None,
32            config,
33        }
34    }
35
36    /// 设置名称。
37    pub fn name(mut self, name: impl Into<String>) -> Self {
38        self.name = name.into();
39        self
40    }
41
42    /// 设置描述。
43    pub fn description(mut self, description: impl Into<String>) -> Self {
44        self.description = Some(description.into());
45        self
46    }
47
48    /// 设置图标地址。
49    pub fn icon_url(mut self, icon_url: impl Into<String>) -> Self {
50        self.icon_url = Some(icon_url.into());
51        self
52    }
53
54    /// 使用默认请求选项执行请求。
55    pub async fn execute(self) -> SDKResult<CreateBadgeResponse> {
56        self.execute_with_options(RequestOption::default()).await
57    }
58
59    /// 使用指定请求选项执行请求。
60    pub async fn execute_with_options(
61        self,
62        option: RequestOption,
63    ) -> SDKResult<CreateBadgeResponse> {
64        validate_required!(self.name, "勋章名称不能为空");
65
66        let request_body = CreateBadgeRequest {
67            name: self.name,
68            description: self.description,
69            icon_url: self.icon_url,
70        };
71
72        let api_request: ApiRequest<CreateBadgeResponse> =
73            ApiRequest::post(AdminApiV1::CreateBadge.path())
74                .body(serde_json::to_value(&request_body)?);
75
76        Transport::request_typed(api_request, &self.config, Some(option), "创建勋章").await
77    }
78}
79
80/// 创建勋章请求体
81#[derive(Debug, Serialize)]
82struct CreateBadgeRequest {
83    name: String,
84    #[serde(skip_serializing_if = "Option::is_none")]
85    description: Option<String>,
86    #[serde(skip_serializing_if = "Option::is_none")]
87    icon_url: Option<String>,
88}
89
90/// 创建勋章响应
91#[derive(Debug, Clone, Deserialize, Serialize)]
92/// 创建勋章的响应。
93pub struct CreateBadgeResponse {
94    /// 勋章 ID。
95    pub badge_id: String,
96    /// 名称。
97    pub name: String,
98    /// 描述。
99    pub description: Option<String>,
100    /// 图标地址。
101    pub icon_url: Option<String>,
102    /// 创建时间。
103    pub create_time: String,
104}
105
106impl ApiResponseTrait for CreateBadgeResponse {
107    fn data_format() -> ResponseFormat {
108        ResponseFormat::Data
109    }
110}
111
112/// 旧名兼容别名(将在 v1.0 移除)
113#[deprecated(note = "renamed to CreateBadgeRequestBuilder, will be removed in v1.0 (#271)")]
114pub type CreateBadgeBuilder = CreateBadgeRequestBuilder;
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119
120    #[test]
121    fn test_builder_basic() {
122        let config = openlark_core::config::Config::builder()
123            .app_id("test_app")
124            .app_secret("test_secret")
125            .build();
126        let request = CreateBadgeRequestBuilder::new(config.clone())
127            .name("test".to_string())
128            .description("test".to_string());
129        let _ = request;
130    }
131}