async_openai_wasm/
file.rs1use bytes::Bytes;
2
3use crate::{
4 Client, RequestOptions,
5 config::Config,
6 error::OpenAIError,
7 types::files::{CreateFileRequest, DeleteFileResponse, ListFilesResponse, OpenAIFile},
8};
9
10pub struct Files<'c, C: Config> {
12 client: &'c Client<C>,
13 pub(crate) request_options: RequestOptions,
14}
15
16impl<'c, C: Config> Files<'c, C> {
17 pub fn new(client: &'c Client<C>) -> Self {
18 Self {
19 client,
20 request_options: RequestOptions::new(),
21 }
22 }
23
24 #[crate::byot(
34 T0 = Clone,
35 R = serde::de::DeserializeOwned,
36 where_clause = "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
37 )]
38 pub async fn create(&self, request: CreateFileRequest) -> Result<OpenAIFile, OpenAIError> {
39 self.client
40 .post_form("/files", request, &self.request_options)
41 .await
42 }
43
44 #[crate::byot(R = serde::de::DeserializeOwned)]
46 pub async fn list(&self) -> Result<ListFilesResponse, OpenAIError> {
47 self.client.get("/files", &self.request_options).await
48 }
49
50 #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
52 pub async fn retrieve(&self, file_id: &str) -> Result<OpenAIFile, OpenAIError> {
53 self.client
54 .get(format!("/files/{file_id}").as_str(), &self.request_options)
55 .await
56 }
57
58 #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
60 pub async fn delete(&self, file_id: &str) -> Result<DeleteFileResponse, OpenAIError> {
61 self.client
62 .delete(format!("/files/{file_id}").as_str(), &self.request_options)
63 .await
64 }
65
66 pub async fn content(&self, file_id: &str) -> Result<Bytes, OpenAIError> {
68 let (bytes, _headers) = self
69 .client
70 .get_raw(
71 format!("/files/{file_id}/content").as_str(),
72 &self.request_options,
73 )
74 .await?;
75 Ok(bytes)
76 }
77}