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