Skip to main content

openai_interface/rest/
delete.rs

1//! Delete request
2
3use std::{future::Future, str::FromStr};
4
5use serde::de::DeserializeOwned;
6
7use crate::errors::OapiError;
8
9pub trait Delete {
10    /// Returns the URL with query parameters
11    ///
12    /// Parameters:
13    /// - _base_url_: should look like <https://api.openai.com/v1>
14    fn build_url(&self, base_url: &str) -> Result<String, OapiError>;
15}
16
17pub trait DeleteNoStream: Delete + Send + Sync {
18    type Response: DeserializeOwned + FromStr<Err = OapiError> + Send + Sync;
19
20    /// Sends a DELETE request and returns the raw response body.
21    ///
22    /// The `client` is supplied by the caller; see [`crate::rest::default_client`]
23    /// for a sensible default.
24    fn get_response_string(
25        &self,
26        client: &reqwest::Client,
27        base_url: &str,
28        key: &str,
29    ) -> impl Future<Output = Result<String, OapiError>> + Send + Sync {
30        async move {
31            let url = self.build_url(base_url)?;
32
33            let response = client
34                .delete(url)
35                .header("Accept", "application/json")
36                .bearer_auth(key)
37                .send()
38                .await?;
39
40            crate::rest::response_text_checked(response).await
41        }
42    }
43
44    /// Sends a DELETE request and deserializes the response.
45    fn get_response(
46        &self,
47        client: &reqwest::Client,
48        base_url: &str,
49        key: &str,
50    ) -> impl Future<Output = Result<Self::Response, OapiError>> + Send + Sync {
51        async move {
52            let text = self.get_response_string(client, base_url, key).await?;
53            let result = Self::Response::from_str(&text)?;
54            Ok(result)
55        }
56    }
57}