openai-interface 0.10.0

A low-level Rust interface for the OpenAI API
Documentation
//! Cancel a batch job via `POST /batches/{batch_id}/cancel`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.
//!
//! Cancellation is asynchronous: the request returns the batch with
//! status `cancelling`, and it eventually settles as `cancelled`.

use url::Url;

use crate::{
    batches::Batch,
    errors::OapiError,
    rest::post::{Post, PostNoStream},
};

/// Cancels an in-progress batch by its ID.
#[derive(Debug, Clone, serde::Serialize)]
pub struct CancelBatchRequest<'a> {
    /// The ID of the batch to cancel, e.g. `batch_...`.
    #[serde(skip)]
    pub batch_id: &'a str,
}

impl Post for CancelBatchRequest<'_> {
    fn is_streaming(&self) -> bool {
        false
    }

    /// Builds the URL for the request.
    ///
    /// `base_url` should be like <https://api.openai.com/v1>
    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
        url.path_segments_mut()
            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
            .push("batches")
            .push(self.batch_id)
            .push("cancel");
        Ok(url.to_string())
    }
}

impl PostNoStream for CancelBatchRequest<'_> {
    type Response = Batch;
}