rust-genai 0.3.1

Rust SDK for the Google Gemini API and Vertex AI
Documentation
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
//! Files API surface.

use std::path::Path;
use std::sync::Arc;
use std::time::{Duration, Instant};

use reqwest::header::{HeaderMap, HeaderName, HeaderValue};

use crate::client::Credentials;
use crate::client::{Backend, ClientInner};
use crate::error::{Error, Result};
use crate::http_response::{
    sdk_http_response_from_headers, sdk_http_response_from_headers_and_body,
};
use crate::upload;
#[cfg(test)]
use crate::upload::CHUNK_SIZE;
use rust_genai_types::enums::FileState;
use rust_genai_types::files::{
    CreateFileConfig, CreateFileResponse, DeleteFileConfig, DeleteFileResponse, DownloadFileConfig,
    File, GetFileConfig, ListFilesConfig, ListFilesResponse, RegisterFilesConfig,
    RegisterFilesResponse, UploadFileConfig,
};
use serde_json::Value;

#[derive(Clone)]
pub struct Files {
    pub(crate) inner: Arc<ClientInner>,
}

impl Files {
    pub(crate) const fn new(inner: Arc<ClientInner>) -> Self {
        Self { inner }
    }

    /// 上传文件(直接上传字节数据)。
    ///
    /// # Errors
    /// 当配置无效、请求失败或响应解析失败时返回错误。
    pub async fn upload(&self, data: Vec<u8>, mime_type: impl Into<String>) -> Result<File> {
        let config = UploadFileConfig {
            mime_type: Some(mime_type.into()),
            ..UploadFileConfig::default()
        };
        self.upload_with_config(data, config).await
    }

    /// 初始化一个文件的 resumable upload,并返回原始 HTTP headers(含 `x-goog-upload-url`)。
    ///
    /// 该方法只执行 `start` 请求,不会上传文件内容。
    ///
    /// # Errors
    /// 当配置无效、请求失败或响应解析失败时返回错误。
    pub async fn create(&self, file: File) -> Result<CreateFileResponse> {
        self.create_with_config(file, CreateFileConfig::default())
            .await
    }

    /// 初始化一个文件的 resumable upload(自定义配置)。
    ///
    /// # Errors
    /// 当配置无效、请求失败或响应解析失败时返回错误。
    pub async fn create_with_config(
        &self,
        mut file: File,
        mut config: CreateFileConfig,
    ) -> Result<CreateFileResponse> {
        ensure_gemini_backend(&self.inner)?;

        let should_return_http_response = config.should_return_http_response.unwrap_or(false);
        let http_options = config.http_options.take();
        let mime_type = file.mime_type.clone().ok_or_else(|| Error::InvalidConfig {
            message: "mime_type is required when creating a resumable upload session".into(),
        })?;
        let size_bytes = file
            .size_bytes
            .as_deref()
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .map(|value| {
                value.parse::<u64>().map_err(|_| Error::InvalidConfig {
                    message: format!("Invalid size_bytes: {value}"),
                })
            })
            .transpose()?;

        if let Some(name) = file.name.take() {
            file.name = Some(normalize_upload_name(&name));
        }

        let (_, headers, text) = self
            .start_resumable_upload(file, size_bytes, &mime_type, None, http_options.as_ref())
            .await?;

        let response = CreateFileResponse {
            sdk_http_response: Some(if should_return_http_response {
                sdk_http_response_from_headers_and_body(&headers, text)
            } else {
                sdk_http_response_from_headers(&headers)
            }),
        };
        Ok(response)
    }

    /// 上传文件(自定义配置)。
    ///
    /// # Errors
    /// 当配置无效、请求失败或响应解析失败时返回错误。
    pub async fn upload_with_config(
        &self,
        data: Vec<u8>,
        mut config: UploadFileConfig,
    ) -> Result<File> {
        ensure_gemini_backend(&self.inner)?;

        let http_options = config.http_options.take();
        let mime_type = config
            .mime_type
            .clone()
            .ok_or_else(|| Error::InvalidConfig {
                message: "mime_type is required when uploading raw bytes".into(),
            })?;
        let size_bytes = data.len() as u64;
        let file = build_upload_file(config, size_bytes, &mime_type);
        let (upload_url, _, _) = self
            .start_resumable_upload(
                file,
                Some(size_bytes),
                &mime_type,
                None,
                http_options.as_ref(),
            )
            .await?;
        self.upload_bytes(&upload_url, &data, http_options.as_ref())
            .await
    }

    /// 从文件路径上传。
    ///
    /// # Errors
    /// 当文件无效、请求失败或响应解析失败时返回错误。
    pub async fn upload_from_path(&self, path: impl AsRef<Path>) -> Result<File> {
        self.upload_from_path_with_config(path, UploadFileConfig::default())
            .await
    }

    /// 从文件路径上传(自定义配置)。
    ///
    /// # Errors
    /// 当文件无效、请求失败或响应解析失败时返回错误。
    pub async fn upload_from_path_with_config(
        &self,
        path: impl AsRef<Path>,
        mut config: UploadFileConfig,
    ) -> Result<File> {
        ensure_gemini_backend(&self.inner)?;

        let path = path.as_ref();
        let metadata = tokio::fs::metadata(path).await?;
        if !metadata.is_file() {
            return Err(Error::InvalidConfig {
                message: format!("{} is not a valid file path", path.display()),
            });
        }

        let http_options = config.http_options.take();
        let size_bytes = metadata.len();
        let mime_type = config.mime_type.take().unwrap_or_else(|| {
            mime_guess::from_path(path)
                .first_or_octet_stream()
                .essence_str()
                .to_string()
        });

        let file_name = path.file_name().and_then(|name| name.to_str());
        let file = build_upload_file(config, size_bytes, &mime_type);
        let (upload_url, _, _) = self
            .start_resumable_upload(
                file,
                Some(size_bytes),
                &mime_type,
                file_name,
                http_options.as_ref(),
            )
            .await?;
        let mut file_handle = tokio::fs::File::open(path).await?;
        self.upload_reader(
            &upload_url,
            &mut file_handle,
            size_bytes,
            http_options.as_ref(),
        )
        .await
    }

    /// 下载 GENERATED 文件(返回字节内容)。
    ///
    /// Gemini API 当前只允许下载服务端生成的文件;上传得到的 `source=UPLOADED`
    /// 文件会返回 `INVALID_ARGUMENT`。
    ///
    /// # Errors
    /// 当请求失败或响应解析失败时返回错误。
    pub async fn download(&self, name_or_uri: impl AsRef<str>) -> Result<Vec<u8>> {
        self.download_with_config(name_or_uri, DownloadFileConfig::default())
            .await
    }

    /// 下载 GENERATED 文件(自定义配置)。
    ///
    /// # Errors
    /// 当请求失败或响应解析失败时返回错误。
    pub async fn download_with_config(
        &self,
        name_or_uri: impl AsRef<str>,
        mut config: DownloadFileConfig,
    ) -> Result<Vec<u8>> {
        ensure_gemini_backend(&self.inner)?;

        let http_options = config.http_options.take();
        let file_name = normalize_file_name(name_or_uri.as_ref())?;
        let url = build_file_download_url(&self.inner, &file_name, http_options.as_ref());
        let mut request = self.inner.http.get(url);
        request = apply_http_options(request, http_options.as_ref())?;
        let response = self
            .inner
            .send_with_http_options(request, http_options.as_ref())
            .await?;
        if !response.status().is_success() {
            return Err(Error::api_error_from_response(response, None).await);
        }
        let bytes = response.bytes().await?;
        Ok(bytes.to_vec())
    }

    /// 列出文件。
    ///
    /// # Errors
    /// 当请求失败或响应解析失败时返回错误。
    pub async fn list(&self) -> Result<ListFilesResponse> {
        self.list_with_config(ListFilesConfig::default()).await
    }

    /// 列出文件(自定义配置)。
    ///
    /// # Errors
    /// 当请求失败或响应解析失败时返回错误。
    pub async fn list_with_config(&self, config: ListFilesConfig) -> Result<ListFilesResponse> {
        ensure_gemini_backend(&self.inner)?;
        let http_options = config.http_options.as_ref();
        let url = build_files_list_url(&self.inner, &config, http_options)?;
        let mut request = self.inner.http.get(url);
        request = apply_http_options(request, http_options)?;
        let response = self
            .inner
            .send_with_http_options(request, http_options)
            .await?;
        if !response.status().is_success() {
            return Err(Error::api_error_from_response(response, None).await);
        }
        let headers = response.headers().clone();
        let mut result = response.json::<ListFilesResponse>().await?;
        result.sdk_http_response = Some(sdk_http_response_from_headers(&headers));
        Ok(result)
    }

    /// 列出所有文件(自动翻页)。
    ///
    /// # Errors
    /// 当请求失败或响应解析失败时返回错误。
    pub async fn all(&self) -> Result<Vec<File>> {
        self.all_with_config(ListFilesConfig::default()).await
    }

    /// 列出所有文件(带配置,自动翻页)。
    ///
    /// # Errors
    /// 当请求失败或响应解析失败时返回错误。
    pub async fn all_with_config(&self, mut config: ListFilesConfig) -> Result<Vec<File>> {
        let mut files = Vec::new();
        loop {
            let response = self.list_with_config(config.clone()).await?;
            if let Some(items) = response.files {
                files.extend(items);
            }
            match response.next_page_token {
                Some(token) if !token.is_empty() => {
                    config.page_token = Some(token);
                }
                _ => break,
            }
        }
        Ok(files)
    }

    /// 获取文件元数据。
    ///
    /// # Errors
    /// 当请求失败或响应解析失败时返回错误。
    pub async fn get(&self, name_or_uri: impl AsRef<str>) -> Result<File> {
        self.get_with_config(name_or_uri, GetFileConfig::default())
            .await
    }

    /// 获取文件元数据(自定义配置)。
    ///
    /// # Errors
    /// 当请求失败或响应解析失败时返回错误。
    pub async fn get_with_config(
        &self,
        name_or_uri: impl AsRef<str>,
        mut config: GetFileConfig,
    ) -> Result<File> {
        ensure_gemini_backend(&self.inner)?;

        let http_options = config.http_options.take();
        let file_name = normalize_file_name(name_or_uri.as_ref())?;
        let url = build_file_url(&self.inner, &file_name, http_options.as_ref());
        let mut request = self.inner.http.get(url);
        request = apply_http_options(request, http_options.as_ref())?;
        let response = self
            .inner
            .send_with_http_options(request, http_options.as_ref())
            .await?;
        if !response.status().is_success() {
            return Err(Error::api_error_from_response(response, None).await);
        }
        Ok(response.json::<File>().await?)
    }

    /// 删除文件。
    ///
    /// # Errors
    /// 当请求失败或响应解析失败时返回错误。
    pub async fn delete(&self, name_or_uri: impl AsRef<str>) -> Result<DeleteFileResponse> {
        self.delete_with_config(name_or_uri, DeleteFileConfig::default())
            .await
    }

    /// 删除文件(自定义配置)。
    ///
    /// # Errors
    /// 当请求失败或响应解析失败时返回错误。
    pub async fn delete_with_config(
        &self,
        name_or_uri: impl AsRef<str>,
        mut config: DeleteFileConfig,
    ) -> Result<DeleteFileResponse> {
        ensure_gemini_backend(&self.inner)?;

        let http_options = config.http_options.take();
        let file_name = normalize_file_name(name_or_uri.as_ref())?;
        let url = build_file_url(&self.inner, &file_name, http_options.as_ref());
        let mut request = self.inner.http.delete(url);
        request = apply_http_options(request, http_options.as_ref())?;
        let response = self
            .inner
            .send_with_http_options(request, http_options.as_ref())
            .await?;
        if !response.status().is_success() {
            return Err(Error::api_error_from_response(response, None).await);
        }
        let headers = response.headers().clone();
        let text = response.text().await.unwrap_or_default();
        let mut result = if text.trim().is_empty() {
            DeleteFileResponse::default()
        } else {
            serde_json::from_str::<DeleteFileResponse>(&text)?
        };
        result.sdk_http_response = Some(sdk_http_response_from_headers(&headers));
        Ok(result)
    }

    /// 注册 Google Cloud Storage 文件(使其可用于 Gemini Developer API)。
    ///
    /// 该方法要求客户端使用 OAuth/ADC(即 `Credentials::OAuth` 或 `Credentials::ApplicationDefault`),
    /// **不支持** API key 认证。
    ///
    /// # Errors
    /// 当配置无效、请求失败或响应解析失败时返回错误。
    pub async fn register_files(&self, uris: Vec<String>) -> Result<RegisterFilesResponse> {
        self.register_files_with_config(uris, RegisterFilesConfig::default())
            .await
    }

    /// 注册 Google Cloud Storage 文件(自定义配置)。
    ///
    /// # Errors
    /// 当配置无效、请求失败或响应解析失败时返回错误。
    pub async fn register_files_with_config(
        &self,
        uris: Vec<String>,
        mut config: RegisterFilesConfig,
    ) -> Result<RegisterFilesResponse> {
        ensure_gemini_backend(&self.inner)?;
        if matches!(self.inner.config.credentials, Credentials::ApiKey(_)) {
            return Err(Error::InvalidConfig {
                message: "register_files requires OAuth/ADC credentials, API key is not supported"
                    .into(),
            });
        }

        let should_return_http_response = config.should_return_http_response.unwrap_or(false);
        let http_options = config.http_options.take();
        let url = build_files_register_url(&self.inner, http_options.as_ref());
        let mut request = self
            .inner
            .http
            .post(url)
            .json(&serde_json::json!({ "uris": uris }));
        request = apply_http_options(request, http_options.as_ref())?;

        let response = self
            .inner
            .send_with_http_options(request, http_options.as_ref())
            .await?;
        if !response.status().is_success() {
            return Err(Error::api_error_from_response(response, None).await);
        }

        let headers = response.headers().clone();
        let text = response.text().await.unwrap_or_default();
        if should_return_http_response {
            return Ok(RegisterFilesResponse {
                sdk_http_response: Some(sdk_http_response_from_headers_and_body(&headers, text)),
                ..Default::default()
            });
        }
        if text.trim().is_empty() {
            return Ok(RegisterFilesResponse {
                sdk_http_response: Some(sdk_http_response_from_headers(&headers)),
                ..Default::default()
            });
        }
        let mut result: RegisterFilesResponse = serde_json::from_str(&text)?;
        result.sdk_http_response = Some(sdk_http_response_from_headers(&headers));
        Ok(result)
    }

    /// 轮询直到文件状态变为 ACTIVE。
    ///
    /// # Errors
    /// 当请求失败、文件失败或超时返回错误。
    pub async fn wait_for_active(
        &self,
        name_or_uri: impl AsRef<str>,
        config: WaitForFileConfig,
    ) -> Result<File> {
        ensure_gemini_backend(&self.inner)?;

        let start = Instant::now();
        loop {
            let file = self.get(name_or_uri.as_ref()).await?;
            match file.state {
                Some(FileState::Active) => return Ok(file),
                Some(FileState::Failed) => {
                    return Err(Error::api_error_with_retryable(
                        500,
                        "File processing failed",
                        false,
                    ));
                }
                _ => {}
            }

            if let Some(timeout) = config.timeout {
                if start.elapsed() >= timeout {
                    return Err(Error::Timeout {
                        message: "Timed out waiting for file to become ACTIVE".into(),
                    });
                }
            }

            tokio::time::sleep(config.poll_interval).await;
        }
    }

    async fn start_resumable_upload(
        &self,
        file: File,
        size_bytes: Option<u64>,
        mime_type: &str,
        file_name: Option<&str>,
        http_options: Option<&rust_genai_types::http::HttpOptions>,
    ) -> Result<(String, HeaderMap, String)> {
        let url = build_files_upload_url(&self.inner, http_options);
        let mut request = self.inner.http.post(url);
        request = apply_http_options(request, http_options)?;
        request = request
            .header("X-Goog-Upload-Protocol", "resumable")
            .header("X-Goog-Upload-Command", "start")
            .header("X-Goog-Upload-Header-Content-Type", mime_type);
        if let Some(size_bytes) = size_bytes {
            request = request.header(
                "X-Goog-Upload-Header-Content-Length",
                size_bytes.to_string(),
            );
        }

        if let Some(file_name) = file_name {
            request = request.header("X-Goog-Upload-File-Name", file_name);
        }

        let mut body = serde_json::json!({ "file": file });
        if let Some(options) = http_options {
            merge_extra_body(&mut body, options)?;
        }
        let request = request.json(&body);
        let response = self
            .inner
            .send_with_http_options(request, http_options)
            .await?;
        if !response.status().is_success() {
            return Err(Error::api_error_from_response(response, None).await);
        }

        let headers = response.headers().clone();
        let upload_url = headers
            .get("x-goog-upload-url")
            .and_then(|value| value.to_str().ok())
            .ok_or_else(|| Error::Parse {
                message: "Missing x-goog-upload-url header".into(),
            })?
            .to_string();
        let text = response.text().await.unwrap_or_default();

        Ok((upload_url, headers, text))
    }

    async fn upload_bytes(
        &self,
        upload_url: &str,
        data: &[u8],
        http_options: Option<&rust_genai_types::http::HttpOptions>,
    ) -> Result<File> {
        let validate_status = |status: &str| {
            if status != "active" {
                return Err(Error::Parse {
                    message: format!("Unexpected upload status: {status}"),
                });
            }
            Ok(())
        };

        upload::upload_bytes_with(
            data,
            |chunk, offset, finalize| {
                self.send_upload_chunk(upload_url, chunk, offset, finalize, http_options)
            },
            validate_status,
            "Upload finished without final response",
        )
        .await
    }

    async fn upload_reader(
        &self,
        upload_url: &str,
        reader: &mut tokio::fs::File,
        total_size: u64,
        http_options: Option<&rust_genai_types::http::HttpOptions>,
    ) -> Result<File> {
        let validate_status = |status: &str| {
            if status != "active" {
                return Err(Error::Parse {
                    message: format!("Unexpected upload status: {status}"),
                });
            }
            Ok(())
        };

        upload::upload_reader_with(
            reader,
            total_size,
            |chunk, offset, finalize| {
                self.send_upload_chunk(upload_url, chunk, offset, finalize, http_options)
            },
            validate_status,
            "Upload finished without final response",
        )
        .await
    }

    async fn send_upload_chunk(
        &self,
        upload_url: &str,
        chunk: Vec<u8>,
        offset: u64,
        finalize: bool,
        http_options: Option<&rust_genai_types::http::HttpOptions>,
    ) -> Result<(String, Option<File>)> {
        let command = if finalize {
            "upload, finalize"
        } else {
            "upload"
        };
        let chunk_len = chunk.len();
        let mut request = self.inner.http.post(upload_url);
        request = apply_http_options(request, http_options)?;
        let request = request
            .header("X-Goog-Upload-Command", command)
            .header("X-Goog-Upload-Offset", offset.to_string())
            .header("Content-Length", chunk_len.to_string())
            .body(chunk);

        let response = self
            .inner
            .send_with_http_options(request, http_options)
            .await?;

        if !response.status().is_success() {
            return Err(Error::api_error_from_response(response, None).await);
        }

        let upload_status = response
            .headers()
            .get("x-goog-upload-status")
            .and_then(|value| value.to_str().ok())
            .ok_or_else(|| Error::Parse {
                message: "Missing x-goog-upload-status header".into(),
            })?
            .to_string();

        let body = response.bytes().await?;
        if body.is_empty() {
            return Ok((upload_status, None));
        }

        let value: Value = serde_json::from_slice(&body)?;
        let file_value = value.get("file").cloned().unwrap_or(value);
        let file: File = serde_json::from_value(file_value)?;

        Ok((upload_status, Some(file)))
    }
}

#[derive(Debug, Clone)]
pub struct WaitForFileConfig {
    pub poll_interval: Duration,
    pub timeout: Option<Duration>,
}

impl Default for WaitForFileConfig {
    fn default() -> Self {
        Self {
            poll_interval: Duration::from_secs(2),
            timeout: Some(Duration::from_secs(300)),
        }
    }
}

#[cfg(test)]
fn finalize_upload(status: &str, file: Option<File>) -> Result<File> {
    upload::finalize_upload(status, file)
}

fn ensure_gemini_backend(inner: &ClientInner) -> Result<()> {
    if inner.config.backend == Backend::VertexAi {
        return Err(Error::InvalidConfig {
            message: "Files API is only supported in Gemini API".into(),
        });
    }
    Ok(())
}

fn build_upload_file(config: UploadFileConfig, size_bytes: u64, mime_type: &str) -> File {
    let mut file = File::default();
    if let Some(name) = config.name {
        file.name = Some(normalize_upload_name(&name));
    }
    file.display_name = config.display_name;
    file.mime_type = Some(mime_type.to_string());
    file.size_bytes = Some(size_bytes.to_string());
    file
}

fn normalize_upload_name(name: &str) -> String {
    if name.starts_with("files/") {
        name.to_string()
    } else {
        format!("files/{name}")
    }
}

fn normalize_file_name(value: &str) -> Result<String> {
    if value.starts_with("http://") || value.starts_with("https://") {
        let marker = "files/";
        let start = value.find(marker).ok_or_else(|| Error::InvalidConfig {
            message: format!("Could not find 'files/' in URI: {value}"),
        })?;
        let suffix = &value[start + marker.len()..];
        let name: String = suffix
            .chars()
            .take_while(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || *c == '-')
            .collect();
        if name.is_empty() {
            return Err(Error::InvalidConfig {
                message: format!("Could not extract file name from URI: {value}"),
            });
        }
        Ok(name)
    } else if value.starts_with("files/") {
        Ok(value.trim_start_matches("files/").to_string())
    } else {
        Ok(value.to_string())
    }
}

fn build_files_upload_url(
    inner: &ClientInner,
    http_options: Option<&rust_genai_types::http::HttpOptions>,
) -> String {
    let base = http_options
        .and_then(|opts| opts.base_url.as_deref())
        .unwrap_or(&inner.api_client.base_url);
    let version = http_options
        .and_then(|opts| opts.api_version.as_deref())
        .unwrap_or(&inner.api_client.api_version);
    format!("{base}upload/{version}/files")
}

fn build_files_list_url(
    inner: &ClientInner,
    config: &ListFilesConfig,
    http_options: Option<&rust_genai_types::http::HttpOptions>,
) -> Result<String> {
    let base = http_options
        .and_then(|opts| opts.base_url.as_deref())
        .unwrap_or(&inner.api_client.base_url);
    let version = http_options
        .and_then(|opts| opts.api_version.as_deref())
        .unwrap_or(&inner.api_client.api_version);
    let url = format!("{base}{version}/files");
    add_list_query_params(&url, config)
}

fn build_files_register_url(
    inner: &ClientInner,
    http_options: Option<&rust_genai_types::http::HttpOptions>,
) -> String {
    let base = http_options
        .and_then(|opts| opts.base_url.as_deref())
        .unwrap_or(&inner.api_client.base_url);
    let version = http_options
        .and_then(|opts| opts.api_version.as_deref())
        .unwrap_or(&inner.api_client.api_version);
    format!("{base}{version}/files:register")
}

fn build_file_url(
    inner: &ClientInner,
    name: &str,
    http_options: Option<&rust_genai_types::http::HttpOptions>,
) -> String {
    let base = http_options
        .and_then(|opts| opts.base_url.as_deref())
        .unwrap_or(&inner.api_client.base_url);
    let version = http_options
        .and_then(|opts| opts.api_version.as_deref())
        .unwrap_or(&inner.api_client.api_version);
    format!("{base}{version}/files/{name}")
}

fn build_file_download_url(
    inner: &ClientInner,
    name: &str,
    http_options: Option<&rust_genai_types::http::HttpOptions>,
) -> String {
    let base = http_options
        .and_then(|opts| opts.base_url.as_deref())
        .unwrap_or(&inner.api_client.base_url);
    let version = http_options
        .and_then(|opts| opts.api_version.as_deref())
        .unwrap_or(&inner.api_client.api_version);
    format!("{base}{version}/files/{name}:download?alt=media")
}

fn add_list_query_params(url: &str, config: &ListFilesConfig) -> Result<String> {
    let mut url = reqwest::Url::parse(url).map_err(|err| Error::InvalidConfig {
        message: err.to_string(),
    })?;
    {
        let mut pairs = url.query_pairs_mut();
        if let Some(page_size) = config.page_size {
            pairs.append_pair("pageSize", &page_size.to_string());
        }
        if let Some(page_token) = &config.page_token {
            pairs.append_pair("pageToken", page_token);
        }
    }
    Ok(url.to_string())
}

fn apply_http_options(
    mut request: reqwest::RequestBuilder,
    http_options: Option<&rust_genai_types::http::HttpOptions>,
) -> Result<reqwest::RequestBuilder> {
    if let Some(options) = http_options {
        if let Some(timeout) = options.timeout {
            request = request.timeout(Duration::from_millis(timeout));
        }
        if let Some(headers) = &options.headers {
            for (key, value) in headers {
                let name =
                    HeaderName::from_bytes(key.as_bytes()).map_err(|_| Error::InvalidConfig {
                        message: format!("Invalid header name: {key}"),
                    })?;
                let value = HeaderValue::from_str(value).map_err(|_| Error::InvalidConfig {
                    message: format!("Invalid header value for {key}"),
                })?;
                request = request.header(name, value);
            }
        }
    }
    Ok(request)
}

fn merge_extra_body(
    body: &mut Value,
    http_options: &rust_genai_types::http::HttpOptions,
) -> Result<()> {
    if let Some(extra) = &http_options.extra_body {
        match (body, extra) {
            (Value::Object(body_map), Value::Object(extra_map)) => {
                for (key, value) in extra_map {
                    body_map.insert(key.clone(), value.clone());
                }
            }
            (_, _) => {
                return Err(Error::InvalidConfig {
                    message: "HttpOptions.extra_body must be an object".into(),
                });
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::Client;
    use crate::test_support::test_client_inner;
    use serde_json::json;
    use std::collections::HashMap;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, Request, Respond, ResponseTemplate};

    #[test]
    fn test_normalize_file_name() {
        assert_eq!(normalize_file_name("files/abc-123").unwrap(), "abc-123");
        assert_eq!(normalize_file_name("abc-123").unwrap(), "abc-123");
        assert_eq!(
            normalize_file_name("https://example.com/files/abc-123?foo=bar").unwrap(),
            "abc-123"
        );
    }

    #[test]
    fn test_build_urls() {
        let client = Client::new("test-key").unwrap();
        let files = client.files();
        let url = build_files_upload_url(&files.inner, None);
        assert_eq!(
            url,
            "https://generativelanguage.googleapis.com/upload/v1beta/files"
        );
        let url = build_files_register_url(&files.inner, None);
        assert_eq!(
            url,
            "https://generativelanguage.googleapis.com/v1beta/files:register"
        );
    }

    #[test]
    fn test_normalize_upload_and_list_params() {
        assert_eq!(normalize_upload_name("files/abc"), "files/abc");
        assert_eq!(normalize_upload_name("abc"), "files/abc");
        assert!(normalize_file_name("https://example.com/no-files").is_err());
        assert!(normalize_file_name("https://example.com/files/?x").is_err());

        let url = add_list_query_params(
            "https://example.com/files",
            &ListFilesConfig {
                http_options: None,
                page_size: Some(3),
                page_token: Some("t".to_string()),
            },
        )
        .unwrap();
        assert!(url.contains("pageSize=3"));
        assert!(url.contains("pageToken=t"));
    }

    #[test]
    fn test_build_upload_file_and_finalize_errors() {
        let file = build_upload_file(
            UploadFileConfig {
                name: Some("abc".to_string()),
                display_name: Some("d".to_string()),
                ..Default::default()
            },
            5,
            "text/plain",
        );
        assert_eq!(file.name.as_deref(), Some("files/abc"));
        assert_eq!(file.size_bytes.as_deref(), Some("5"));

        let err = finalize_upload("active", None).unwrap_err();
        assert!(matches!(err, Error::Parse { .. }));
        let err = finalize_upload("final", None).unwrap_err();
        assert!(matches!(err, Error::Parse { .. }));

        let finalized = finalize_upload("final", Some(file.clone())).unwrap();
        assert_eq!(finalized.name, file.name);
    }

    #[test]
    fn test_ensure_gemini_backend_error() {
        let vertex = test_client_inner(Backend::VertexAi);
        let err = ensure_gemini_backend(&vertex).unwrap_err();
        assert!(matches!(err, Error::InvalidConfig { .. }));
    }

    #[tokio::test]
    async fn test_start_resumable_upload_and_send_chunk_errors() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/upload/v1beta/files"))
            .respond_with(ResponseTemplate::new(200))
            .mount(&server)
            .await;

        let client = Client::builder()
            .api_key("test-key")
            .base_url(server.uri())
            .build()
            .unwrap();
        let files = client.files();
        let file = build_upload_file(UploadFileConfig::default(), 1, "text/plain");
        let err = files
            .start_resumable_upload(file, Some(1), "text/plain", None, None)
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Parse { .. }));

        Mock::given(method("POST"))
            .and(path("/upload-chunk"))
            .respond_with(ResponseTemplate::new(200))
            .mount(&server)
            .await;
        let err = files
            .send_upload_chunk(
                &format!("{}/upload-chunk", server.uri()),
                Vec::new(),
                0,
                true,
                None,
            )
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Parse { .. }));

        Mock::given(method("POST"))
            .and(path("/upload-fail"))
            .respond_with(ResponseTemplate::new(400).set_body_string("bad"))
            .mount(&server)
            .await;
        let err = files
            .send_upload_chunk(
                &format!("{}/upload-fail", server.uri()),
                Vec::new(),
                0,
                true,
                None,
            )
            .await
            .unwrap_err();
        assert!(matches!(err, Error::ApiError { .. }));
    }

    #[tokio::test]
    async fn test_files_upload_errors() {
        let client = Client::new("test-key").unwrap();
        let files = client.files();

        let err = files
            .upload_with_config(vec![1, 2, 3], UploadFileConfig::default())
            .await
            .unwrap_err();
        assert!(matches!(err, Error::InvalidConfig { .. }));

        let temp_dir = std::env::temp_dir().join("rust_genai_files_test_dir");
        let _ = tokio::fs::create_dir_all(&temp_dir).await;
        let err = files
            .upload_from_path_with_config(&temp_dir, UploadFileConfig::default())
            .await
            .unwrap_err();
        assert!(matches!(err, Error::InvalidConfig { .. }));
        let _ = tokio::fs::remove_dir_all(&temp_dir).await;
    }

    #[tokio::test]
    async fn test_files_create_resumable_upload_sets_sdk_http_response() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/upload/v1beta/files"))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("x-goog-upload-url", format!("{}/upload-url", server.uri()))
                    .set_body_string("raw-body"),
            )
            .mount(&server)
            .await;

        let client = Client::builder()
            .api_key("test-key")
            .base_url(server.uri())
            .build()
            .unwrap();
        let files = client.files();
        let file = File {
            mime_type: Some("text/plain".to_string()),
            size_bytes: Some("3".to_string()),
            ..Default::default()
        };

        let response = files
            .create_with_config(
                file,
                CreateFileConfig {
                    http_options: Some(rust_genai_types::http::HttpOptions {
                        headers: Some(HashMap::from([("x-test".to_string(), "1".to_string())])),
                        extra_body: Some(json!({"extra": "value"})),
                        ..Default::default()
                    }),
                    should_return_http_response: Some(true),
                },
            )
            .await
            .unwrap();

        let sdk = response.sdk_http_response.unwrap();
        let headers = sdk.headers.unwrap();
        assert!(headers.contains_key("x-goog-upload-url"));
        assert_eq!(sdk.body.as_deref(), Some("raw-body"));

        let received = server.received_requests().await.unwrap();
        let body = String::from_utf8_lossy(&received[0].body);
        assert!(body.contains(r#""extra":"value""#));
        assert!(received[0].headers.get("x-test").is_some());
    }

    #[tokio::test]
    async fn test_start_resumable_upload_error_response() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/upload/v1beta/files"))
            .respond_with(ResponseTemplate::new(500).set_body_string("boom"))
            .mount(&server)
            .await;

        let client = Client::builder()
            .api_key("test-key")
            .base_url(server.uri())
            .build()
            .unwrap();
        let files = client.files();
        let file = build_upload_file(UploadFileConfig::default(), 1, "text/plain");
        let err = files
            .start_resumable_upload(file, Some(1), "text/plain", None, None)
            .await
            .unwrap_err();
        assert!(matches!(err, Error::ApiError { .. }));
    }

    #[tokio::test]
    async fn test_upload_bytes_empty_and_status_errors() {
        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/upload-empty"))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("x-goog-upload-status", "final")
                    .set_body_json(json!({
                        "file": {"name": "files/empty", "state": "ACTIVE"}
                    })),
            )
            .mount(&server)
            .await;

        Mock::given(method("POST"))
            .and(path("/upload-bad"))
            .respond_with(
                ResponseTemplate::new(200).insert_header("x-goog-upload-status", "paused"),
            )
            .mount(&server)
            .await;

        let client = Client::builder()
            .api_key("test-key")
            .base_url(server.uri())
            .build()
            .unwrap();
        let files = client.files();

        let file = files
            .upload_bytes(&format!("{}/upload-empty", server.uri()), &[], None)
            .await
            .unwrap();
        assert_eq!(file.name.as_deref(), Some("files/empty"));

        let data = vec![0u8; CHUNK_SIZE + 1];
        let err = files
            .upload_bytes(&format!("{}/upload-bad", server.uri()), &data, None)
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Parse { .. }));
    }

    #[tokio::test]
    async fn test_upload_reader_empty_file() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/upload-empty-file"))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("x-goog-upload-status", "final")
                    .set_body_json(json!({
                        "file": {"name": "files/empty-file", "state": "ACTIVE"}
                    })),
            )
            .mount(&server)
            .await;

        let client = Client::builder()
            .api_key("test-key")
            .base_url(server.uri())
            .build()
            .unwrap();
        let files = client.files();
        let temp_path = std::env::temp_dir().join("rust_genai_empty_upload_file");
        let _ = tokio::fs::write(&temp_path, &[]).await;
        let mut handle = tokio::fs::File::open(&temp_path).await.unwrap();

        let file = files
            .upload_reader(
                &format!("{}/upload-empty-file", server.uri()),
                &mut handle,
                0,
                None,
            )
            .await
            .unwrap();
        assert_eq!(file.name.as_deref(), Some("files/empty-file"));
        let _ = tokio::fs::remove_file(&temp_path).await;
    }

    #[tokio::test]
    async fn test_upload_bytes_and_reader_active_then_final() {
        #[derive(Clone)]
        struct UploadResponder;

        impl Respond for UploadResponder {
            fn respond(&self, request: &Request) -> ResponseTemplate {
                let finalize = request
                    .headers
                    .get("x-goog-upload-command")
                    .and_then(|value| value.to_str().ok())
                    .is_some_and(|value| value.contains("finalize"));
                if finalize {
                    ResponseTemplate::new(200)
                        .insert_header("x-goog-upload-status", "final")
                        .set_body_json(json!({
                            "file": {"name": "files/final", "state": "ACTIVE"}
                        }))
                } else {
                    ResponseTemplate::new(200).insert_header("x-goog-upload-status", "active")
                }
            }
        }

        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/upload-active"))
            .respond_with(UploadResponder)
            .mount(&server)
            .await;

        let client = Client::builder()
            .api_key("test-key")
            .base_url(server.uri())
            .build()
            .unwrap();
        let files = client.files();
        let data = vec![0u8; CHUNK_SIZE + 1];
        let file = files
            .upload_bytes(&format!("{}/upload-active", server.uri()), &data, None)
            .await
            .unwrap();
        assert_eq!(file.name.as_deref(), Some("files/final"));

        Mock::given(method("POST"))
            .and(path("/upload-reader"))
            .respond_with(UploadResponder)
            .mount(&server)
            .await;
        let temp_path = std::env::temp_dir().join("rust_genai_reader_active");
        let _ = tokio::fs::write(&temp_path, vec![0u8; CHUNK_SIZE + 1]).await;
        let mut handle = tokio::fs::File::open(&temp_path).await.unwrap();
        let file = files
            .upload_reader(
                &format!("{}/upload-reader", server.uri()),
                &mut handle,
                (CHUNK_SIZE + 1) as u64,
                None,
            )
            .await
            .unwrap();
        assert_eq!(file.name.as_deref(), Some("files/final"));
        let _ = tokio::fs::remove_file(&temp_path).await;
    }

    #[tokio::test]
    async fn test_upload_with_config_and_mime_guess() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/upload/v1beta/files"))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("x-goog-upload-url", format!("{}/upload-ok", server.uri())),
            )
            .mount(&server)
            .await;
        Mock::given(method("POST"))
            .and(path("/upload-ok"))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("x-goog-upload-status", "final")
                    .set_body_json(json!({
                        "file": {"name": "files/ok", "state": "ACTIVE"}
                    })),
            )
            .mount(&server)
            .await;

        let client = Client::builder()
            .api_key("test-key")
            .base_url(server.uri())
            .build()
            .unwrap();
        let files = client.files();
        let file = files.upload(vec![1, 2, 3], "text/plain").await.unwrap();
        assert_eq!(file.name.as_deref(), Some("files/ok"));

        let temp_path = std::env::temp_dir().join("rust_genai_upload_guess.txt");
        let _ = tokio::fs::write(&temp_path, b"hello").await;
        let file = files
            .upload_from_path_with_config(&temp_path, UploadFileConfig::default())
            .await
            .unwrap();
        assert_eq!(file.name.as_deref(), Some("files/ok"));
        let _ = tokio::fs::remove_file(&temp_path).await;
    }

    #[tokio::test]
    async fn test_wait_for_active_timeout_after_sleep() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1beta/files/slow"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "name": "files/slow",
                "state": "PROCESSING"
            })))
            .mount(&server)
            .await;

        let client = Client::builder()
            .api_key("test-key")
            .base_url(server.uri())
            .build()
            .unwrap();
        let files = client.files();
        let err = files
            .wait_for_active(
                "slow",
                WaitForFileConfig {
                    poll_interval: Duration::from_millis(1),
                    timeout: Some(Duration::from_millis(2)),
                },
            )
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Timeout { .. }));
    }

    #[test]
    fn test_add_list_query_params_invalid_url() {
        let err = add_list_query_params("http://[::1", &ListFilesConfig::default()).unwrap_err();
        assert!(matches!(err, Error::InvalidConfig { .. }));
    }
}