Skip to main content

openai_interface/files/
content.rs

1//! Returns the contents of the specified file.
2//!
3//! > ![warn] This module is untested!
4//! > OpenAI-compatible providers (DeepSeek, Qwen) are not known to implement
5//! > this endpoint, and no OpenAI API key was available for testing. If you
6//! > encounter any issues, please report them on the repository.
7
8pub mod request {
9    use std::collections::HashMap;
10
11    use url::Url;
12
13    use crate::{
14        errors::OapiError,
15        rest::get::{Get, GetBinary},
16    };
17
18    /// Returns the contents of the specified file as raw bytes.
19    #[derive(Debug, Default)]
20    pub struct ContentRequest<'a> {
21        /// The ID of the file to use for this request.
22        pub file_id: &'a str,
23        /// Add additional query parameters to the request.
24        pub extra_query: HashMap<&'a str, &'a str>,
25    }
26
27    impl Get for ContentRequest<'_> {
28        /// base_url should look like <https://api.openai.com/v1>
29        fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
30            let mut url =
31                Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
32            url.path_segments_mut()
33                .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
34                .push("files")
35                .push(self.file_id)
36                .push("content");
37
38            for (key, value) in &self.extra_query {
39                url.query_pairs_mut().append_pair(key, value);
40            }
41
42            Ok(url.to_string())
43        }
44    }
45
46    impl GetBinary for ContentRequest<'_> {}
47}
48
49#[cfg(test)]
50mod tests {
51    use super::request::ContentRequest;
52    use crate::rest::get::Get;
53
54    #[test]
55    fn test_build_url() {
56        let request = ContentRequest {
57            file_id: "file-abc123",
58            ..Default::default()
59        };
60        let url = request.build_url("https://api.openai.com/v1/").unwrap();
61        assert_eq!(url, "https://api.openai.com/v1/files/file-abc123/content");
62    }
63}