Skip to main content

openlark_workflow/v2/custom_field/
add.rs

1//! 将自定义字段加入资源
2//!
3//! docPath: https://open.feishu.cn/document/task-v2/custom_field/add
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use openlark_core::{
7    SDKResult,
8    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
9    config::Config,
10    validate_required,
11};
12use serde::{Deserialize, Serialize};
13use std::sync::Arc;
14
15/// 将自定义字段加入资源请求体
16#[derive(Debug, Clone, Serialize, Default)]
17pub struct AddCustomFieldBody {
18    /// 任务清单 GUID
19    #[serde(skip_serializing_if = "String::is_empty")]
20    pub tasklist_guid: String,
21}
22
23/// 将自定义字段加入资源响应
24#[derive(Debug, Clone, Deserialize)]
25pub struct AddCustomFieldResponse {
26    /// 是否成功
27    pub success: bool,
28}
29
30/// 将自定义字段加入资源请求
31#[derive(Debug, Clone)]
32pub struct AddCustomFieldRequest {
33    config: Arc<Config>,
34    custom_field_guid: String,
35    body: AddCustomFieldBody,
36}
37
38impl AddCustomFieldRequest {
39    /// 创建新的请求构建器。
40    pub fn new(config: Arc<Config>, custom_field_guid: impl Into<String>) -> Self {
41        Self {
42            config,
43            custom_field_guid: custom_field_guid.into(),
44            body: AddCustomFieldBody::default(),
45        }
46    }
47
48    /// 设置任务清单 GUID
49    pub fn tasklist_guid(mut self, tasklist_guid: impl Into<String>) -> Self {
50        self.body.tasklist_guid = tasklist_guid.into();
51        self
52    }
53
54    /// 执行请求
55    pub async fn execute(self) -> SDKResult<AddCustomFieldResponse> {
56        self.execute_with_options(openlark_core::req_option::RequestOption::default())
57            .await
58    }
59
60    /// 执行请求(带选项)
61    pub async fn execute_with_options(
62        self,
63        option: openlark_core::req_option::RequestOption,
64    ) -> SDKResult<AddCustomFieldResponse> {
65        validate_required!(self.custom_field_guid.trim(), "自定义字段 GUID 不能为空");
66
67        let api_endpoint = TaskApiV2::CustomFieldAdd(self.custom_field_guid);
68        let mut request = ApiRequest::<AddCustomFieldResponse>::post(api_endpoint.to_url());
69
70        let request_body = &self.body;
71        request = request.body(serialize_params(request_body, "将自定义字段加入资源")?);
72
73        let response =
74            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
75        extract_response_data(response, "将自定义字段加入资源")
76    }
77}
78
79impl ApiResponseTrait for AddCustomFieldResponse {
80    fn data_format() -> ResponseFormat {
81        ResponseFormat::Data
82    }
83}
84
85#[cfg(test)]
86#[allow(unused_imports)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn test_add_custom_field_url() {
92        let endpoint = TaskApiV2::CustomFieldAdd("test_guid".to_string());
93        assert_eq!(
94            endpoint.to_url(),
95            "/open-apis/task/v2/custom_fields/test_guid/add"
96        );
97    }
98}