openai-interface 0.6.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;

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,
        key: &str,
    ) -> impl Future<Output = Result<String, OapiError>> + Send + Sync {
        async move {
            let url = self.build_url(base_url)?;

            let response = client
                .delete(url)
                .header("Accept", "application/json")
                .bearer_auth(key)
                .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,
        key: &str,
    ) -> impl Future<Output = Result<Self::Response, OapiError>> + Send + Sync {
        async move {
            let text = self.get_response_string(client, base_url, key).await?;
            let result = Self::Response::from_str(&text)?;
            Ok(result)
        }
    }
}