1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! API handle for file management operations.
use crate::error::Error;
use super::super::Client;
/// API handle for file management operations.
///
/// Provides direct access to server-side file upload, listing, and deletion functionality
/// without client-side logic. All file management decisions are explicit developer calls.
#[ derive( Debug ) ]
pub struct FilesApi< 'a >
{
pub( crate ) client : &'a Client,
}
impl FilesApi< '_ >
{
/// Upload a file to the Gemini API.
///
/// This method uploads a file to the Gemini API for use in content generation
/// and other operations. The file is stored server-side and can be referenced
/// in subsequent API calls.
///
/// # Arguments
///
/// * `request` - The upload file request containing file data and metadata
///
/// # Returns
///
/// Returns an [`UploadFileResponse`] containing:
/// - `file`: [`FileMetadata`] with file information including URI and processing state
///
/// # Errors
///
/// This method returns an error in the following cases:
/// - [`Error::NetworkError`] - Network connectivity issues or request timeout
/// - [`Error::AuthenticationError`] - Invalid or missing API key
/// - [`Error::ServerError`] - Gemini API server-side errors (5xx status codes)
/// - [`Error::ApiError`] - File size limits exceeded or unsupported file type
///
/// # Examples
///
/// ```rust,no_run
/// # use api_gemini::client::Client;
/// # use api_gemini::models::*;
/// # #[ tokio::main ]
/// # async fn main() -> Result< (), Box< dyn std::error::Error > > {
/// let client = Client::new()?;
/// let files_api = client.files();
///
/// // Read file data
/// let file_data = std::fs::read("example.png")?;
///
/// let request = UploadFileRequest {
/// file_data,
/// mime_type : "image/png".to_string(),
/// display_name : Some("Example Image".to_string()),
/// };
///
/// let response = files_api.upload(&request).await?;
/// println!("Uploaded file : {}", response.file.name);
/// # Ok( () )
/// # }
/// ```
#[ inline ]
pub async fn upload( &self, request : &crate::models::UploadFileRequest ) -> Result< crate::models::UploadFileResponse, Error >
{
let url = format!( "{}/upload/v1beta/files", self.client.base_url );
// Create multipart form for file upload
let form = reqwest::multipart::Form::new()
.part( "file", reqwest::multipart::Part::bytes( request.file_data.clone() )
.mime_str( &request.mime_type.clone() )?
.file_name( request.display_name.as_deref().unwrap_or( "file" ).to_string() ) );
let response = self.client.http
.post( &url )
.header( "X-Goog-Api-Key", &self.client.api_key )
.multipart( form )
.send()
.await
.map_err( Error::from )?;
if response.status().is_success()
{
let upload_response : crate::models::UploadFileResponse = response
.json()
.await
.map_err( |e| Error::DeserializationError( e.to_string() ) )?;
Ok( upload_response )
}
else
{
let status = response.status();
let text = response.text().await.unwrap_or_else( |_| "Failed to read error response".to_string() );
Err( Error::ApiError( format!( "HTTP {status}: {text}" ) ) )
}
}
/// List all files uploaded to the Gemini API.
///
/// This method retrieves a list of all files that have been uploaded to the Gemini API,
/// including their metadata and processing status. Supports pagination for large file lists.
///
/// # Arguments
///
/// * `request` - Optional list files request for pagination and filtering
///
/// # Returns
///
/// Returns a [`ListFilesResponse`] containing:
/// - `files`: Vector of [`FileMetadata`] objects with file information
/// - `next_page_token`: Token for retrieving the next page of results
///
/// # Errors
///
/// This method returns an error in the following cases:
/// - [`Error::NetworkError`] - Network connectivity issues or request timeout
/// - [`Error::AuthenticationError`] - Invalid or missing API key
/// - [`Error::ServerError`] - Gemini API server-side errors (5xx status codes)
/// - [`Error::DeserializationError`] - Failed to parse the API response
///
/// # Examples
///
/// ```rust,no_run
/// # use api_gemini::client::Client;
/// # use api_gemini::models::*;
/// # #[ tokio::main ]
/// # async fn main() -> Result< (), Box< dyn std::error::Error > > {
/// let client = Client::new()?;
/// let files_api = client.files();
///
/// // List all files
/// let request = ListFilesRequest::default();
/// let response = files_api.list(&request).await?;
///
/// for file in &response.files {
/// println!("File : {} ({})", file.name, file.mime_type);
/// }
/// # Ok( () )
/// # }
/// ```
#[ inline ]
pub async fn list( &self, request : &crate::models::ListFilesRequest ) -> Result< crate::models::ListFilesResponse, Error >
{
let mut url = format!( "{}/v1beta/files", self.client.base_url );
// Add query parameters if provided
let mut query_params = Vec::new();
if let Some( page_size ) = request.page_size
{
query_params.push( format!( "pageSize={page_size}" ) );
}
if let Some( ref page_token ) = request.page_token
{
query_params.push( format!( "pageToken={page_token}" ) );
}
if !query_params.is_empty()
{
url.push( '?' );
url.push_str( &query_params.join( "&" ) );
}
crate ::internal::http::execute_legacy::< (), crate::models::ListFilesResponse >
(
&self.client.http,
reqwest ::Method::GET,
&url,
&self.client.api_key,
None,
)
.await
}
/// Get metadata for a specific file.
///
/// This method retrieves detailed metadata for a specific file that has been
/// uploaded to the Gemini API, including processing status and download URI.
///
/// # Arguments
///
/// * `file_name` - The name/ID of the file to retrieve
///
/// # Returns
///
/// Returns [`FileMetadata`] containing detailed file information
///
/// # Errors
///
/// This method returns an error in the following cases:
/// - [`Error::NetworkError`] - Network connectivity issues or request timeout
/// - [`Error::AuthenticationError`] - Invalid or missing API key
/// - [`Error::ServerError`] - Gemini API server-side errors (5xx status codes)
/// - [`Error::ApiError`] - File not found (404)
///
/// # Examples
///
/// ```rust,no_run
/// # use api_gemini::client::Client;
/// # use api_gemini::models::*;
/// # #[ tokio::main ]
/// # async fn main() -> Result< (), Box< dyn std::error::Error > > {
/// let client = Client::new()?;
/// let files_api = client.files();
///
/// let file_metadata = files_api.get("files/abc123").await?;
/// println!("File : {} ({})", file_metadata.name, file_metadata.mime_type);
/// # Ok( () )
/// # }
/// ```
#[ inline ]
pub async fn get( &self, file_name : &str ) -> Result< crate::models::FileMetadata, Error >
{
let url = format!( "{}/v1beta/{}", self.client.base_url, file_name );
crate ::internal::http::execute_legacy::< (), crate::models::FileMetadata >
(
&self.client.http,
reqwest ::Method::GET,
&url,
&self.client.api_key,
None,
)
.await
}
/// Delete a file from the Gemini API.
///
/// This method permanently deletes a file that has been uploaded to the Gemini API.
/// Once deleted, the file cannot be recovered and can no longer be used in API calls.
///
/// # Arguments
///
/// * `file_name` - The name/ID of the file to delete
///
/// # Returns
///
/// Returns `Ok(())` if the file was successfully deleted
///
/// # Errors
///
/// This method returns an error in the following cases:
/// - [`Error::NetworkError`] - Network connectivity issues or request timeout
/// - [`Error::AuthenticationError`] - Invalid or missing API key
/// - [`Error::ServerError`] - Gemini API server-side errors (5xx status codes)
/// - [`Error::ApiError`] - File not found (404) or deletion failed
///
/// # Examples
///
/// ```rust,no_run
/// # use api_gemini::client::Client;
/// # #[ tokio::main ]
/// # async fn main() -> Result< (), Box< dyn std::error::Error > > {
/// let client = Client::new()?;
/// let files_api = client.files();
///
/// files_api.delete("files/abc123").await?;
/// println!("File deleted successfully");
/// # Ok( () )
/// # }
/// ```
#[ inline ]
pub async fn delete( &self, file_name : &str ) -> Result< (), Error >
{
let url = format!( "{}/v1beta/{}", self.client.base_url, file_name );
let response = self.client.http
.delete( &url )
.header( "X-Goog-Api-Key", &self.client.api_key )
.send()
.await
.map_err( Error::from )?;
if response.status().is_success()
{
Ok( () )
}
else
{
let status = response.status();
let text = response.text().await.unwrap_or_else( |_| "Failed to read error response".to_string() );
Err( Error::ApiError( format!( "HTTP {status}: {text}" ) ) )
}
}
}