openai-interface 0.14.0

A low-level Rust interface for the OpenAI API
Documentation
//! Delete request

use std::{future::Future, str::FromStr};

use serde::de::DeserializeOwned;

use crate::errors::OapiError;
use crate::rest::{RequestOptions, post::apply_options};

pub trait Delete {
    /// Returns the URL with query parameters
    ///
    /// Parameters:
    /// - _base_url_: should look like <https://api.openai.com/v1>
    fn build_url(&self, base_url: &str) -> Result<String, OapiError>;
}

pub trait DeleteNoStream: Delete + Send + Sync {
    type Response: DeserializeOwned + FromStr<Err = OapiError> + Send + Sync;

    /// Sends a DELETE request and returns the raw response body.
    ///
    /// The `client` is supplied by the caller; see [`crate::rest::default_client`]
    /// for a sensible default.
    fn get_response_string(
        &self,
        client: &reqwest::Client,
        base_url: &str,
        options: &RequestOptions,
    ) -> impl Future<Output = Result<String, OapiError>> + Send + Sync {
        async move {
            let url = self.build_url(base_url)?;

            let response = apply_options(
                client.delete(url).header("Accept", "application/json"),
                options,
            )
            .send()
            .await?;

            crate::rest::response_text_checked(response).await
        }
    }

    /// Sends a DELETE request and deserializes the response.
    fn get_response(
        &self,
        client: &reqwest::Client,
        base_url: &str,
        options: &RequestOptions,
    ) -> impl Future<Output = Result<Self::Response, OapiError>> + Send + Sync {
        async move {
            let text = self.get_response_string(client, base_url, options).await?;
            let result = Self::Response::from_str(&text)?;
            Ok(result)
        }
    }
}