Skip to main content

openai_interface/rest/
get.rs

1//! GET request functionality for OpenAI interface
2//!
3//! This module provides HTTP GET request functionality for making requests to OpenAI-compatible APIs.
4//! Unlike POST requests, GET requests typically don't require serialization of request bodies,
5//! but may include query parameters.
6
7use std::{future::Future, str::FromStr};
8
9use serde::de::DeserializeOwned;
10
11use crate::errors::OapiError;
12use crate::rest::{RequestOptions, post::apply_options};
13
14/// Base trait for GET requests
15pub trait Get {
16    /// Returns the URL with query parameters if needed
17    ///
18    /// # Example
19    ///
20    /// ```rust
21    /// use openai_interface::rest::get::Get;
22    /// use openai_interface::errors::OapiError;
23    ///
24    /// struct MyRequest {
25    ///     id: String,
26    ///     limit: Option<u32>,
27    /// }
28    ///
29    /// impl Get for MyRequest {
30    ///     fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
31    ///         let mut url = format!("{}/{}", base_url, self.id);
32    ///         if let Some(limit) = self.limit {
33    ///             url.push_str(&format!("?limit={}", limit));
34    ///         }
35    ///         Ok(url)
36    ///     }
37    /// }
38    /// ```
39    fn build_url(&self, base_url: &str) -> Result<String, OapiError>;
40}
41
42/// Trait for non-streaming GET requests
43pub trait GetNoStream: Get + Sync + Send {
44    type Response: DeserializeOwned + FromStr<Err = OapiError> + Send + Sync;
45
46    /// Sends a GET request and returns the raw response body.
47    ///
48    /// The `client` is supplied by the caller; see [`crate::rest::default_client`]
49    /// for a sensible default.
50    fn get_response_string(
51        &self,
52        client: &reqwest::Client,
53        base_url: &str,
54        options: &RequestOptions,
55    ) -> impl Future<Output = Result<String, OapiError>> + Send + Sync {
56        async move {
57            let url = self.build_url(base_url)?;
58            let response = apply_options(
59                client.get(&url).header("Accept", "application/json"),
60                options,
61            )
62            .send()
63            .await?;
64
65            crate::rest::response_text_checked(response).await
66        }
67    }
68
69    /// Sends a GET request and deserializes the response
70    fn get_response(
71        &self,
72        client: &reqwest::Client,
73        base_url: &str,
74        options: &RequestOptions,
75    ) -> impl Future<Output = Result<Self::Response, OapiError>> + Send + Sync {
76        async move {
77            let text = self.get_response_string(client, base_url, options).await?;
78            let result = Self::Response::from_str(&text)?;
79            Ok(result)
80        }
81    }
82}
83
84/// Trait for GET requests whose response body is binary rather than JSON.
85///
86/// Endpoints such as `GET /files/{file_id}/content` return the raw file
87/// contents instead of a JSON object, so the body is returned as raw bytes.
88pub trait GetBinary: Get + Sync + Send {
89    /// Sends a GET request and returns the raw response body bytes.
90    fn get_response_bytes(
91        &self,
92        client: &reqwest::Client,
93        base_url: &str,
94        options: &RequestOptions,
95    ) -> impl Future<Output = Result<Vec<u8>, OapiError>> + Send + Sync {
96        async move {
97            let url = self.build_url(base_url)?;
98            let response = apply_options(client.get(&url).header("Accept", "*/*"), options)
99                .send()
100                .await?;
101
102            crate::rest::response_bytes_checked(response).await
103        }
104    }
105}