google-genai 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
google/genai/files.py ADDED
@@ -0,0 +1,1211 @@
1
+ # Copyright 2024 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+
16
+ import mimetypes
17
+ import os
18
+ from typing import Optional, Union
19
+ from urllib.parse import urlencode
20
+ from . import _common
21
+ from . import _transformers as t
22
+ from . import types
23
+ from ._api_client import ApiClient
24
+ from ._common import get_value_by_path as getv
25
+ from ._common import set_value_by_path as setv
26
+ from .pagers import AsyncPager, Pager
27
+
28
+
29
+ def _ListFilesConfig_to_mldev(
30
+ api_client: ApiClient,
31
+ from_object: Union[dict, object],
32
+ parent_object: dict = None,
33
+ ) -> dict:
34
+ to_object = {}
35
+ if getv(from_object, ['http_options']) is not None:
36
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
37
+
38
+ if getv(from_object, ['page_size']) is not None:
39
+ setv(
40
+ parent_object, ['_query', 'pageSize'], getv(from_object, ['page_size'])
41
+ )
42
+
43
+ if getv(from_object, ['page_token']) is not None:
44
+ setv(
45
+ parent_object,
46
+ ['_query', 'pageToken'],
47
+ getv(from_object, ['page_token']),
48
+ )
49
+
50
+ return to_object
51
+
52
+
53
+ def _ListFilesConfig_to_vertex(
54
+ api_client: ApiClient,
55
+ from_object: Union[dict, object],
56
+ parent_object: dict = None,
57
+ ) -> dict:
58
+ to_object = {}
59
+ if getv(from_object, ['http_options']) is not None:
60
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
61
+
62
+ if getv(from_object, ['page_size']) is not None:
63
+ setv(
64
+ parent_object, ['_query', 'pageSize'], getv(from_object, ['page_size'])
65
+ )
66
+
67
+ if getv(from_object, ['page_token']) is not None:
68
+ setv(
69
+ parent_object,
70
+ ['_query', 'pageToken'],
71
+ getv(from_object, ['page_token']),
72
+ )
73
+
74
+ return to_object
75
+
76
+
77
+ def _ListFilesParameters_to_mldev(
78
+ api_client: ApiClient,
79
+ from_object: Union[dict, object],
80
+ parent_object: dict = None,
81
+ ) -> dict:
82
+ to_object = {}
83
+ if getv(from_object, ['config']) is not None:
84
+ setv(
85
+ to_object,
86
+ ['config'],
87
+ _ListFilesConfig_to_mldev(
88
+ api_client, getv(from_object, ['config']), to_object
89
+ ),
90
+ )
91
+
92
+ return to_object
93
+
94
+
95
+ def _ListFilesParameters_to_vertex(
96
+ api_client: ApiClient,
97
+ from_object: Union[dict, object],
98
+ parent_object: dict = None,
99
+ ) -> dict:
100
+ to_object = {}
101
+ if getv(from_object, ['config']):
102
+ raise ValueError('config parameter is not supported in Vertex AI.')
103
+
104
+ return to_object
105
+
106
+
107
+ def _FileStatus_to_mldev(
108
+ api_client: ApiClient,
109
+ from_object: Union[dict, object],
110
+ parent_object: dict = None,
111
+ ) -> dict:
112
+ to_object = {}
113
+ if getv(from_object, ['details']) is not None:
114
+ setv(to_object, ['details'], getv(from_object, ['details']))
115
+
116
+ if getv(from_object, ['message']) is not None:
117
+ setv(to_object, ['message'], getv(from_object, ['message']))
118
+
119
+ if getv(from_object, ['code']) is not None:
120
+ setv(to_object, ['code'], getv(from_object, ['code']))
121
+
122
+ return to_object
123
+
124
+
125
+ def _FileStatus_to_vertex(
126
+ api_client: ApiClient,
127
+ from_object: Union[dict, object],
128
+ parent_object: dict = None,
129
+ ) -> dict:
130
+ to_object = {}
131
+ if getv(from_object, ['details']):
132
+ raise ValueError('details parameter is not supported in Vertex AI.')
133
+
134
+ if getv(from_object, ['message']):
135
+ raise ValueError('message parameter is not supported in Vertex AI.')
136
+
137
+ if getv(from_object, ['code']):
138
+ raise ValueError('code parameter is not supported in Vertex AI.')
139
+
140
+ return to_object
141
+
142
+
143
+ def _File_to_mldev(
144
+ api_client: ApiClient,
145
+ from_object: Union[dict, object],
146
+ parent_object: dict = None,
147
+ ) -> dict:
148
+ to_object = {}
149
+ if getv(from_object, ['name']) is not None:
150
+ setv(to_object, ['name'], getv(from_object, ['name']))
151
+
152
+ if getv(from_object, ['display_name']) is not None:
153
+ setv(to_object, ['displayName'], getv(from_object, ['display_name']))
154
+
155
+ if getv(from_object, ['mime_type']) is not None:
156
+ setv(to_object, ['mimeType'], getv(from_object, ['mime_type']))
157
+
158
+ if getv(from_object, ['size_bytes']) is not None:
159
+ setv(to_object, ['sizeBytes'], getv(from_object, ['size_bytes']))
160
+
161
+ if getv(from_object, ['create_time']) is not None:
162
+ setv(to_object, ['createTime'], getv(from_object, ['create_time']))
163
+
164
+ if getv(from_object, ['expiration_time']) is not None:
165
+ setv(to_object, ['expirationTime'], getv(from_object, ['expiration_time']))
166
+
167
+ if getv(from_object, ['update_time']) is not None:
168
+ setv(to_object, ['updateTime'], getv(from_object, ['update_time']))
169
+
170
+ if getv(from_object, ['sha256_hash']) is not None:
171
+ setv(to_object, ['sha256Hash'], getv(from_object, ['sha256_hash']))
172
+
173
+ if getv(from_object, ['uri']) is not None:
174
+ setv(to_object, ['uri'], getv(from_object, ['uri']))
175
+
176
+ if getv(from_object, ['state']) is not None:
177
+ setv(to_object, ['state'], getv(from_object, ['state']))
178
+
179
+ if getv(from_object, ['video_metadata']) is not None:
180
+ setv(to_object, ['videoMetadata'], getv(from_object, ['video_metadata']))
181
+
182
+ if getv(from_object, ['error']) is not None:
183
+ setv(
184
+ to_object,
185
+ ['error'],
186
+ _FileStatus_to_mldev(
187
+ api_client, getv(from_object, ['error']), to_object
188
+ ),
189
+ )
190
+
191
+ return to_object
192
+
193
+
194
+ def _File_to_vertex(
195
+ api_client: ApiClient,
196
+ from_object: Union[dict, object],
197
+ parent_object: dict = None,
198
+ ) -> dict:
199
+ to_object = {}
200
+ if getv(from_object, ['name']):
201
+ raise ValueError('name parameter is not supported in Vertex AI.')
202
+
203
+ if getv(from_object, ['display_name']):
204
+ raise ValueError('display_name parameter is not supported in Vertex AI.')
205
+
206
+ if getv(from_object, ['mime_type']):
207
+ raise ValueError('mime_type parameter is not supported in Vertex AI.')
208
+
209
+ if getv(from_object, ['size_bytes']):
210
+ raise ValueError('size_bytes parameter is not supported in Vertex AI.')
211
+
212
+ if getv(from_object, ['create_time']):
213
+ raise ValueError('create_time parameter is not supported in Vertex AI.')
214
+
215
+ if getv(from_object, ['expiration_time']):
216
+ raise ValueError('expiration_time parameter is not supported in Vertex AI.')
217
+
218
+ if getv(from_object, ['update_time']):
219
+ raise ValueError('update_time parameter is not supported in Vertex AI.')
220
+
221
+ if getv(from_object, ['sha256_hash']):
222
+ raise ValueError('sha256_hash parameter is not supported in Vertex AI.')
223
+
224
+ if getv(from_object, ['uri']):
225
+ raise ValueError('uri parameter is not supported in Vertex AI.')
226
+
227
+ if getv(from_object, ['state']):
228
+ raise ValueError('state parameter is not supported in Vertex AI.')
229
+
230
+ if getv(from_object, ['video_metadata']):
231
+ raise ValueError('video_metadata parameter is not supported in Vertex AI.')
232
+
233
+ if getv(from_object, ['error']):
234
+ raise ValueError('error parameter is not supported in Vertex AI.')
235
+
236
+ return to_object
237
+
238
+
239
+ def _CreateFileConfig_to_mldev(
240
+ api_client: ApiClient,
241
+ from_object: Union[dict, object],
242
+ parent_object: dict = None,
243
+ ) -> dict:
244
+ to_object = {}
245
+ if getv(from_object, ['http_options']) is not None:
246
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
247
+
248
+ return to_object
249
+
250
+
251
+ def _CreateFileConfig_to_vertex(
252
+ api_client: ApiClient,
253
+ from_object: Union[dict, object],
254
+ parent_object: dict = None,
255
+ ) -> dict:
256
+ to_object = {}
257
+ if getv(from_object, ['http_options']) is not None:
258
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
259
+
260
+ return to_object
261
+
262
+
263
+ def _CreateFileParameters_to_mldev(
264
+ api_client: ApiClient,
265
+ from_object: Union[dict, object],
266
+ parent_object: dict = None,
267
+ ) -> dict:
268
+ to_object = {}
269
+ if getv(from_object, ['file']) is not None:
270
+ setv(
271
+ to_object,
272
+ ['file'],
273
+ _File_to_mldev(api_client, getv(from_object, ['file']), to_object),
274
+ )
275
+
276
+ if getv(from_object, ['config']) is not None:
277
+ setv(
278
+ to_object,
279
+ ['config'],
280
+ _CreateFileConfig_to_mldev(
281
+ api_client, getv(from_object, ['config']), to_object
282
+ ),
283
+ )
284
+
285
+ return to_object
286
+
287
+
288
+ def _CreateFileParameters_to_vertex(
289
+ api_client: ApiClient,
290
+ from_object: Union[dict, object],
291
+ parent_object: dict = None,
292
+ ) -> dict:
293
+ to_object = {}
294
+ if getv(from_object, ['file']):
295
+ raise ValueError('file parameter is not supported in Vertex AI.')
296
+
297
+ if getv(from_object, ['config']):
298
+ raise ValueError('config parameter is not supported in Vertex AI.')
299
+
300
+ return to_object
301
+
302
+
303
+ def _GetFileConfig_to_mldev(
304
+ api_client: ApiClient,
305
+ from_object: Union[dict, object],
306
+ parent_object: dict = None,
307
+ ) -> dict:
308
+ to_object = {}
309
+ if getv(from_object, ['http_options']) is not None:
310
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
311
+
312
+ return to_object
313
+
314
+
315
+ def _GetFileConfig_to_vertex(
316
+ api_client: ApiClient,
317
+ from_object: Union[dict, object],
318
+ parent_object: dict = None,
319
+ ) -> dict:
320
+ to_object = {}
321
+ if getv(from_object, ['http_options']) is not None:
322
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
323
+
324
+ return to_object
325
+
326
+
327
+ def _GetFileParameters_to_mldev(
328
+ api_client: ApiClient,
329
+ from_object: Union[dict, object],
330
+ parent_object: dict = None,
331
+ ) -> dict:
332
+ to_object = {}
333
+ if getv(from_object, ['name']) is not None:
334
+ setv(
335
+ to_object,
336
+ ['_url', 'file'],
337
+ t.t_file_name(api_client, getv(from_object, ['name'])),
338
+ )
339
+
340
+ if getv(from_object, ['config']) is not None:
341
+ setv(
342
+ to_object,
343
+ ['config'],
344
+ _GetFileConfig_to_mldev(
345
+ api_client, getv(from_object, ['config']), to_object
346
+ ),
347
+ )
348
+
349
+ return to_object
350
+
351
+
352
+ def _GetFileParameters_to_vertex(
353
+ api_client: ApiClient,
354
+ from_object: Union[dict, object],
355
+ parent_object: dict = None,
356
+ ) -> dict:
357
+ to_object = {}
358
+ if getv(from_object, ['name']):
359
+ raise ValueError('name parameter is not supported in Vertex AI.')
360
+
361
+ if getv(from_object, ['config']):
362
+ raise ValueError('config parameter is not supported in Vertex AI.')
363
+
364
+ return to_object
365
+
366
+
367
+ def _DeleteFileConfig_to_mldev(
368
+ api_client: ApiClient,
369
+ from_object: Union[dict, object],
370
+ parent_object: dict = None,
371
+ ) -> dict:
372
+ to_object = {}
373
+ if getv(from_object, ['http_options']) is not None:
374
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
375
+
376
+ return to_object
377
+
378
+
379
+ def _DeleteFileConfig_to_vertex(
380
+ api_client: ApiClient,
381
+ from_object: Union[dict, object],
382
+ parent_object: dict = None,
383
+ ) -> dict:
384
+ to_object = {}
385
+ if getv(from_object, ['http_options']) is not None:
386
+ setv(to_object, ['httpOptions'], getv(from_object, ['http_options']))
387
+
388
+ return to_object
389
+
390
+
391
+ def _DeleteFileParameters_to_mldev(
392
+ api_client: ApiClient,
393
+ from_object: Union[dict, object],
394
+ parent_object: dict = None,
395
+ ) -> dict:
396
+ to_object = {}
397
+ if getv(from_object, ['name']) is not None:
398
+ setv(
399
+ to_object,
400
+ ['_url', 'file'],
401
+ t.t_file_name(api_client, getv(from_object, ['name'])),
402
+ )
403
+
404
+ if getv(from_object, ['config']) is not None:
405
+ setv(
406
+ to_object,
407
+ ['config'],
408
+ _DeleteFileConfig_to_mldev(
409
+ api_client, getv(from_object, ['config']), to_object
410
+ ),
411
+ )
412
+
413
+ return to_object
414
+
415
+
416
+ def _DeleteFileParameters_to_vertex(
417
+ api_client: ApiClient,
418
+ from_object: Union[dict, object],
419
+ parent_object: dict = None,
420
+ ) -> dict:
421
+ to_object = {}
422
+ if getv(from_object, ['name']):
423
+ raise ValueError('name parameter is not supported in Vertex AI.')
424
+
425
+ if getv(from_object, ['config']):
426
+ raise ValueError('config parameter is not supported in Vertex AI.')
427
+
428
+ return to_object
429
+
430
+
431
+ def _FileStatus_from_mldev(
432
+ api_client: ApiClient,
433
+ from_object: Union[dict, object],
434
+ parent_object: dict = None,
435
+ ) -> dict:
436
+ to_object = {}
437
+ if getv(from_object, ['details']) is not None:
438
+ setv(to_object, ['details'], getv(from_object, ['details']))
439
+
440
+ if getv(from_object, ['message']) is not None:
441
+ setv(to_object, ['message'], getv(from_object, ['message']))
442
+
443
+ if getv(from_object, ['code']) is not None:
444
+ setv(to_object, ['code'], getv(from_object, ['code']))
445
+
446
+ return to_object
447
+
448
+
449
+ def _FileStatus_from_vertex(
450
+ api_client: ApiClient,
451
+ from_object: Union[dict, object],
452
+ parent_object: dict = None,
453
+ ) -> dict:
454
+ to_object = {}
455
+
456
+ return to_object
457
+
458
+
459
+ def _File_from_mldev(
460
+ api_client: ApiClient,
461
+ from_object: Union[dict, object],
462
+ parent_object: dict = None,
463
+ ) -> dict:
464
+ to_object = {}
465
+ if getv(from_object, ['name']) is not None:
466
+ setv(to_object, ['name'], getv(from_object, ['name']))
467
+
468
+ if getv(from_object, ['displayName']) is not None:
469
+ setv(to_object, ['display_name'], getv(from_object, ['displayName']))
470
+
471
+ if getv(from_object, ['mimeType']) is not None:
472
+ setv(to_object, ['mime_type'], getv(from_object, ['mimeType']))
473
+
474
+ if getv(from_object, ['sizeBytes']) is not None:
475
+ setv(to_object, ['size_bytes'], getv(from_object, ['sizeBytes']))
476
+
477
+ if getv(from_object, ['createTime']) is not None:
478
+ setv(to_object, ['create_time'], getv(from_object, ['createTime']))
479
+
480
+ if getv(from_object, ['expirationTime']) is not None:
481
+ setv(to_object, ['expiration_time'], getv(from_object, ['expirationTime']))
482
+
483
+ if getv(from_object, ['updateTime']) is not None:
484
+ setv(to_object, ['update_time'], getv(from_object, ['updateTime']))
485
+
486
+ if getv(from_object, ['sha256Hash']) is not None:
487
+ setv(to_object, ['sha256_hash'], getv(from_object, ['sha256Hash']))
488
+
489
+ if getv(from_object, ['uri']) is not None:
490
+ setv(to_object, ['uri'], getv(from_object, ['uri']))
491
+
492
+ if getv(from_object, ['state']) is not None:
493
+ setv(to_object, ['state'], getv(from_object, ['state']))
494
+
495
+ if getv(from_object, ['videoMetadata']) is not None:
496
+ setv(to_object, ['video_metadata'], getv(from_object, ['videoMetadata']))
497
+
498
+ if getv(from_object, ['error']) is not None:
499
+ setv(
500
+ to_object,
501
+ ['error'],
502
+ _FileStatus_from_mldev(
503
+ api_client, getv(from_object, ['error']), to_object
504
+ ),
505
+ )
506
+
507
+ return to_object
508
+
509
+
510
+ def _File_from_vertex(
511
+ api_client: ApiClient,
512
+ from_object: Union[dict, object],
513
+ parent_object: dict = None,
514
+ ) -> dict:
515
+ to_object = {}
516
+
517
+ return to_object
518
+
519
+
520
+ def _ListFilesResponse_from_mldev(
521
+ api_client: ApiClient,
522
+ from_object: Union[dict, object],
523
+ parent_object: dict = None,
524
+ ) -> dict:
525
+ to_object = {}
526
+ if getv(from_object, ['nextPageToken']) is not None:
527
+ setv(to_object, ['next_page_token'], getv(from_object, ['nextPageToken']))
528
+
529
+ if getv(from_object, ['files']) is not None:
530
+ setv(
531
+ to_object,
532
+ ['files'],
533
+ [
534
+ _File_from_mldev(api_client, item, to_object)
535
+ for item in getv(from_object, ['files'])
536
+ ],
537
+ )
538
+
539
+ return to_object
540
+
541
+
542
+ def _ListFilesResponse_from_vertex(
543
+ api_client: ApiClient,
544
+ from_object: Union[dict, object],
545
+ parent_object: dict = None,
546
+ ) -> dict:
547
+ to_object = {}
548
+
549
+ return to_object
550
+
551
+
552
+ def _CreateFileResponse_from_mldev(
553
+ api_client: ApiClient,
554
+ from_object: Union[dict, object],
555
+ parent_object: dict = None,
556
+ ) -> dict:
557
+ to_object = {}
558
+
559
+ return to_object
560
+
561
+
562
+ def _CreateFileResponse_from_vertex(
563
+ api_client: ApiClient,
564
+ from_object: Union[dict, object],
565
+ parent_object: dict = None,
566
+ ) -> dict:
567
+ to_object = {}
568
+
569
+ return to_object
570
+
571
+
572
+ def _DeleteFileResponse_from_mldev(
573
+ api_client: ApiClient,
574
+ from_object: Union[dict, object],
575
+ parent_object: dict = None,
576
+ ) -> dict:
577
+ to_object = {}
578
+
579
+ return to_object
580
+
581
+
582
+ def _DeleteFileResponse_from_vertex(
583
+ api_client: ApiClient,
584
+ from_object: Union[dict, object],
585
+ parent_object: dict = None,
586
+ ) -> dict:
587
+ to_object = {}
588
+
589
+ return to_object
590
+
591
+
592
+ class Files(_common.BaseModule):
593
+
594
+ def _list(
595
+ self, *, config: Optional[types.ListFilesConfigOrDict] = None
596
+ ) -> types.ListFilesResponse:
597
+ """Lists all files from the service.
598
+
599
+ Args:
600
+ config (ListFilesConfig): Optional, configuration for the list method.
601
+
602
+ Returns:
603
+ ListFilesResponse: The response for the list method.
604
+
605
+ Usage:
606
+
607
+ .. code-block:: python
608
+ pager = client.files.list(config={'page_size': 10})
609
+ for file in pager.page:
610
+ print(file.name)
611
+ """
612
+
613
+ parameter_model = types._ListFilesParameters(
614
+ config=config,
615
+ )
616
+
617
+ if self.api_client.vertexai:
618
+ raise ValueError('This method is only supported in the default client.')
619
+ else:
620
+ request_dict = _ListFilesParameters_to_mldev(
621
+ self.api_client, parameter_model
622
+ )
623
+ path = 'files'.format_map(request_dict.get('_url'))
624
+
625
+ query_params = request_dict.get('_query')
626
+ if query_params:
627
+ path = f'{path}?{urlencode(query_params)}'
628
+ # TODO: remove the hack that pops config.
629
+ config = request_dict.pop('config', None)
630
+ http_options = config.pop('httpOptions', None) if config else None
631
+ request_dict = _common.convert_to_dict(request_dict)
632
+ request_dict = _common.apply_base64_encoding(request_dict)
633
+
634
+ response_dict = self.api_client.request(
635
+ 'get', path, request_dict, http_options
636
+ )
637
+
638
+ if self.api_client.vertexai:
639
+ response_dict = _ListFilesResponse_from_vertex(
640
+ self.api_client, response_dict
641
+ )
642
+ else:
643
+ response_dict = _ListFilesResponse_from_mldev(
644
+ self.api_client, response_dict
645
+ )
646
+
647
+ return_value = types.ListFilesResponse._from_response(
648
+ response_dict, parameter_model
649
+ )
650
+ self.api_client._verify_response(return_value)
651
+ return return_value
652
+
653
+ def _create(
654
+ self,
655
+ *,
656
+ file: types.FileOrDict,
657
+ config: Optional[types.CreateFileConfigOrDict] = None,
658
+ ) -> types.CreateFileResponse:
659
+ parameter_model = types._CreateFileParameters(
660
+ file=file,
661
+ config=config,
662
+ )
663
+
664
+ if self.api_client.vertexai:
665
+ raise ValueError('This method is only supported in the default client.')
666
+ else:
667
+ request_dict = _CreateFileParameters_to_mldev(
668
+ self.api_client, parameter_model
669
+ )
670
+ path = 'upload/v1beta/files'.format_map(request_dict.get('_url'))
671
+
672
+ query_params = request_dict.get('_query')
673
+ if query_params:
674
+ path = f'{path}?{urlencode(query_params)}'
675
+ # TODO: remove the hack that pops config.
676
+ config = request_dict.pop('config', None)
677
+ http_options = config.pop('httpOptions', None) if config else None
678
+ request_dict = _common.convert_to_dict(request_dict)
679
+ request_dict = _common.apply_base64_encoding(request_dict)
680
+
681
+ response_dict = self.api_client.request(
682
+ 'post', path, request_dict, http_options
683
+ )
684
+
685
+ if self.api_client.vertexai:
686
+ response_dict = _CreateFileResponse_from_vertex(
687
+ self.api_client, response_dict
688
+ )
689
+ else:
690
+ response_dict = _CreateFileResponse_from_mldev(
691
+ self.api_client, response_dict
692
+ )
693
+
694
+ return_value = types.CreateFileResponse._from_response(
695
+ response_dict, parameter_model
696
+ )
697
+ self.api_client._verify_response(return_value)
698
+ return return_value
699
+
700
+ def get(
701
+ self, *, name: str, config: Optional[types.GetFileConfigOrDict] = None
702
+ ) -> types.File:
703
+ """Retrieves the file information from the service.
704
+
705
+ Args:
706
+ name (str): The name identifier for the file to retrieve.
707
+ config (GetFileConfig): Optional, configuration for the get method.
708
+
709
+ Returns:
710
+ File: The file information.
711
+
712
+ Usage:
713
+
714
+ .. code-block:: python
715
+ file = client.files.get(name='files/...')
716
+ print(file.uri)
717
+ """
718
+
719
+ parameter_model = types._GetFileParameters(
720
+ name=name,
721
+ config=config,
722
+ )
723
+
724
+ if self.api_client.vertexai:
725
+ raise ValueError('This method is only supported in the default client.')
726
+ else:
727
+ request_dict = _GetFileParameters_to_mldev(
728
+ self.api_client, parameter_model
729
+ )
730
+ path = 'files/{file}'.format_map(request_dict.get('_url'))
731
+
732
+ query_params = request_dict.get('_query')
733
+ if query_params:
734
+ path = f'{path}?{urlencode(query_params)}'
735
+ # TODO: remove the hack that pops config.
736
+ config = request_dict.pop('config', None)
737
+ http_options = config.pop('httpOptions', None) if config else None
738
+ request_dict = _common.convert_to_dict(request_dict)
739
+ request_dict = _common.apply_base64_encoding(request_dict)
740
+
741
+ response_dict = self.api_client.request(
742
+ 'get', path, request_dict, http_options
743
+ )
744
+
745
+ if self.api_client.vertexai:
746
+ response_dict = _File_from_vertex(self.api_client, response_dict)
747
+ else:
748
+ response_dict = _File_from_mldev(self.api_client, response_dict)
749
+
750
+ return_value = types.File._from_response(response_dict, parameter_model)
751
+ self.api_client._verify_response(return_value)
752
+ return return_value
753
+
754
+ def delete(
755
+ self, *, name: str, config: Optional[types.DeleteFileConfigOrDict] = None
756
+ ) -> types.DeleteFileResponse:
757
+ """Deletes an existing file from the service.
758
+
759
+ Args:
760
+ name (str): The name identifier for the file to delete.
761
+ config (DeleteFileConfig): Optional, configuration for the delete method.
762
+
763
+ Returns:
764
+ DeleteFileResponse: The response for the delete method
765
+
766
+ Usage:
767
+
768
+ .. code-block:: python
769
+ client.files.delete(name='files/...')
770
+ """
771
+
772
+ parameter_model = types._DeleteFileParameters(
773
+ name=name,
774
+ config=config,
775
+ )
776
+
777
+ if self.api_client.vertexai:
778
+ raise ValueError('This method is only supported in the default client.')
779
+ else:
780
+ request_dict = _DeleteFileParameters_to_mldev(
781
+ self.api_client, parameter_model
782
+ )
783
+ path = 'files/{file}'.format_map(request_dict.get('_url'))
784
+
785
+ query_params = request_dict.get('_query')
786
+ if query_params:
787
+ path = f'{path}?{urlencode(query_params)}'
788
+ # TODO: remove the hack that pops config.
789
+ config = request_dict.pop('config', None)
790
+ http_options = config.pop('httpOptions', None) if config else None
791
+ request_dict = _common.convert_to_dict(request_dict)
792
+ request_dict = _common.apply_base64_encoding(request_dict)
793
+
794
+ response_dict = self.api_client.request(
795
+ 'delete', path, request_dict, http_options
796
+ )
797
+
798
+ if self.api_client.vertexai:
799
+ response_dict = _DeleteFileResponse_from_vertex(
800
+ self.api_client, response_dict
801
+ )
802
+ else:
803
+ response_dict = _DeleteFileResponse_from_mldev(
804
+ self.api_client, response_dict
805
+ )
806
+
807
+ return_value = types.DeleteFileResponse._from_response(
808
+ response_dict, parameter_model
809
+ )
810
+ self.api_client._verify_response(return_value)
811
+ return return_value
812
+
813
+ def upload(
814
+ self,
815
+ *,
816
+ path: str,
817
+ config: Optional[types.UploadFileConfigOrDict] = None,
818
+ ) -> types.File:
819
+ """Calls the API to upload a file using a supported file service.
820
+
821
+ Args:
822
+ path: The path to the file or a file-like object (e.g. `BytesIO`) to be
823
+ uploaded.
824
+ config: Optional parameters to set `diplay_name`, `mime_type`, and `name`.
825
+ """
826
+ if self.api_client.vertexai:
827
+ raise ValueError(
828
+ 'Vertex AI does not support creating files. You can upload files to'
829
+ ' GCS files instead.'
830
+ )
831
+ config_model = None
832
+ if config:
833
+ if isinstance(config, dict):
834
+ config_model = types.UploadFileConfig(**config)
835
+ else:
836
+ config_model = config
837
+ file = types.File(
838
+ mime_type=config_model.mime_type,
839
+ name=config_model.name,
840
+ display_name=config_model.display_name,
841
+ )
842
+ else: # if not config
843
+ file = types.File()
844
+ if file.name is not None and not file.name.startswith('files/'):
845
+ file.name = f'files/{file.name}'
846
+
847
+ fs_path = os.fspath(path)
848
+ if not fs_path or not os.path.isfile(fs_path):
849
+ raise FileNotFoundError(f'{path} is not a valid file path.')
850
+ file.size_bytes = os.path.getsize(fs_path)
851
+ if file.mime_type is None:
852
+ file.mime_type, _ = mimetypes.guess_type(fs_path)
853
+ if file.mime_type is None:
854
+ raise ValueError(
855
+ 'Unknown mime type: Could not determine the mimetype for your file\n'
856
+ ' please set the `mime_type` argument'
857
+ )
858
+ response = {}
859
+ if config_model and config_model.http_options:
860
+ http_options = config_model.http_options
861
+ else:
862
+ http_options = {
863
+ 'api_version': '', # api-version is set in the path.
864
+ 'headers': {
865
+ 'Content-Type': 'application/json',
866
+ 'X-Goog-Upload-Protocol': 'resumable',
867
+ 'X-Goog-Upload-Command': 'start',
868
+ 'X-Goog-Upload-Header-Content-Length': f'{file.size_bytes}',
869
+ 'X-Goog-Upload-Header-Content-Type': f'{file.mime_type}',
870
+ },
871
+ 'response_payload': response,
872
+ }
873
+ self._create(file=file, config={'http_options': http_options})
874
+ if (
875
+ 'headers' not in response
876
+ or 'X-Goog-Upload-URL' not in response['headers']
877
+ ):
878
+ raise KeyError(
879
+ 'Failed to create file. Upload URL did not returned from the create'
880
+ ' file request.'
881
+ )
882
+ upload_url = response['headers']['X-Goog-Upload-URL']
883
+
884
+ return_file = self.api_client.upload_file(
885
+ fs_path, upload_url, file.size_bytes
886
+ )
887
+
888
+ return types.File._from_response(
889
+ _File_from_mldev(self.api_client, return_file['file']), None
890
+ )
891
+
892
+ def list(
893
+ self, *, config: Optional[types.ListFilesConfigOrDict] = None
894
+ ) -> Pager[types.File]:
895
+ return Pager(
896
+ 'files',
897
+ self._list,
898
+ self._list(config=config),
899
+ config,
900
+ )
901
+
902
+
903
+ class AsyncFiles(_common.BaseModule):
904
+
905
+ async def _list(
906
+ self, *, config: Optional[types.ListFilesConfigOrDict] = None
907
+ ) -> types.ListFilesResponse:
908
+ """Lists all files from the service.
909
+
910
+ Args:
911
+ config (ListFilesConfig): Optional, configuration for the list method.
912
+
913
+ Returns:
914
+ ListFilesResponse: The response for the list method.
915
+
916
+ Usage:
917
+
918
+ .. code-block:: python
919
+ pager = client.files.list(config={'page_size': 10})
920
+ for file in pager.page:
921
+ print(file.name)
922
+ """
923
+
924
+ parameter_model = types._ListFilesParameters(
925
+ config=config,
926
+ )
927
+
928
+ if self.api_client.vertexai:
929
+ raise ValueError('This method is only supported in the default client.')
930
+ else:
931
+ request_dict = _ListFilesParameters_to_mldev(
932
+ self.api_client, parameter_model
933
+ )
934
+ path = 'files'.format_map(request_dict.get('_url'))
935
+
936
+ query_params = request_dict.get('_query')
937
+ if query_params:
938
+ path = f'{path}?{urlencode(query_params)}'
939
+ # TODO: remove the hack that pops config.
940
+ config = request_dict.pop('config', None)
941
+ http_options = config.pop('httpOptions', None) if config else None
942
+ request_dict = _common.convert_to_dict(request_dict)
943
+ request_dict = _common.apply_base64_encoding(request_dict)
944
+
945
+ response_dict = await self.api_client.async_request(
946
+ 'get', path, request_dict, http_options
947
+ )
948
+
949
+ if self.api_client.vertexai:
950
+ response_dict = _ListFilesResponse_from_vertex(
951
+ self.api_client, response_dict
952
+ )
953
+ else:
954
+ response_dict = _ListFilesResponse_from_mldev(
955
+ self.api_client, response_dict
956
+ )
957
+
958
+ return_value = types.ListFilesResponse._from_response(
959
+ response_dict, parameter_model
960
+ )
961
+ self.api_client._verify_response(return_value)
962
+ return return_value
963
+
964
+ async def _create(
965
+ self,
966
+ *,
967
+ file: types.FileOrDict,
968
+ config: Optional[types.CreateFileConfigOrDict] = None,
969
+ ) -> types.CreateFileResponse:
970
+ parameter_model = types._CreateFileParameters(
971
+ file=file,
972
+ config=config,
973
+ )
974
+
975
+ if self.api_client.vertexai:
976
+ raise ValueError('This method is only supported in the default client.')
977
+ else:
978
+ request_dict = _CreateFileParameters_to_mldev(
979
+ self.api_client, parameter_model
980
+ )
981
+ path = 'upload/v1beta/files'.format_map(request_dict.get('_url'))
982
+
983
+ query_params = request_dict.get('_query')
984
+ if query_params:
985
+ path = f'{path}?{urlencode(query_params)}'
986
+ # TODO: remove the hack that pops config.
987
+ config = request_dict.pop('config', None)
988
+ http_options = config.pop('httpOptions', None) if config else None
989
+ request_dict = _common.convert_to_dict(request_dict)
990
+ request_dict = _common.apply_base64_encoding(request_dict)
991
+
992
+ response_dict = await self.api_client.async_request(
993
+ 'post', path, request_dict, http_options
994
+ )
995
+
996
+ if self.api_client.vertexai:
997
+ response_dict = _CreateFileResponse_from_vertex(
998
+ self.api_client, response_dict
999
+ )
1000
+ else:
1001
+ response_dict = _CreateFileResponse_from_mldev(
1002
+ self.api_client, response_dict
1003
+ )
1004
+
1005
+ return_value = types.CreateFileResponse._from_response(
1006
+ response_dict, parameter_model
1007
+ )
1008
+ self.api_client._verify_response(return_value)
1009
+ return return_value
1010
+
1011
+ async def get(
1012
+ self, *, name: str, config: Optional[types.GetFileConfigOrDict] = None
1013
+ ) -> types.File:
1014
+ """Retrieves the file information from the service.
1015
+
1016
+ Args:
1017
+ name (str): The name identifier for the file to retrieve.
1018
+ config (GetFileConfig): Optional, configuration for the get method.
1019
+
1020
+ Returns:
1021
+ File: The file information.
1022
+
1023
+ Usage:
1024
+
1025
+ .. code-block:: python
1026
+ file = client.files.get(name='files/...')
1027
+ print(file.uri)
1028
+ """
1029
+
1030
+ parameter_model = types._GetFileParameters(
1031
+ name=name,
1032
+ config=config,
1033
+ )
1034
+
1035
+ if self.api_client.vertexai:
1036
+ raise ValueError('This method is only supported in the default client.')
1037
+ else:
1038
+ request_dict = _GetFileParameters_to_mldev(
1039
+ self.api_client, parameter_model
1040
+ )
1041
+ path = 'files/{file}'.format_map(request_dict.get('_url'))
1042
+
1043
+ query_params = request_dict.get('_query')
1044
+ if query_params:
1045
+ path = f'{path}?{urlencode(query_params)}'
1046
+ # TODO: remove the hack that pops config.
1047
+ config = request_dict.pop('config', None)
1048
+ http_options = config.pop('httpOptions', None) if config else None
1049
+ request_dict = _common.convert_to_dict(request_dict)
1050
+ request_dict = _common.apply_base64_encoding(request_dict)
1051
+
1052
+ response_dict = await self.api_client.async_request(
1053
+ 'get', path, request_dict, http_options
1054
+ )
1055
+
1056
+ if self.api_client.vertexai:
1057
+ response_dict = _File_from_vertex(self.api_client, response_dict)
1058
+ else:
1059
+ response_dict = _File_from_mldev(self.api_client, response_dict)
1060
+
1061
+ return_value = types.File._from_response(response_dict, parameter_model)
1062
+ self.api_client._verify_response(return_value)
1063
+ return return_value
1064
+
1065
+ async def delete(
1066
+ self, *, name: str, config: Optional[types.DeleteFileConfigOrDict] = None
1067
+ ) -> types.DeleteFileResponse:
1068
+ """Deletes an existing file from the service.
1069
+
1070
+ Args:
1071
+ name (str): The name identifier for the file to delete.
1072
+ config (DeleteFileConfig): Optional, configuration for the delete method.
1073
+
1074
+ Returns:
1075
+ DeleteFileResponse: The response for the delete method
1076
+
1077
+ Usage:
1078
+
1079
+ .. code-block:: python
1080
+ client.files.delete(name='files/...')
1081
+ """
1082
+
1083
+ parameter_model = types._DeleteFileParameters(
1084
+ name=name,
1085
+ config=config,
1086
+ )
1087
+
1088
+ if self.api_client.vertexai:
1089
+ raise ValueError('This method is only supported in the default client.')
1090
+ else:
1091
+ request_dict = _DeleteFileParameters_to_mldev(
1092
+ self.api_client, parameter_model
1093
+ )
1094
+ path = 'files/{file}'.format_map(request_dict.get('_url'))
1095
+
1096
+ query_params = request_dict.get('_query')
1097
+ if query_params:
1098
+ path = f'{path}?{urlencode(query_params)}'
1099
+ # TODO: remove the hack that pops config.
1100
+ config = request_dict.pop('config', None)
1101
+ http_options = config.pop('httpOptions', None) if config else None
1102
+ request_dict = _common.convert_to_dict(request_dict)
1103
+ request_dict = _common.apply_base64_encoding(request_dict)
1104
+
1105
+ response_dict = await self.api_client.async_request(
1106
+ 'delete', path, request_dict, http_options
1107
+ )
1108
+
1109
+ if self.api_client.vertexai:
1110
+ response_dict = _DeleteFileResponse_from_vertex(
1111
+ self.api_client, response_dict
1112
+ )
1113
+ else:
1114
+ response_dict = _DeleteFileResponse_from_mldev(
1115
+ self.api_client, response_dict
1116
+ )
1117
+
1118
+ return_value = types.DeleteFileResponse._from_response(
1119
+ response_dict, parameter_model
1120
+ )
1121
+ self.api_client._verify_response(return_value)
1122
+ return return_value
1123
+
1124
+ async def upload(
1125
+ self,
1126
+ *,
1127
+ path: str,
1128
+ config: Optional[types.UploadFileConfigOrDict] = None,
1129
+ ) -> types.File:
1130
+ """Calls the API to upload a file asynchronously using a supported file service.
1131
+
1132
+ Args:
1133
+ path: The path to the file or a file-like object (e.g. `BytesIO`) to be
1134
+ uploaded.
1135
+ config: Optional parameters to set `diplay_name`, `mime_type`, and `name`.
1136
+ """
1137
+ if self.api_client.vertexai:
1138
+ raise ValueError(
1139
+ 'Vertex AI does not support creating files. You can upload files to'
1140
+ ' GCS files instead.'
1141
+ )
1142
+ config_model = None
1143
+ if config:
1144
+ if isinstance(config, dict):
1145
+ config_model = types.UploadFileConfig(**config)
1146
+ else:
1147
+ config_model = config
1148
+ file = types.File(
1149
+ mime_type=config_model.mime_type,
1150
+ name=config_model.name,
1151
+ display_name=config_model.display_name,
1152
+ )
1153
+ else: # if not config
1154
+ file = types.File()
1155
+ if file.name is not None and not file.name.startswith('files/'):
1156
+ file.name = f'files/{file.name}'
1157
+
1158
+ fs_path = os.fspath(path)
1159
+ if not fs_path or not os.path.isfile(fs_path):
1160
+ raise FileNotFoundError(f'{path} is not a valid file path.')
1161
+ file.size_bytes = os.path.getsize(fs_path)
1162
+ if file.mime_type is None:
1163
+ file.mime_type, _ = mimetypes.guess_type(fs_path)
1164
+ if file.mime_type is None:
1165
+ raise ValueError(
1166
+ 'Unknown mime type: Could not determine the mimetype for your file\n'
1167
+ ' please set the `mime_type` argument'
1168
+ )
1169
+ response = {}
1170
+ if config_model and config_model.http_options:
1171
+ http_options = config_model.http_options
1172
+ else:
1173
+ http_options = {
1174
+ 'api_version': '', # api-version is set in the path.
1175
+ 'headers': {
1176
+ 'Content-Type': 'application/json',
1177
+ 'X-Goog-Upload-Protocol': 'resumable',
1178
+ 'X-Goog-Upload-Command': 'start',
1179
+ 'X-Goog-Upload-Header-Content-Length': f'{file.size_bytes}',
1180
+ 'X-Goog-Upload-Header-Content-Type': f'{file.mime_type}',
1181
+ },
1182
+ 'response_payload': response,
1183
+ }
1184
+ await self._create(file=file, config={'http_options': http_options})
1185
+ if (
1186
+ 'headers' not in response
1187
+ or 'X-Goog-Upload-URL' not in response['headers']
1188
+ ):
1189
+ raise KeyError(
1190
+ 'Failed to create file. Upload URL did not returned from the create'
1191
+ ' file request.'
1192
+ )
1193
+ upload_url = response['headers']['X-Goog-Upload-URL']
1194
+
1195
+ return_file = await self.api_client.async_upload_file(
1196
+ fs_path, upload_url, file.size_bytes
1197
+ )
1198
+
1199
+ return types.File._from_response(
1200
+ _File_from_mldev(self.api_client, return_file['file']), None
1201
+ )
1202
+
1203
+ async def list(
1204
+ self, *, config: Optional[types.ListFilesConfigOrDict] = None
1205
+ ) -> AsyncPager[types.File]:
1206
+ return AsyncPager(
1207
+ 'files',
1208
+ self._list,
1209
+ await self._list(config=config),
1210
+ config,
1211
+ )