Skip to main content

zai_rs/model/async_chat_get/
data.rs

1use super::response::AsyncTaskResult;
2use crate::client::ZaiClient;
3
4/// Request for retrieving any asynchronous task by its task identifier.
5#[derive(Clone)]
6pub struct AsyncTaskGetRequest {
7    task_id: String,
8}
9
10impl std::fmt::Debug for AsyncTaskGetRequest {
11    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        formatter
13            .debug_struct("AsyncTaskGetRequest")
14            .field("task_id", &"[REDACTED]")
15            .finish()
16    }
17}
18
19impl AsyncTaskGetRequest {
20    /// Create a new get-result request for the given task id.
21    pub fn new(task_id: impl Into<String>) -> Self {
22        Self {
23            task_id: task_id.into(),
24        }
25    }
26
27    /// Borrow the task identifier used in the result URL.
28    pub fn task_id(&self) -> &str {
29        &self.task_id
30    }
31
32    /// Validate that the task id is non-empty.
33    pub fn validate(&self) -> crate::ZaiResult<()> {
34        if self.task_id.trim().is_empty() {
35            return Err(crate::client::error::ZaiError::ApiError {
36                code: crate::client::error::codes::SDK_VALIDATION,
37                message: "task_id must be non-empty".to_string(),
38            });
39        }
40        Ok(())
41    }
42
43    /// Fetch the asynchronous task result via a [`ZaiClient`].
44    pub async fn send_via(&self, client: &ZaiClient) -> crate::ZaiResult<AsyncTaskResult> {
45        self.validate()?;
46        let route = crate::client::routes::TASKS_GET;
47        let url = client.endpoints().resolve_route(route, &[&self.task_id])?;
48        client
49            .send_empty::<AsyncTaskResult>(route.method(), url)
50            .await
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn request_validates_and_redacts_the_task_identifier() {
60        assert!(AsyncTaskGetRequest::new(" ").validate().is_err());
61        let request = AsyncTaskGetRequest::new("private-task-id");
62        assert_eq!(request.task_id(), "private-task-id");
63        assert!(!format!("{request:?}").contains("private-task-id"));
64    }
65}