use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
validate_required,
};
use serde::{Deserialize, Serialize};
pub struct AgreeTaskRequestBuilder {
approval_task_id: String,
config: Config,
}
impl AgreeTaskRequestBuilder {
pub fn new(config: Config) -> Self {
Self {
approval_task_id: String::new(),
config,
}
}
pub fn approval_task_id(mut self, approval_task_id: impl Into<String>) -> Self {
self.approval_task_id = approval_task_id.into();
self
}
pub async fn execute(self) -> SDKResult<AgreeTaskResponse> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(self, option: RequestOption) -> SDKResult<AgreeTaskResponse> {
validate_required!(self.approval_task_id, "任务ID不能为空");
let url = format!(
"/open-apis/apaas/v1/approval_tasks/{}/agree",
self.approval_task_id
);
let api_request: ApiRequest<AgreeTaskResponse> = ApiRequest::post(url);
let response = Transport::request(api_request, &self.config, Some(option)).await?;
response
.data
.ok_or_else(|| openlark_core::error::validation_error("同意人工任务", "响应数据为空"))
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AgreeTaskResponse {
pub result: String,
}
impl ApiResponseTrait for AgreeTaskResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[deprecated(note = "renamed to AgreeTaskRequestBuilder, will be removed in v1.0 (#271)")]
pub type AgreeTaskBuilder = AgreeTaskRequestBuilder;
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_agree_approval_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/approval_tasks/task_001/agree"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0,
"msg": "success",
"data": { "result": "success" }
})))
.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 = AgreeTaskRequestBuilder::new(config)
.approval_task_id("task_001")
.execute()
.await
.expect("同意人工任务应成功");
assert_eq!(resp.result, "success");
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/apaas/v1/approval_tasks/task_001/agree"
);
}
}