Skip to main content

openai_interface/batches/
cancel.rs

1//! Cancel a batch job via `POST /batches/{batch_id}/cancel`.
2//!
3//! > ![warn] This module is untested!
4//! > If you encounter any issues, please report them on the repository.
5//!
6//! Cancellation is asynchronous: the request returns the batch with
7//! status `cancelling`, and it eventually settles as `cancelled`.
8
9use url::Url;
10
11use crate::{
12    batches::Batch,
13    errors::OapiError,
14    rest::post::{Post, PostNoStream},
15};
16
17/// Cancels an in-progress batch by its ID.
18#[derive(Debug, Clone, serde::Serialize)]
19pub struct CancelBatchRequest<'a> {
20    /// The ID of the batch to cancel, e.g. `batch_...`.
21    #[serde(skip)]
22    pub batch_id: &'a str,
23}
24
25impl Post for CancelBatchRequest<'_> {
26    fn is_streaming(&self) -> bool {
27        false
28    }
29
30    /// Builds the URL for the request.
31    ///
32    /// `base_url` should be like <https://api.openai.com/v1>
33    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
34        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
35        url.path_segments_mut()
36            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
37            .push("batches")
38            .push(self.batch_id)
39            .push("cancel");
40        Ok(url.to_string())
41    }
42}
43
44impl PostNoStream for CancelBatchRequest<'_> {
45    type Response = Batch;
46}