1use std::sync::Arc;
2
3use serde::{Deserialize, Serialize};
4
5use super::types::BatchItem;
6use crate::{
7 ZaiResult,
8 client::{
9 endpoints::{ApiBase, EndpointConfig, join_url, paths},
10 http::{HttpClient, HttpClientConfig, parse_typed_response},
11 },
12};
13
14#[derive(Debug, Clone, Serialize, Deserialize, Default)]
16pub struct CancelBatchBody {}
17
18pub struct CancelBatchRequest {
20 pub key: String,
22 url: String,
24 endpoint_config: EndpointConfig,
25 api_base: ApiBase,
26 batch_id: String,
27 http_config: Arc<HttpClientConfig>,
28 body: CancelBatchBody,
30}
31
32impl CancelBatchRequest {
33 pub fn new(key: String, batch_id: impl AsRef<str>) -> Self {
35 let batch_id = batch_id.as_ref().to_string();
36 let endpoint_config = EndpointConfig::default();
37 let api_base = ApiBase::PaasV4;
38 let path = join_url(&join_url(paths::BATCHES, &batch_id), "cancel");
39 let url = endpoint_config.url(&api_base, &path);
40 Self {
41 key,
42 url,
43 endpoint_config,
44 api_base,
45 batch_id,
46 http_config: Arc::new(HttpClientConfig::default()),
47 body: CancelBatchBody::default(),
48 }
49 }
50
51 fn rebuild_url(&mut self) {
52 let path = join_url(&join_url(paths::BATCHES, &self.batch_id), "cancel");
53 self.url = self.endpoint_config.url(&self.api_base, &path);
54 }
55
56 pub fn with_base_url(mut self, base: impl Into<String>) -> Self {
57 self.api_base = ApiBase::Custom(base.into());
58 self.rebuild_url();
59 self
60 }
61
62 pub fn with_endpoint_config(mut self, endpoint_config: EndpointConfig) -> Self {
63 self.endpoint_config = endpoint_config;
64 self.rebuild_url();
65 self
66 }
67
68 pub fn with_http_config(mut self, config: HttpClientConfig) -> Self {
69 self.http_config = Arc::new(config);
70 self
71 }
72
73 pub async fn send(&self) -> ZaiResult<CancelBatchResponse> {
75 let resp: reqwest::Response = self.post().await?;
76 let parsed = parse_typed_response::<CancelBatchResponse>(resp).await?;
77 Ok(parsed)
78 }
79}
80
81impl HttpClient for CancelBatchRequest {
82 type Body = CancelBatchBody;
83 type ApiUrl = String;
84 type ApiKey = String;
85
86 fn api_url(&self) -> &Self::ApiUrl {
87 &self.url
88 }
89 fn api_key(&self) -> &Self::ApiKey {
90 &self.key
91 }
92 fn body(&self) -> &Self::Body {
93 &self.body
94 }
95
96 fn http_config(&self) -> Arc<HttpClientConfig> {
97 Arc::clone(&self.http_config)
98 }
99}
100
101pub type CancelBatchResponse = BatchItem;