zai_rs/batches/
cancel.rs

1use serde::{Deserialize, Serialize};
2
3use super::types::BatchItem;
4use crate::client::http::HttpClient;
5
6/// Empty body for cancel API (serializes to `{}`)
7#[derive(Debug, Clone, Serialize, Deserialize, Default)]
8pub struct CancelBatchBody {}
9
10/// Cancel a running batch (POST /paas/v4/batches/{batch_id}/cancel)
11pub struct CancelBatchRequest {
12    /// Bearer API key
13    pub key: String,
14    /// Full URL including path parameter
15    url: String,
16    /// Empty JSON body
17    body: CancelBatchBody,
18}
19
20impl CancelBatchRequest {
21    /// Create a new cancel request for the given batch_id
22    pub fn new(key: String, batch_id: impl AsRef<str>) -> Self {
23        let url = format!(
24            "https://open.bigmodel.cn/api/paas/v4/batches/{}/cancel",
25            batch_id.as_ref()
26        );
27        Self {
28            key,
29            url,
30            body: CancelBatchBody::default(),
31        }
32    }
33
34    /// Send the request and parse typed response
35    pub async fn send(&self) -> anyhow::Result<CancelBatchResponse> {
36        let resp: reqwest::Response = self.post().await?;
37        let parsed = resp.json::<CancelBatchResponse>().await?;
38        Ok(parsed)
39    }
40}
41
42impl HttpClient for CancelBatchRequest {
43    type Body = CancelBatchBody;
44    type ApiUrl = String;
45    type ApiKey = String;
46
47    fn api_url(&self) -> &Self::ApiUrl {
48        &self.url
49    }
50    fn api_key(&self) -> &Self::ApiKey {
51        &self.key
52    }
53    fn body(&self) -> &Self::Body {
54        &self.body
55    }
56}
57
58/// Response type: a single Batch object
59pub type CancelBatchResponse = BatchItem;