openai-interface 0.13.0

A low-level Rust interface for the OpenAI API
Documentation
//! Cancel a background response via `POST /responses/{response_id}/cancel`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.
//!
//! Only responses created with `background: true` can be cancelled while
//! they are still queued or in progress. Provider support varies: Qwen's
//! Responses API documents this endpoint, while DeepSeek's Responses API
//! is stateless and does not run background responses.

use url::Url;

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

/// Cancels a previously created background response by its ID.
#[derive(Debug, Clone, serde::Serialize)]
pub struct CancelRequest<'a> {
    /// The ID of the response to cancel, e.g. `resp_...`.
    #[serde(skip)]
    pub response_id: &'a str,
}

impl Post for CancelRequest<'_> {
    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("responses")
            .push(self.response_id)
            .push("cancel");
        Ok(url.to_string())
    }
}

impl PostNoStream for CancelRequest<'_> {
    type Response = crate::responses::Response;
}