-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfiles.py
More file actions
1037 lines (885 loc) · 37.4 KB
/
files.py
File metadata and controls
1037 lines (885 loc) · 37.4 KB
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
from urllib.parse import urlparse
from xml.dom.minidom import parseString
from functools import wraps
import warnings
from typing import Union, Dict, Any, Optional, Literal
import requests
from pythonik.constants import (
GCS_KEYFRAME_LOCATION_KEY,
GCS_UPLOADID_KEY,
S3_UPLOADID_KEY,
)
from pythonik.exceptions import UnexpectedStorageMethodForProxy
from pythonik.models.base import Response, StorageMethod, PaginatedResponse
from pythonik.models.files.file import (
File,
FileSetsFilesResponse,
Files,
FileSet,
FileSets,
FileCreate,
FileSetCreate,
S3MultipartUploadResponse,
)
from pythonik.models.files.keyframe import (
Keyframe,
Keyframes,
GCSKeyframeUploadResponse,
)
from pythonik.models.files.proxy import Proxies, Proxy
from pythonik.specs.base import Spec, PythonikResponse
from pythonik.models.files.storage import Storage, Storages
from pythonik.models.files.format import Component, Formats, Format, FormatCreate
GET_ASSET_PROXY_PATH = "assets/{}/proxies/{}/"
GET_ASSET_PROXIES_PATH = "assets/{}/proxies/"
GET_ASSET_PROXIES_MULTIPART_URL_PATH = GET_ASSET_PROXY_PATH + "multipart_url/part/"
GET_ASSET_PROXIES_MULTIPART_COMPLETE_URL_PATH = GET_ASSET_PROXY_PATH + "multipart_url/"
GET_ASSETS_FORMATS_PATH = "assets/{}/formats/"
GET_ASSETS_FORMAT_PATH = "assets/{}/formats/{}/"
GET_ASSETS_FORMAT_COMPONENTS_PATH = "assets/{}/formats/{}/components"
GET_ASSETS_FILES_PATH = "assets/{}/files/"
GET_ASSETS_FILE_SETS_PATH = "assets/{}/file_sets/"
GET_ASSETS_FILE_SET_FILES_PATH = "assets/{}/file_sets/{}/files/"
GET_STORAGE_PATH = "storages/{}/"
GET_STORAGES_PATH = "storages/"
GET_ASSET_KEYFRAME = "assets/{}/keyframes/{}/"
GET_ASSET_KEYFRAMES = "assets/{}/keyframes/"
GET_ASSETS_FILE_PATH = "assets/{}/files/{}/"
DELETE_ASSETS_FILE_SET_PATH = "assets/{}/file_sets/{}/"
DELETE_ASSETS_FILE_PATH = "assets/{}/files/{}/"
GET_ASSETS_VERSION_FILE_SETS_PATH = "assets/{}/versions/{}/file_sets/"
GET_ASSETS_VERSION_FILES_PATH = "assets/{}/versions/{}/files/"
GET_ASSETS_VERSION_FORMATS_PATH = "assets/{}/versions/{}/formats/"
class FilesSpec(Spec):
server = "API/files/"
def create_asset_format_component(
self,
asset_id: str,
format_id: str,
body: Union[Component, Dict[str, Any]],
exclude_defaults: bool = True,
**kwargs,
) -> Response:
"""
Create a new format component
Args:
asset_id: ID of the asset
format_id: ID for the format
body: Component creation parameters, either as Component model or dict
exclude_defaults: Whether to exclude default values when dumping Pydantic models
**kwargs: Additional kwargs to pass to the request
Returns:
Response(model=Formats)
Required roles:
- can_write_formats
Raises:
400 Bad request
401 Token is invalid
404 Formats for this asset don't exist
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._post(
GET_ASSETS_FORMAT_COMPONENTS_PATH.format(asset_id, format_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, Formats)
def delete_asset_file(self, asset_id: str, file_id: str, **kwargs) -> Response:
"""Delete a specific file from an asset
Args:
asset_id: The ID of the asset
file_id: The ID of the file to delete
**kwargs: Additional kwargs to pass to the request
Returns:
Response with no data model
"""
response = self._delete(DELETE_ASSETS_FILE_PATH.format(asset_id, file_id), **kwargs)
return self.parse_response(response, model=None)
def delete_asset_file_set(
self, asset_id: str, file_set_id: str, keep_source: bool = False, **kwargs
) -> Response:
"""Delete asset's file set, file entries, and actual files
Args:
asset_id: The ID of the asset
file_set_id: The ID of the file set to delete
keep_source: If true, keep source objects
Returns:
Response with FileSet model if status code is 200 (file set marked as deleted)
Response with no data model if status code is 204 (immediate deletion)
"""
params = {"keep_source": keep_source} if keep_source else None
response = self._delete(
DELETE_ASSETS_FILE_SET_PATH.format(asset_id, file_set_id),
params=params,
**kwargs
)
# If status is 204, return response with no model
if response.status_code == 204:
return self.parse_response(response, model=None)
# If status is possibly 200, return response with FileSet model
return self.parse_response(response, FileSet)
def delete_asset_keyframe(self, asset_id: str, keyframe_id: str, **kwargs):
response = self._delete(GET_ASSET_KEYFRAME.format(asset_id, keyframe_id), **kwargs)
return self.parse_response(response, model=None)
def get_asset_file(self, asset_id: str, file_id: str, **kwargs) -> Response:
"""Get metadata for a specific file associated with an asset
Args:
asset_id: The ID of the asset
file_id: The ID of the file to retrieve
**kwargs: Additional arguments to pass to the request
Returns:
Response with File model
"""
resp = self._get(GET_ASSETS_FILE_PATH.format(asset_id, file_id), **kwargs)
return self.parse_response(resp, File)
def get_asset_file_set_files(self, asset_id: str, file_sets_id: str, **kwargs) -> Response:
"""
Retrieve files for a specific file set
"""
response = self._get(GET_ASSETS_FILE_SET_FILES_PATH.format(asset_id, file_sets_id), **kwargs)
return self.parse_response(response, FileSetsFilesResponse)
def get_asset_keyframe(self, asset_id: str, keyframe_id: str, **kwargs) -> Response:
"""Get a specific keyframe for an asset
Args:
asset_id: The ID of the asset
keyframe_id: The ID of the keyframe to retrieve
Returns:
Response with Keyframe model
"""
response = self._get(GET_ASSET_KEYFRAME.format(asset_id, keyframe_id), **kwargs)
return self.parse_response(response, Keyframe)
def get_asset_keyframes(self, asset_id: str, **kwargs) -> Keyframes:
"""Get all keyframes for an asset
Args:
asset_id: The ID of the asset
Returns:
Response containing list of Keyframes
"""
response = self._get(GET_ASSET_KEYFRAMES.format(asset_id), **kwargs)
return self.parse_response(response, Keyframes)
def create_asset_keyframe(
self, asset_id: str, body: Union[Keyframe, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""Create a new keyframe for an asset
Args:
asset_id: The ID of the asset
body: Keyframe object containing the keyframe data, either as Keyframe model or dict
exclude_defaults: Whether to exclude default values when dumping Pydantic models
**kwargs: Additional arguments to pass to the request
Returns:
Response with created Keyframe model
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._post(
GET_ASSET_KEYFRAMES.format(asset_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, Keyframe)
def get_asset_proxy(self, asset_id: str, proxy_id: str, **kwargs) -> Response:
"""Get asset's proxy
Returns: Response(model=Proxy)
"""
resp = self._get(GET_ASSET_PROXY_PATH.format(asset_id, proxy_id), **kwargs)
return self.parse_response(resp, Proxy)
def update_asset_proxy(
self, asset_id: str, proxy_id: str, body: Union[Proxy, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""
Update asset's proxy
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._patch(
GET_ASSET_PROXY_PATH.format(asset_id, proxy_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, Proxy)
def create_asset_proxy(
self, asset_id: str, body: Union[Proxy, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""
Create proxy and associate to asset
Returns: Response(model=Proxy)
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._post(
GET_ASSET_PROXIES_PATH.format(asset_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, Proxy)
def partial_update_keyframe(
self,
asset_id: str,
keyframe_id: str,
body: Union[Keyframe, Dict[str, Any]],
exclude_defaults: bool = True,
**kwargs,
) -> Response:
"""Partially update an asset keyframe using PATCH"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._patch(
GET_ASSET_KEYFRAME.format(asset_id, keyframe_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, Keyframe)
def update_keyframe(
self,
asset_id: str,
keyframe_id: str,
body: Union[Keyframe, Dict[str, Any]],
exclude_defaults: bool = True,
**kwargs,
) -> Response:
"""Update an asset keyframe using POST"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._post(
GET_ASSET_KEYFRAME.format(asset_id, keyframe_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, Keyframe)
def get_upload_id_for_keyframe(self, keyframe: Keyframe) -> PythonikResponse:
"""
Get upload ID for keyframe. This ID is required to upload keyframe files.
:return: PythonikResponse
:raises UnexpectedStorageMethodForProxy: When keyframe exists on an unsupported storage method (i.e. Pythonik cannot
automatically determine the upload ID)
"""
headers = {"Origin": self.base_url, "Referer": self.base_url}
if keyframe.storage_method == StorageMethod.S3:
upload_url = keyframe.multipart_upload_url
headers = {"Host": urlparse(upload_url).netloc, **headers}
elif keyframe.storage_method == StorageMethod.GCS:
upload_url = keyframe.upload_url
headers = {"X-Goog-Resumable": "start", **headers}
else:
# escape hatch
supported_methods = [StorageMethod.S3, StorageMethod.GCS]
raise UnexpectedStorageMethodForProxy(
f"Unexpected storage method: {keyframe.storage_method}."
f" Pythonik supports {supported_methods}."
)
upload_url_response = requests.post(upload_url, headers=headers)
if not upload_url_response.ok:
return PythonikResponse(response=upload_url_response, data=None)
if keyframe.storage_method == StorageMethod.S3:
raise NotImplementedError(
"Pythonik does not currently support creating keyframes on S3"
)
xml = parseString(upload_url_response.text)
key = xml.getElementsByTagName("Key")[0].firstChild.nodeValue
bucket = xml.getElementsByTagName("Bucket")[0].firstChild.nodeValue
upload_id = xml.getElementsByTagName(S3_UPLOADID_KEY)[
0
].firstChild.nodeValue
data = upload_id
elif keyframe.storage_method == StorageMethod.GCS:
upload_id = upload_url_response.headers[GCS_UPLOADID_KEY]
location = upload_url_response.headers[GCS_KEYFRAME_LOCATION_KEY]
data = GCSKeyframeUploadResponse(upload_id=upload_id, location=location)
else:
supported_methods = [StorageMethod.S3, StorageMethod.GCS]
raise UnexpectedStorageMethodForProxy(
f"Unexpected storage method: {keyframe.storage_method}."
f" Pythonik supports {supported_methods}."
)
return PythonikResponse(response=upload_url_response, data=data)
def get_upload_id_for_proxy(self, asset_id: str, proxy_id: str) -> PythonikResponse:
"""
Get upload ID for proxy. This ID is required to upload proxy files.
:param asset_id: Asset ID
:param proxy_id: Proxy ID
:return: PythonikResponse
:raises UnexpectedStorageMethodForProxy: When prox exists on an unsupported storage method (i.e. Pythonik cannot
automatically determine the upload ID)
"""
proxy_response = self.get_asset_proxy(asset_id, proxy_id)
if not proxy_response.response.ok:
# bubble up the error for caller to handle
return proxy_response
proxy = proxy_response.data
headers = {"Origin": self.base_url, "Referer": self.base_url}
if proxy.storage_method == StorageMethod.S3:
upload_url = proxy.multipart_upload_url
headers = {"Host": urlparse(upload_url).netloc, **headers}
elif proxy.storage_method == StorageMethod.GCS:
upload_url = proxy.upload_url
headers = {"X-Goog-Resumable": "start", **headers}
else:
# escape hatch
supported_methods = [StorageMethod.S3, StorageMethod.GCS]
raise UnexpectedStorageMethodForProxy(
f"Unexpected storage method: {proxy.storage_method}."
f" pythonik supports {supported_methods}."
)
upload_url_response = requests.post(upload_url, headers=headers)
if not upload_url_response.ok:
return PythonikResponse(response=upload_url_response, data=None)
if proxy.storage_method == StorageMethod.S3:
xml = parseString(upload_url_response.text)
# key = xml.getElementsByTagName("Key")[0].firstChild.nodeValue
# bucket = xml.getElementsByTagName("Bucket")[0].firstChild.nodeValue
upload_id = xml.getElementsByTagName(S3_UPLOADID_KEY)[
0
].firstChild.nodeValue
elif proxy.storage_method == StorageMethod.GCS:
upload_id = upload_url_response.headers[GCS_UPLOADID_KEY]
else:
supported_methods = [StorageMethod.S3, StorageMethod.GCS]
raise UnexpectedStorageMethodForProxy(
f"Unexpected storage method: {proxy.storage_method}."
f" pythonik supports {supported_methods}."
)
return PythonikResponse(response=upload_url_response, data=upload_id)
def get_s3_presigned_url(
self, asset_id: str, proxy_id: str, upload_id: str, part_number: int,
**kwargs
) -> PythonikResponse:
"""
Get a singed part URL to upload a proxy.
:param asset_id: Asset ID
:param proxy_id: Proxy ID
:param upload_id: Upload ID
:param part_number: Upload part number
:return: PythonikResponse
"""
response = self._get(
path=GET_ASSET_PROXIES_MULTIPART_URL_PATH.format(asset_id, proxy_id),
params={"upload_id": upload_id, "parts_num": part_number},
**kwargs
)
if not response.ok:
return PythonikResponse(response=response, data=None)
return self.parse_response(response, S3MultipartUploadResponse)
def get_s3_complete_url(
self, asset_id: str, proxy_id: str, upload_id: str, **kwargs
) -> PythonikResponse:
response = self._get(
GET_ASSET_PROXIES_MULTIPART_COMPLETE_URL_PATH.format(asset_id, proxy_id),
params={"upload_id": upload_id, "type": "complete_url"},
**kwargs
)
if not response.ok:
return PythonikResponse(response=response, data=None)
return PythonikResponse(response=response, data=response.json()["complete_url"])
def get_asset_proxies(self, asset_id: str, **kwargs) -> PythonikResponse:
resp = self._get(GET_ASSET_PROXIES_PATH.format(asset_id), **kwargs)
return self.parse_response(resp, Proxies)
def create_asset_format(
self, asset_id: str, body: Union[FormatCreate, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""
Create format and associate it to asset
Returns: Response(model=Format)
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._post(
GET_ASSETS_FORMATS_PATH.format(asset_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, Format)
def create_asset_file(
self, asset_id: str, body: Union[FileCreate, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""
Create file and associate to asset
Returns: Response(model=File)
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._post(
GET_ASSETS_FILES_PATH.format(asset_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, File)
def create_asset_filesets(
self, asset_id: str, body: Union[FileSetCreate, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
warnings.warn(
"'create_asset_filesets' is deprecated. Use 'create_asset_file_sets' instead.",
DeprecationWarning,
stacklevel=2
)
return self.create_asset_file_sets(asset_id, body, exclude_defaults, **kwargs)
def create_asset_file_sets(
self, asset_id: str, body: Union[FileSetCreate, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""
Create file sets and associate it to asset
Returns: Response(model=FileSet)
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._post(
GET_ASSETS_FILE_SETS_PATH.format(asset_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, FileSet)
def get_asset_file_sets_by_version(
self, asset_id: str, version_id: str, per_page: int = None, last_id: str = None, file_count: bool = None, **kwargs
) -> Response:
"""
Get all asset's file sets by version
Args:
asset_id: ID of the asset
version_id: ID of the version
per_page: The number of items for each page
last_id: ID of a last file set on previous page
file_count: Set to true if you need a total amount of files in a file set
**kwargs: Additional kwargs to pass to the request
Returns:
Response(model=FileSets)
Required roles:
- can_read_files
Raises:
401 Token is invalid
404 FileSets for this asset don't exist
"""
params = {}
if per_page is not None:
params["per_page"] = per_page
if last_id is not None:
params["last_id"] = last_id
if file_count is not None:
params["file_count"] = file_count
response = self._get(
GET_ASSETS_VERSION_FILE_SETS_PATH.format(asset_id, version_id),
params=params,
**kwargs,
)
return self.parse_response(response, FileSets)
def get_asset_filesets(self, asset_id: str, **kwargs) -> Response:
"""Get all file sets associated with an asset
Args:
asset_id: The ID of the asset
**kwargs: Additional arguments to pass to the request
Returns:
Response containing list of FileSets
"""
resp = self._get(GET_ASSETS_FILE_SETS_PATH.format(asset_id), **kwargs)
return self.parse_response(resp, FileSets)
def get_asset_formats(self, asset_id: str, **kwargs) -> Response:
"""Get all formats associated with an asset
Args:
asset_id: The ID of the asset
**kwargs: Additional arguments to pass to the request
Returns:
Response containing list of Formats
"""
resp = self._get(GET_ASSETS_FORMATS_PATH.format(asset_id), **kwargs)
return self.parse_response(resp, Formats)
def get_asset_format(self, asset_id: str, format_id: str, **kwargs) -> Response:
"""Get a specific format for an asset
Args:
asset_id: The ID of the asset
format_id: The ID of the format to retrieve
**kwargs: Additional arguments to pass to the request
Returns:
Response with Format model
"""
resp = self._get(GET_ASSETS_FORMAT_PATH.format(asset_id, format_id), **kwargs)
return self.parse_response(resp, Format)
def get_asset_files(self, asset_id: str, **kwargs) -> Response:
"""Get all files associated with an asset
Args:
asset_id: The ID of the asset
**kwargs: Additional arguments to pass to the request
Returns:
Response containing list of Files
"""
resp = self._get(GET_ASSETS_FILES_PATH.format(asset_id), **kwargs)
return self.parse_response(resp, Files)
def get_storage(self, storage_id: str, **kwargs):
"""Get metadata for a specific storage
Args:
storage_id: The ID of the storage to retrieve
**kwargs: Additional arguments to pass to the request
Returns:
Response with Storage model
"""
resp = self._get(GET_STORAGE_PATH.format(storage_id), **kwargs)
return self.parse_response(resp, Storage)
def get_storages(self, **kwargs):
"""Get metadata for all available storages
Args:
**kwargs: Additional arguments to pass to the request
Returns:
Response containing list of Storages
"""
resp = self._get(GET_STORAGES_PATH, **kwargs)
return self.parse_response(resp, Storages)
def update_asset_format(
self, asset_id: str, format_id: str, body: Union[FormatCreate, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""
Update format information for an asset using PUT
Args:
asset_id: ID of the asset
format_id: ID of the format to update
body: Format update parameters, either as FormatCreate model or dict
exclude_defaults: Whether to exclude default values when dumping Pydantic models
**kwargs: Additional kwargs to pass to the request
Returns:
Response(model=Format)
Required roles:
- can_write_formats
Raises:
400 Bad request
401 Token is invalid
404 Format for this asset doesn't exist
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._put(
GET_ASSETS_FORMAT_PATH.format(asset_id, format_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, Format)
def partial_update_asset_format(
self, asset_id: str, format_id: str, body: Union[FormatCreate, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""
Partially update format information for an asset using PATCH
Args:
asset_id: ID of the asset
format_id: ID of the format to update
body: Format update parameters, either as FormatCreate model or dict
exclude_defaults: Whether to exclude default values when dumping Pydantic models
**kwargs: Additional kwargs to pass to the request
Returns:
Response(model=Format)
Required roles:
- can_write_formats
Raises:
400 Bad request
401 Token is invalid
404 Format for this asset doesn't exist
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._patch(
GET_ASSETS_FORMAT_PATH.format(asset_id, format_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, Format)
def update_asset_file_set(
self, asset_id: str, file_set_id: str, body: Union[FileSetCreate, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""
Update file set information for an asset using PUT
Args:
asset_id: ID of the asset
file_set_id: ID of the file set to update
body: File set update parameters, either as FileSetCreate model or dict
exclude_defaults: Whether to exclude default values when dumping Pydantic models
**kwargs: Additional kwargs to pass to the request
Returns:
Response(model=FileSet)
Required roles:
- can_write_files
Raises:
400 Bad request
401 Token is invalid
404 File set for this asset doesn't exist
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._put(
GET_ASSETS_FILE_SETS_PATH.format(asset_id, file_set_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, FileSet)
def partial_update_asset_file_set(
self, asset_id: str, file_set_id: str, body: Union[FileSetCreate, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""
Partially update file set information for an asset using PATCH
Args:
asset_id: ID of the asset
file_set_id: ID of the file set to update
body: File set update parameters, either as FileSetCreate model or dict
exclude_defaults: Whether to exclude default values when dumping Pydantic models
**kwargs: Additional kwargs to pass to the request
Returns:
Response(model=FileSet)
Required roles:
- can_write_files
Raises:
400 Bad request
401 Token is invalid
404 File set for this asset doesn't exist
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._patch(
GET_ASSETS_FILE_SETS_PATH.format(asset_id, file_set_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, FileSet)
def update_asset_file(
self, asset_id: str, file_id: str, body: Union[FileCreate, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""
Update file information for an asset using PUT
Args:
asset_id: ID of the asset
file_id: ID of the file to update
body: File update parameters, either as FileCreate model or dict
exclude_defaults: Whether to exclude default values when dumping Pydantic models
**kwargs: Additional kwargs to pass to the request
Returns:
Response(model=File)
Required roles:
- can_write_files
Raises:
400 Bad request
401 Token is invalid
404 File for this asset doesn't exist
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._put(
GET_ASSETS_FILE_PATH.format(asset_id, file_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, File)
def partial_update_asset_file(
self, asset_id: str, file_id: str, body: Union[FileCreate, Dict[str, Any]], exclude_defaults: bool = True, **kwargs
) -> Response:
"""
Partially update file information for an asset using PATCH
Args:
asset_id: ID of the asset
file_id: ID of the file to update
body: File update parameters, either as FileCreate model or dict
exclude_defaults: Whether to exclude default values when dumping Pydantic models
**kwargs: Additional kwargs to pass to the request
Returns:
Response(model=File)
Required roles:
- can_write_files
Raises:
400 Bad request
401 Token is invalid
404 File for this asset doesn't exist
"""
json_data = self._prepare_model_data(body, exclude_defaults=exclude_defaults)
response = self._patch(
GET_ASSETS_FILE_PATH.format(asset_id, file_id),
json=json_data,
**kwargs,
)
return self.parse_response(response, File)
def get_asset_formats_by_version(
self, asset_id: str, version_id: str, per_page: int = None, last_id: str = None, **kwargs
) -> Response:
"""
Get all asset's formats by version
Args:
asset_id: ID of the asset
version_id: ID of the version
per_page: The number of items for each page
last_id: ID of a last format on previous page
**kwargs: Additional kwargs to pass to the request
Returns:
Response(model=Formats)
Required roles:
- can_read_formats
Raises:
401 Token is invalid
404 Formats for this asset don't exist
"""
params = {}
if per_page is not None:
params["per_page"] = per_page
if last_id is not None:
params["last_id"] = last_id
response = self._get(
GET_ASSETS_VERSION_FORMATS_PATH.format(asset_id, version_id),
params=params,
**kwargs,
)
return self.parse_response(response, Formats)
def get_asset_files_by_version(
self, asset_id: str, version_id: str, per_page: int = None, last_id: str = None,
generate_signed_url: bool = None, content_disposition: str = None, **kwargs
) -> Response:
"""
Get all asset's files by version
Args:
asset_id: ID of the asset
version_id: ID of the version
per_page: The number of items for each page
last_id: ID of a last file on previous page
generate_signed_url: Set to False if you do not need a URL, will slow things down otherwise
content_disposition: Set to attachment if you want a download link. Note that this will not create a download in asset history
**kwargs: Additional kwargs to pass to the request
Returns:
Response(model=Files)
Required roles:
- can_read_files
Raises:
401 Token is invalid
404 Files for this asset don't exist
"""
params = {}
if per_page is not None:
params["per_page"] = per_page
if last_id is not None:
params["last_id"] = last_id
if generate_signed_url is not None:
params["generate_signed_url"] = generate_signed_url
if content_disposition is not None:
params["content_disposition"] = content_disposition
response = self._get(
GET_ASSETS_VERSION_FILES_PATH.format(asset_id, version_id),
params=params,
**kwargs,
)
return self.parse_response(response, Files)
def create_storage_file(
self,
storage_id: str,
body: Union[File, Dict[str, Any]],
exclude_defaults: bool = True,
**kwargs
) -> Response:
"""
Create file without associating it to an asset.
Args:
storage_id: The ID of the storage to retrieve
body: Storage file creation parameters, either as File model or dict
exclude_defaults: Whether to exclude default values when dumping
Pydantic models
**kwargs: Additional arguments to pass to the request
Returns:
Response(model=PaginatedResponse) with created file information
"""
json_data = self._prepare_model_data(
body, exclude_defaults=exclude_defaults
)
resp = self._post(
self.gen_url(f"storages/{storage_id}/files/"),
json=json_data,
**kwargs
)
return self.parse_response(resp, PaginatedResponse)
def list_asset_format_components(
self, asset_id: str, format_id: str, **kwargs
) -> Response:
"""
Get all components for a format in an asset.
Args:
asset_id: The ID of the asset
format_id: The ID of the format to retrieve
**kwargs: Additional arguments to pass to the request
Returns:
Response(model=PaginatedResponse) containing format components
"""
resp = self._get(
self.gen_url(f"assets/{asset_id}/formats/{format_id}/components/"),
**kwargs
)
return self.parse_response(resp, PaginatedResponse)
def list_storage_files(self, storage_id: str, **kwargs) -> Response:
"""
Get all files on a storage, or files in a storage folder.
Args:
storage_id: The ID of the storage to retrieve
**kwargs: Additional arguments to pass to the request
Returns:
Response(model=PaginatedResponse) containing storage files
"""
resp = self._get(
self.gen_url(f"storages/{storage_id}/files/"), **kwargs
)
return self.parse_response(resp, PaginatedResponse)
def _get_deleted_object_type(
self, object_type: Literal["file_sets", "formats"], **kwargs
) -> Response:
"""
Get deleted object type.
Args:
object_type: The type of object to retrieve
**kwargs: Additional arguments to pass to the request
Returns:
Response(model=None) containing deleted objects
Raises:
ValueError: If object_type is not 'file_sets' or 'formats'
"""
if object_type not in ["file_sets", "formats"]:
raise ValueError("object_type must be one of file_sets or formats")
resp = self._get(self.gen_url(f"delete_queue/{object_type}/"), **kwargs)
return self.parse_response(resp, None)
def get_deleted_file_sets(self, **kwargs) -> Response:
"""
Get deleted file sets.
Args:
**kwargs: Additional arguments to pass to the request
Returns:
Response(model=None) containing deleted file sets
"""
return self._get_deleted_object_type("file_sets", **kwargs)
# Create method alias
get_deleted_filesets = get_deleted_file_sets
def get_deleted_formats(self, **kwargs) -> Response:
"""
Get deleted formats.
Args:
**kwargs: Additional arguments to pass to the request
Returns:
Response(model=None) containing deleted formats
"""
return self._get_deleted_object_type("formats", **kwargs)
def create_mediainfo_job(
self,
asset_id: str,
file_id: str,
priority: Optional[int] = 5,
**kwargs
) -> Response:
"""
Create a job for extracting mediainfo.
Args:
asset_id: ID of the asset
file_id: ID of the file
priority: Job priority 1-10. Default is 5
**kwargs: Additional kwargs to pass to the request