Skip to main content

openlark_platform/admin/v1/badge_image/
create.rs

1//! 上传勋章图片 API
2//! docPath: <https://open.feishu.cn/document/server-docs/admin-v1/badge/badge/create>
3
4use openlark_core::{
5    SDKResult,
6    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
7    config::Config,
8    http::Transport,
9    req_option::RequestOption,
10    validate_required,
11};
12use serde::{Deserialize, Serialize};
13
14/// 上传勋章图片的请求构建器。
15pub struct CreateBadgeImageRequestBuilder {
16    image: String,
17    config: Config,
18}
19
20impl CreateBadgeImageRequestBuilder {
21    /// 创建新的请求构建器。
22    pub fn new(config: Config) -> Self {
23        Self {
24            image: String::new(),
25            config,
26        }
27    }
28
29    /// 设置图片内容。
30    pub fn image(mut self, image: impl Into<String>) -> Self {
31        self.image = image.into();
32        self
33    }
34
35    /// 使用默认请求选项执行请求。
36    pub async fn execute(self) -> SDKResult<CreateBadgeImageResponse> {
37        self.execute_with_options(RequestOption::default()).await
38    }
39
40    /// 使用指定请求选项执行请求。
41    pub async fn execute_with_options(
42        self,
43        option: RequestOption,
44    ) -> SDKResult<CreateBadgeImageResponse> {
45        validate_required!(self.image, "图片不能为空");
46
47        let request_body = CreateBadgeImageRequest { image: self.image };
48        let api_request: ApiRequest<CreateBadgeImageResponse> =
49            ApiRequest::post("/open-apis/admin/v1/badge_images")
50                .body(serde_json::to_value(&request_body)?);
51
52        Transport::request_typed(api_request, &self.config, Some(option), "上传勋章图片").await
53    }
54}
55
56#[derive(Debug, Serialize)]
57struct CreateBadgeImageRequest {
58    image: String,
59}
60
61#[derive(Debug, Clone, Deserialize, Serialize)]
62/// 上传勋章图片的响应。
63pub struct CreateBadgeImageResponse {
64    /// 图片 ID。
65    pub image_id: String,
66    /// 图片访问地址。
67    pub image_url: String,
68}
69
70impl ApiResponseTrait for CreateBadgeImageResponse {
71    fn data_format() -> ResponseFormat {
72        ResponseFormat::Data
73    }
74}
75
76/// 旧名兼容别名(将在 v1.0 移除)
77#[deprecated(note = "renamed to CreateBadgeImageRequestBuilder, will be removed in v1.0 (#271)")]
78pub type CreateBadgeImageBuilder = CreateBadgeImageRequestBuilder;
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn test_builder_basic() {
86        let config = openlark_core::config::Config::builder()
87            .app_id("test_app")
88            .app_secret("test_secret")
89            .build();
90        let request = CreateBadgeImageRequestBuilder::new(config.clone()).image("test".to_string());
91        let _ = request;
92    }
93}