openlark-docs 0.19.0

飞书开放平台云文档服务模块 - 文档、表格、知识库API (202 APIs, 100% 覆盖,不含旧版本)
Documentation
//! 列出仪表盘
//!
//! docPath: <https://open.feishu.cn/document/server-docs/docs/bitable-v1/app-dashboard/list>
use openlark_core::{
    SDKResult,
    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
    config::Config,
    http::Transport,
    req_option::RequestOption,
    validate_required,
};
use serde::{Deserialize, Serialize};

use crate::common::api_endpoints::BitableApiV1;

/// 列出仪表盘请求。
#[derive(Debug, Clone)]
pub struct ListDashboardsRequest {
    config: Config,
    app_token: String,
    page_size: Option<i32>,
    page_token: Option<String>,
}

impl ListDashboardsRequest {
    /// 创建新的仪表盘列表请求。
    pub fn new(config: Config) -> Self {
        Self {
            config,
            app_token: String::new(),
            page_size: None,
            page_token: None,
        }
    }

    /// 设置多维表格 token。
    pub fn app_token(mut self, app_token: impl Into<String>) -> Self {
        self.app_token = app_token.into();
        self
    }

    /// 设置分页大小。
    pub fn page_size(mut self, page_size: i32) -> Self {
        self.page_size = Some(page_size);
        self
    }

    /// 设置分页标记。
    pub fn page_token(mut self, page_token: impl Into<String>) -> Self {
        self.page_token = Some(page_token.into());
        self
    }

    /// 执行请求。
    pub async fn execute(self) -> SDKResult<ListDashboardsResponse> {
        self.execute_with_options(RequestOption::default()).await
    }

    /// 使用指定请求选项执行请求。
    pub async fn execute_with_options(
        self,
        option: RequestOption,
    ) -> SDKResult<ListDashboardsResponse> {
        validate_required!(self.app_token, "app_token 不能为空");
        if let Some(page_size) = self.page_size
            && !(1..=500).contains(&page_size)
        {
            return Err(openlark_core::error::validation_error(
                "page_size",
                "page_size 必须在 1~500 之间",
            ));
        }

        let api_endpoint = BitableApiV1::DashboardList(self.app_token);
        // #439: method 来自 catalog
        let mut api_request: ApiRequest<ListDashboardsResponse> = api_endpoint.to_request();

        api_request = api_request.query_opt("page_size", self.page_size.map(|v| v.to_string()));
        api_request = api_request.query_opt("page_token", self.page_token);

        Transport::request_typed(api_request, &self.config, Some(option), "列出仪表盘").await
    }
}

/// 列出仪表盘响应。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListDashboardsResponse {
    /// 仪表盘列表。
    pub dashboards: Vec<super::Dashboard>,
    /// 下一页分页标记。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
    /// 是否还有更多数据。
    pub has_more: bool,
}

impl ApiResponseTrait for ListDashboardsResponse {
    fn data_format() -> ResponseFormat {
        ResponseFormat::Data
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use wiremock::MockServer;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, ResponseTemplate};

    /// 端到端:GET .../dashboards → ListDashboardsResponse。
    #[tokio::test]
    async fn test_list_dashboards_returns_data_on_success() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/open-apis/bitable/v1/apps/app001/dashboards"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "code": 0, "msg": "success", "data": { "dashboards": [], "has_more": false }
            })))
            .mount(&server)
            .await;
        let config = Config::builder()
            .app_id("ci_app_id")
            .app_secret("ci_app_secret")
            .base_url(server.uri())
            .enable_token_cache(false)
            .build();
        ListDashboardsRequest::new(config)
            .app_token("app001")
            .execute()
            .await
            .expect("列出仪表盘应成功");
        let received = server.received_requests().await.unwrap_or_default();
        assert_eq!(received.len(), 1);
        assert_eq!(
            received[0].url.path(),
            "/open-apis/bitable/v1/apps/app001/dashboards"
        );
    }
}