use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct CcTaskRequestBuilder {
config: Config,
task_id: String,
user_ids: Vec<String>,
reason: Option<String>,
}
impl CcTaskRequestBuilder {
pub fn new(config: Config, task_id: impl Into<String>) -> Self {
Self {
config,
task_id: task_id.into(),
user_ids: Vec::new(),
reason: None,
}
}
pub fn user_id(mut self, user_id: impl Into<String>) -> Self {
self.user_ids.push(user_id.into());
self
}
pub fn user_ids(mut self, user_ids: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.user_ids.extend(user_ids.into_iter().map(Into::into));
self
}
pub fn reason(mut self, reason: impl Into<String>) -> Self {
self.reason = Some(reason.into());
self
}
pub async fn execute(self) -> SDKResult<CcTaskResponse> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(self, option: RequestOption) -> SDKResult<CcTaskResponse> {
let url = format!("/open-apis/apaas/v1/user_tasks/{}/cc", self.task_id);
let request = CcTaskRequest {
user_ids: self.user_ids,
reason: self.reason,
};
let req: ApiRequest<CcTaskResponse> =
ApiRequest::post(&url).body(serde_json::to_value(&request)?);
Transport::request_typed(req, &self.config, Some(option), "Operation").await
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
struct CcTaskRequest {
#[serde(rename = "user_ids")]
user_ids: Vec<String>,
#[serde(rename = "reason", skip_serializing_if = "Option::is_none")]
reason: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CcTaskResponse {
#[serde(rename = "task_id")]
pub task_id: String,
#[serde(rename = "cc_id")]
pub cc_id: String,
#[serde(rename = "message")]
pub message: String,
}
impl ApiResponseTrait for CcTaskResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[deprecated(note = "renamed to CcTaskRequestBuilder, will be removed in v1.0 (#271)")]
pub type CcTaskBuilder = CcTaskRequestBuilder;
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_cc_user_task_returns_data_on_success() {
use serde_json::json;
use wiremock::MockServer;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/open-apis/apaas/v1/user_tasks/task_001/cc"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0,
"msg": "success",
"data": {
"task_id": "task_001",
"cc_id": "cc_001",
"message": "抄送成功"
}
})))
.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();
let resp = CcTaskRequestBuilder::new(config, "task_001")
.user_ids(vec!["u_001".to_string(), "u_002".to_string()])
.reason("请知会")
.execute()
.await
.expect("抄送人工任务应成功");
assert_eq!(resp.task_id, "task_001");
assert_eq!(resp.cc_id, "cc_001");
assert_eq!(resp.message, "抄送成功");
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/apaas/v1/user_tasks/task_001/cc"
);
}
}