openlark-communication 0.15.0

飞书开放平台 Rust SDK - 企业级 API 客户端
Documentation
//! 搜索用户
//!
//! docPath: https://open.feishu.cn/document/server-docs/contact-v3/user/search-users

use openlark_core::{api::ApiRequest, config::Config, http::Transport, SDKResult};

use crate::{common::api_utils::extract_response_data, endpoints::SEARCH_V1_USER};

/// 搜索用户请求
pub struct SearchUserRequest {
    config: Config,
    query: Option<String>,
}

impl SearchUserRequest {
    pub fn new(config: Config) -> Self {
        Self {
            config,
            query: None,
        }
    }

    /// 搜索关键字(查询参数,可选)
    pub fn query(mut self, query: impl Into<String>) -> Self {
        self.query = Some(query.into());
        self
    }

    /// 执行请求
    ///
    /// docPath: https://open.feishu.cn/document/server-docs/contact-v3/user/search-users
    pub async fn execute(self) -> SDKResult<serde_json::Value> {
        self.execute_with_options(openlark_core::req_option::RequestOption::default())
            .await
    }

    pub async fn execute_with_options(
        self,
        option: openlark_core::req_option::RequestOption,
    ) -> SDKResult<serde_json::Value> {
        let mut req: ApiRequest<serde_json::Value> = ApiRequest::get(SEARCH_V1_USER);

        if let Some(query) = self.query {
            req = req.query("query", &query);
        }
        let resp = Transport::request(req, &self.config, Some(option)).await?;
        extract_response_data(resp, "搜索用户")
    }
}

#[cfg(test)]
#[allow(unused_imports)]
mod tests {

    #[test]
    fn test_serialization_roundtrip() {
        // 基础序列化测试
        let json = r#"{"test": "value"}"#;
        assert!(serde_json::from_str::<serde_json::Value>(json).is_ok());
    }

    #[test]
    fn test_deserialization_from_json() {
        // 基础反序列化测试
        let json = r#"{"field": "data"}"#;
        let value: serde_json::Value = serde_json::from_str(json).unwrap();
        assert_eq!(value["field"], "data");
    }
}