portkey_sdk/service/
files.rs1use std::future::Future;
6
7use crate::client::PortkeyClient;
8use crate::error::Result;
9use crate::model::{DeleteFileResponse, FileObject, ListFilesResponse, UploadFileRequest};
10
11pub trait FilesService {
13 fn upload_file(&self, request: UploadFileRequest) -> impl Future<Output = Result<FileObject>>;
40
41 fn list_files(&self) -> impl Future<Output = Result<ListFilesResponse>>;
57
58 fn retrieve_file(&self, file_id: &str) -> impl Future<Output = Result<FileObject>>;
78
79 fn retrieve_file_content(&self, file_id: &str) -> impl Future<Output = Result<Vec<u8>>>;
99
100 fn delete_file(&self, file_id: &str) -> impl Future<Output = Result<DeleteFileResponse>>;
120}
121
122impl FilesService for PortkeyClient {
123 #[cfg_attr(
124 feature = "tracing",
125 tracing::instrument(skip(self, request), fields(filename = %request.filename, purpose = %request.purpose))
126 )]
127 async fn upload_file(&self, request: UploadFileRequest) -> Result<FileObject> {
128 #[cfg(feature = "tracing")]
129 tracing::debug!(
130 target: crate::TRACING_TARGET_SERVICE,
131 "Uploading file"
132 );
133
134 let part =
135 reqwest::multipart::Part::bytes(request.file).file_name(request.filename.clone());
136
137 let form = reqwest::multipart::Form::new()
138 .part("file", part)
139 .text("purpose", request.purpose);
140
141 let response = self
142 .send_multipart(reqwest::Method::POST, "/files", form)
143 .await?
144 .error_for_status()?
145 .json::<FileObject>()
146 .await?;
147
148 #[cfg(feature = "tracing")]
149 tracing::info!(
150 target: crate::TRACING_TARGET_SERVICE,
151 id = %response.id,
152 filename = %response.filename,
153 "File uploaded successfully"
154 );
155
156 Ok(response)
157 }
158
159 #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
160 async fn list_files(&self) -> Result<ListFilesResponse> {
161 #[cfg(feature = "tracing")]
162 tracing::debug!(
163 target: crate::TRACING_TARGET_SERVICE,
164 "Listing files"
165 );
166
167 let response = self
168 .send(reqwest::Method::GET, "/files")
169 .await?
170 .error_for_status()?
171 .json::<ListFilesResponse>()
172 .await?;
173
174 #[cfg(feature = "tracing")]
175 tracing::info!(
176 target: crate::TRACING_TARGET_SERVICE,
177 count = response.data.len(),
178 "Files listed successfully"
179 );
180
181 Ok(response)
182 }
183
184 #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(file_id)))]
185 async fn retrieve_file(&self, file_id: &str) -> Result<FileObject> {
186 #[cfg(feature = "tracing")]
187 tracing::debug!(
188 target: crate::TRACING_TARGET_SERVICE,
189 "Retrieving file"
190 );
191
192 let response = self
193 .send(reqwest::Method::GET, &format!("/files/{}", file_id))
194 .await?
195 .error_for_status()?
196 .json::<FileObject>()
197 .await?;
198
199 #[cfg(feature = "tracing")]
200 tracing::info!(
201 target: crate::TRACING_TARGET_SERVICE,
202 id = %response.id,
203 filename = %response.filename,
204 "File retrieved successfully"
205 );
206
207 Ok(response)
208 }
209
210 #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(file_id)))]
211 async fn retrieve_file_content(&self, file_id: &str) -> Result<Vec<u8>> {
212 #[cfg(feature = "tracing")]
213 tracing::debug!(
214 target: crate::TRACING_TARGET_SERVICE,
215 "Retrieving file content"
216 );
217
218 let response = self
219 .send(reqwest::Method::GET, &format!("/files/{}/content", file_id))
220 .await?
221 .error_for_status()?
222 .bytes()
223 .await?;
224
225 #[cfg(feature = "tracing")]
226 tracing::info!(
227 target: crate::TRACING_TARGET_SERVICE,
228 size = response.len(),
229 "File content retrieved successfully"
230 );
231
232 Ok(response.to_vec())
233 }
234
235 #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(file_id)))]
236 async fn delete_file(&self, file_id: &str) -> Result<DeleteFileResponse> {
237 #[cfg(feature = "tracing")]
238 tracing::debug!(
239 target: crate::TRACING_TARGET_SERVICE,
240 "Deleting file"
241 );
242
243 let response = self
244 .send(reqwest::Method::DELETE, &format!("/files/{}", file_id))
245 .await?
246 .error_for_status()?
247 .json::<DeleteFileResponse>()
248 .await?;
249
250 #[cfg(feature = "tracing")]
251 tracing::info!(
252 target: crate::TRACING_TARGET_SERVICE,
253 id = %response.id,
254 deleted = response.deleted,
255 "File deleted"
256 );
257
258 Ok(response)
259 }
260}