-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathapi.py
More file actions
1677 lines (1349 loc) · 63.9 KB
/
api.py
File metadata and controls
1677 lines (1349 loc) · 63.9 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
# Copyright Cloudinary
import datetime
import json
from six import string_types
import cloudinary
from cloudinary import utils
from cloudinary.api_client.call_api import (
call_metadata_api,
call_metadata_rules_api,
call_api,
call_json_api,
_call_v2_api,
)
from cloudinary.exceptions import (
BadRequest,
AuthorizationRequired,
NotAllowed,
NotFound,
AlreadyExists,
RateLimited,
GeneralError
)
def ping(**options):
"""
Tests the reachability of the Cloudinary API.
See: https://cloudinary.com/documentation/admin_api#ping_cloudinary_servers
:param options: Additional optional configuration parameters (none currently recognized).
:return: The result of the API call.
:rtype: Response
"""
return call_json_api("get", ["ping"], {}, **options)
def usage(**options):
"""
Get account usage details.
Get a report on the status of your Cloudinary account usage details, including storage, credits, bandwidth,
requests, number of resources, and add-on usage. Note that numbers are updated periodically.
See: https://cloudinary.com/documentation/admin_api#get_product_environment_usage_details
:param options: Additional optional parameters.
:keyword date: The date for usage details (string in "YYYY-MM" format or a datetime.date object).
If omitted, returns usage for the current billing period.
:type date: str or datetime.date
:return: Detailed usage information
:rtype: Response
"""
date = options.pop("date", None)
uri = ["usage"]
if date:
if isinstance(date, datetime.date):
date = utils.encode_date_to_usage_api_format(date)
uri.append(date)
return call_json_api("get", uri, {}, **options)
def config(**options):
"""
Get account config details.
Fetches the account's configuration details with optional settings.
See: https://cloudinary.com/documentation/admin_api#get_product_environment_config_details
:param options: The optional parameters for the API request.
:keyword bool settings: When True, returns extended settings in the response (if available).
:return: Detailed config information.
:rtype: Response
"""
params = only(options, "settings")
return call_json_api("get", ["config"], params, **options)
def resource_types(**options):
"""
Retrieves the types of resources (assets) available.
See: https://cloudinary.com/documentation/admin_api#get_resources
:param options: Additional optional configuration parameters (none currently recognized).
:return: The result of the API call.
:rtype: Response
"""
return call_json_api("get", ["resources"], {}, **options)
def resources(**options):
"""
Retrieves resources (assets) based on the provided options.
See: https://cloudinary.com/documentation/admin_api#get_resources
:param options: Additional options to filter the resources.
:keyword str resource_type: The type of the resources. Defaults to "image".
:keyword str type: The specific asset type. Defaults to None (not added to URI).
:keyword str prefix: Return only resources with a public ID (or folder) that starts with this prefix.
:keyword str start_at: Return resources updated since the specified timestamp (format: "yyyy-mm-dd hh:mm:ss").
:keyword str direction: Return resources sorted by "asc" or "desc" order of creation.
:keyword str next_cursor: A string that is returned as part of the response when there are more results to retrieve.
:keyword int max_results: Maximum number of resources to return. Default=10.
:return: The result of the API call.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
upload_type = options.pop("type", None)
uri = ["resources", resource_type]
if upload_type:
uri.append(upload_type)
params = __list_resources_params(**options)
params.update(only(options, "prefix", "start_at"))
return call_json_api("get", uri, params, **options)
def resources_by_tag(tag, **options):
"""
Lists resources (assets) with the specified tag.
This method does not return matching deleted assets, even if they have been backed up.
See: https://cloudinary.com/documentation/admin_api#get_resources_by_tag
:param tag: The tag value.
:type tag: str
:param options: Additional options to filter the resources.
:keyword str resource_type: The type of the resources. Defaults to "image".
:keyword str direction: Return resources in "asc" or "desc" order (by creation).
:keyword int max_results: Maximum number of resources to return. Default=10.
:keyword str next_cursor: A string returned when there are more results to fetch.
:return: The result of the API call.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
uri = ["resources", resource_type, "tags", tag]
params = __list_resources_params(**options)
return call_json_api("get", uri, params, **options)
def resources_by_moderation(kind, status, **options):
"""
Lists resources (assets) currently in the specified moderation queue and status.
See: https://cloudinary.com/documentation/admin_api#get_resources_in_moderation
:param kind: Type of image moderation queue to list (e.g., "manual", "webpurify", "aws_rek", "metascan").
:type kind: str
:param status: Only assets with this moderation status will be returned.
Valid values: "pending", "approved", "rejected".
:type status: str
:param options: Additional options to filter the resources.
:keyword str resource_type: The type of the resources. Defaults to "image".
:keyword str direction: Return resources in "asc" or "desc" order (by creation).
:keyword int max_results: Maximum number of resources to return. Default=10.
:keyword str next_cursor: A string returned when there are more results to fetch.
:return: The result of the API call.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
uri = ["resources", resource_type, "moderations", kind, status]
params = __list_resources_params(**options)
return call_json_api("get", uri, params, **options)
def resources_by_ids(public_ids, **options):
"""
Lists resources (assets) with the specified public IDs.
See: https://cloudinary.com/documentation/admin_api#get_resources
:param public_ids: The requested public_ids (up to 100).
:type public_ids: list[str]
:param options: The optional parameters.
:keyword str resource_type: The type of the resources. Defaults to "image".
:keyword str type: The specific asset type. Defaults to "upload".
:keyword str direction: Return resources in "asc" or "desc" order.
:keyword int max_results: Maximum number of resources to return.
:keyword str next_cursor: A string returned when there are more results to fetch.
:return: The result of the API call.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
upload_type = options.pop("type", "upload")
uri = ["resources", resource_type, upload_type]
params = dict(__resources_params(**options), public_ids=public_ids)
return call_json_api("get", uri, params, **options)
def resources_by_asset_folder(asset_folder, **options):
"""
Returns the details of the resources (assets) under a specified asset_folder.
See: https://cloudinary.com/documentation/admin_api#get_resources_by_asset_folder
:param asset_folder: The Asset Folder of the asset.
:type asset_folder: str
:param options: Additional options to filter the resources.
:keyword str direction: Return resources in "asc" or "desc" order.
:keyword int max_results: Maximum number of resources to return.
:keyword str next_cursor: A string returned when there are more results to fetch.
:return: Resources (assets) of a specific asset_folder.
:rtype: Response
"""
uri = ["resources", "by_asset_folder"]
params = __list_resources_params(**options)
params["asset_folder"] = asset_folder
return call_json_api("get", uri, params, **options)
def resources_by_asset_ids(asset_ids, **options):
"""
Retrieves the resources (assets) indicated in the asset IDs.
This method does not return deleted assets even if they have been backed up.
See: https://cloudinary.com/documentation/admin_api#get_resources_by_asset_ids
:param asset_ids: The requested asset IDs.
:type asset_ids: list[str]
:param options: Additional options to filter the resources.
:keyword str direction: Return resources in "asc" or "desc" order.
:keyword int max_results: Maximum number of resources to return.
:keyword str next_cursor: A string returned when there are more results to fetch.
:return: Resources (assets) as indicated in the asset IDs.
:rtype: Response
"""
uri = ["resources", "by_asset_ids"]
params = dict(__resources_params(**options), asset_ids=asset_ids)
return call_json_api("get", uri, params, **options)
def resources_by_context(key, value=None, **options):
"""
Retrieves resources (assets) with a specified context key.
This method does not return deleted assets even if they have been backed up.
See: https://cloudinary.com/documentation/admin_api#get_resources_by_context
:param key: Only assets with this context key are returned.
:type key: str
:param value: Only assets with this value for the context key are returned.
:type value: str, optional
:param options: Additional options to filter the resources.
:keyword str resource_type: The type of the resources. Defaults to "image".
:keyword str direction: Return resources in "asc" or "desc" order.
:keyword int max_results: Maximum number of resources to return.
:keyword str next_cursor: A string returned when there are more results to fetch.
:return: Resources (assets) with a specified context key.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
uri = ["resources", resource_type, "context"]
params = __list_resources_params(**options)
params["key"] = key
if value is not None:
params["value"] = value
return call_json_api("get", uri, params, **options)
def __resources_params(**options):
"""
Prepares optional parameters for resources_* API calls.
:param options: Additional options
:return: Optional parameters
:rtype: dict
:internal
"""
params = only(options, "tags", "context", "metadata", "moderations")
if options.get("fields"):
params["fields"] = utils.encode_list(utils.build_array(options["fields"]))
return params
def __list_resources_params(**options):
"""
Prepares optional parameters for resources_* API calls.
:param options: Additional options
:return: Optional parameters
:rtype: dict
:internal
"""
resources_params = __resources_params(**options)
resources_params.update(only(options, "next_cursor", "max_results", "direction"))
return resources_params
def visual_search(image_url=None, image_asset_id=None, text=None, image_file=None, **options):
"""
Find images based on their visual content.
See: https://cloudinary.com/documentation/admin_api#visual_search_for_resources
:param image_url: The URL of an image.
:type image_url: str, optional
:param image_asset_id: The asset_id of an image in your account.
:type image_asset_id: str, optional
:param text: A textual description (e.g. "cat").
:type text: str, optional
:param image_file: The image file. (str|callable|Path|bytes)
:type image_file: str or callable or Path or bytes, optional
:param options: Additional optional parameters to pass along.
:return: Resources (assets) that were found
:rtype: Response
"""
uri = ["resources", "visual_search"]
params = {
"image_url": image_url,
"image_asset_id": image_asset_id,
"text": text,
"image_file": utils.handle_file_parameter(image_file, "file")
}
return call_api("post", uri, params, **options)
def resource(public_id, **options):
"""
Returns the details of the specified asset and all its derived assets (by public ID).
See: https://cloudinary.com/documentation/admin_api#get_details_of_a_single_resource_by_public_id
:param public_id: The public ID of the resource.
:type public_id: str
:param options: Additional optional parameters for retrieval.
:keyword str resource_type: The resource type (e.g. "image", "raw").
:keyword str type: The asset's storage type (e.g. "upload").
:keyword bool exif: Whether to return Exif metadata.
:keyword bool faces: Whether to return face coordinates.
:keyword bool colors: Whether to return color information.
:keyword bool image_metadata: Whether to return image metadata.
:keyword bool media_metadata: Whether to return extended media metadata.
:keyword bool cinemagraph_analysis: Whether to include cinemagraph analysis data.
:keyword bool pages: Whether to include the page count of multi-page files.
:keyword bool phash: Whether to include perceptual hash data.
:keyword bool coordinates: Whether to return custom and face coordinates.
:keyword int max_results: The maximum number of derived resources to return.
:keyword bool quality_analysis: Whether to include quality analysis data.
:keyword str derived_next_cursor: A pagination cursor for derived resources.
:keyword bool accessibility_analysis: Whether to include accessibility analysis data.
:keyword bool versions: Whether to include version information for the asset.
:keyword bool related: Whether to include related assets.
:keyword str related_next_cursor: A pagination cursor for related assets.
:return: The result of the API call.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
upload_type = options.pop("type", "upload")
uri = ["resources", resource_type, upload_type, public_id]
params = _prepare_asset_details_params(**options)
return call_json_api("get", uri, params, **options)
def resource_by_asset_id(asset_id, **options):
"""
Returns the details of the specified asset and all its derived assets (by asset ID).
See: https://cloudinary.com/documentation/admin_api#get_details_of_a_single_resource_by_asset_id
:param asset_id: The Asset ID of the asset
:type asset_id: str
:param options: Additional optional parameters for retrieval.
:keyword bool exif: Whether to return Exif metadata.
:keyword bool faces: Whether to return face coordinates.
:keyword bool colors: Whether to return color information.
:keyword bool image_metadata: Whether to return image metadata.
:keyword bool media_metadata: Whether to return extended media metadata.
:keyword bool cinemagraph_analysis: Whether to include cinemagraph analysis data.
:keyword bool pages: Whether to include the page count of multi-page files.
:keyword bool phash: Whether to include perceptual hash data.
:keyword bool coordinates: Whether to return custom and face coordinates.
:keyword int max_results: The maximum number of derived resources to return.
:keyword bool quality_analysis: Whether to include quality analysis data.
:keyword str derived_next_cursor: A pagination cursor for derived resources.
:keyword bool accessibility_analysis: Whether to include accessibility analysis data.
:keyword bool versions: Whether to include version information for the asset.
:keyword bool related: Whether to include related assets.
:keyword str related_next_cursor: A pagination cursor for related assets.
:return: Resource (asset) of a specific asset_id
:rtype: Response
"""
uri = ["resources", asset_id]
params = _prepare_asset_details_params(**options)
return call_json_api("get", uri, params, **options)
def _prepare_asset_details_params(**options):
"""
Prepares optional parameters for resource_by_asset_id or resource_by_public_id API calls.
:param options: Additional options
:return: Optional parameters
:rtype: dict
:internal
"""
return only(options, "exif", "faces", "colors", "image_metadata", "media_metadata", "cinemagraph_analysis",
"pages", "phash", "coordinates", "max_results", "quality_analysis", "derived_next_cursor",
"accessibility_analysis", "versions", "related", "related_next_cursor")
def update(public_id, **options):
"""
Updates the details of a specified resource by public ID.
See: https://cloudinary.com/documentation/admin_api#update_details_of_an_existing_resource
:param public_id: The public ID of the resource to update.
:type public_id: str
:param options: Additional options for the update operation.
:keyword str resource_type: The resource type (e.g. "image", "raw").
:keyword str type: The asset's storage type (e.g. "upload").
:keyword str moderation_status: Sets the moderation status ("approved" / "rejected").
:keyword str raw_convert: Requests raw file conversion ("aspose", etc.).
:keyword str quality_override: Overrides the quality setting.
:keyword str ocr: Requests OCR extraction ("adv_ocr").
:keyword str categorization: Sets the categorization mode (e.g. "google_tagging").
:keyword str detection: Sets the detection mode (e.g. "adv_face").
:keyword str similarity_search: Reserved for similarity search tasks.
:keyword str background_removal: The background removal setting (e.g. "cloudinary_ai" or "pixelz").
:keyword str notification_url: A URL for receiving notifications.
:keyword list tags: The tags to assign to the asset.
:keyword list or str face_coordinates: The face coordinates to set.
:keyword list or str custom_coordinates: The custom coordinates to set.
:keyword list regions: Region data for partial image transformations.
:keyword dict context: Contextual (key/value) metadata.
:keyword dict metadata: Structured metadata.
:keyword float auto_tagging: A float from 0.0 to 1.0. If set, automatically tags an image.
:keyword list access_control: An array of access control rules in dictionary form.
:keyword str asset_folder: The folder path in which to place the asset.
:keyword str display_name: A user-friendly name for the asset.
:keyword bool unique_display_name: If True, ensures the display name is unique.
:keyword bool clear_invalid: If True, removes or corrects invalid data (e.g., invalid context).
:return: The result of the API call.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
upload_type = options.pop("type", "upload")
uri = ["resources", resource_type, upload_type, public_id]
params = only(options, "moderation_status", "raw_convert",
"quality_override", "ocr",
"categorization", "detection", "similarity_search",
"background_removal", "notification_url")
if "tags" in options:
params["tags"] = ",".join(utils.build_array(options["tags"]))
if "face_coordinates" in options:
params["face_coordinates"] = utils.encode_double_array(options.get("face_coordinates"))
if "custom_coordinates" in options:
params["custom_coordinates"] = utils.encode_double_array(options.get("custom_coordinates"))
if "regions" in options:
params["regions"] = utils.json_encode(options.get("regions"))
if "context" in options:
params["context"] = utils.encode_context(options.get("context"))
if "metadata" in options:
params["metadata"] = utils.encode_context(options.get("metadata"))
if "auto_tagging" in options:
params["auto_tagging"] = str(options.get("auto_tagging"))
if "access_control" in options:
params["access_control"] = utils.json_encode(utils.build_list_of_dicts(options.get("access_control")))
if "asset_folder" in options:
params["asset_folder"] = options.get("asset_folder")
if "display_name" in options:
params["display_name"] = options.get("display_name")
if "unique_display_name" in options:
params["unique_display_name"] = options.get("unique_display_name")
if "clear_invalid" in options:
params["clear_invalid"] = options.get("clear_invalid")
return call_json_api("post", uri, params, **options)
def delete_resources(public_ids, **options):
"""
Deletes resources (assets) given their public IDs.
The resources must belong to the specified resource_type and type.
See: https://cloudinary.com/documentation/admin_api#delete_resources
:param public_ids: The public IDs of the resources to delete.
:type public_ids: list[str]
:param options: Additional options.
:keyword str resource_type: Defaults to "image".
:keyword str type: Defaults to "upload".
:keyword list transformations: The derived transformations to delete (if any).
:keyword bool keep_original: When True, keeps the original resource.
:keyword str next_cursor: A string returned when more results are available.
:keyword bool invalidate: When True, invalidates the assets on the CDN.
:return: The result of the command.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
upload_type = options.pop("type", "upload")
uri = ["resources", resource_type, upload_type]
params = __delete_resource_params(options, public_ids=public_ids)
return call_json_api("delete", uri, params, **options)
def delete_resources_by_asset_ids(asset_ids, **options):
"""
Deletes resources (assets) by asset IDs.
See: https://cloudinary.com/documentation/admin_api#delete_resources_by_asset_id
:param asset_ids: The asset IDs of the assets to delete.
:type asset_ids: list[str]
:param options: Additional options.
:keyword list transformations: The derived transformations to delete (if any).
:keyword bool keep_original: When True, keeps the original resource.
:keyword str next_cursor: A string returned when more results are available.
:keyword bool invalidate: When True, invalidates the assets on the CDN.
:return: The result of the command.
:rtype: dict
"""
uri = ["resources"]
params = __delete_resource_params(options, asset_ids=asset_ids)
return call_json_api("delete", uri, params, **options)
def delete_resources_by_prefix(prefix, **options):
"""
Deletes resources (assets) that have a specified prefix for their Public IDs.
See: https://cloudinary.com/documentation/admin_api#delete_resources
:param prefix: The prefix of the Public IDs to delete.
:type prefix: str
:param options: Additional options.
:keyword str resource_type: Defaults to "image".
:keyword str type: Defaults to "upload".
:keyword list transformations: The derived transformations to delete (if any).
:keyword bool keep_original: When True, keeps the original resource.
:keyword str next_cursor: A string returned when more results are available.
:keyword bool invalidate: When True, invalidates the assets on the CDN.
:return: The result of the command.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
upload_type = options.pop("type", "upload")
uri = ["resources", resource_type, upload_type]
params = __delete_resource_params(options, prefix=prefix)
return call_json_api("delete", uri, params, **options)
def delete_all_resources(**options):
"""
Deletes **all** resources (assets) of a specified resource_type and type.
Use with caution: This removes all matching resources from your account.
See: https://cloudinary.com/documentation/admin_api#delete_resources
:param options: Additional options.
:keyword str resource_type: Defaults to "image".
:keyword str type: Defaults to "upload".
:keyword list transformations: The derived transformations to delete (if any).
:keyword bool keep_original: When True, keeps the original resource.
:keyword str next_cursor: A string returned when more results are available.
:keyword bool invalidate: When True, invalidates the assets on the CDN.
:keyword bool all: (Added internally) If True, indicates all resources are to be deleted.
:return: The result of the command.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
upload_type = options.pop("type", "upload")
uri = ["resources", resource_type, upload_type]
params = __delete_resource_params(options, all=True)
return call_json_api("delete", uri, params, **options)
def delete_resources_by_tag(tag, **options):
"""
Deletes resources (assets) that contain a specified tag.
See: https://cloudinary.com/documentation/admin_api#delete_resources_by_tags
:param tag: The tag whose associated resources should be deleted.
:type tag: str
:param options: Additional options.
:keyword str resource_type: Defaults to "image".
:keyword list transformations: The derived transformations to delete (if any).
:keyword bool keep_original: When True, keeps the original resource.
:keyword str next_cursor: A string returned when more results are available.
:keyword bool invalidate: When True, invalidates the assets on the CDN.
:return: The result of the command.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
uri = ["resources", resource_type, "tags", tag]
params = __delete_resource_params(options)
return call_json_api("delete", uri, params, **options)
def delete_derived_resources(derived_resource_ids, **options):
"""
Deletes derived resources by their derived resource IDs.
See: https://cloudinary.com/documentation/admin_api#delete_derived_resources
:param derived_resource_ids: A list of derived resource IDs.
:type derived_resource_ids: list[str]
:param options: Additional optional parameters (none currently recognized).
:return: The result of the command.
:rtype: Response
"""
uri = ["derived_resources"]
params = {"derived_resource_ids": derived_resource_ids}
return call_json_api("delete", uri, params, **options)
def delete_derived_by_transformation(public_ids, transformations,
resource_type='image', type='upload', invalidate=None,
**options):
"""
Deletes derived resources of public IDs, identified by transformations.
See: https://cloudinary.com/documentation/admin_api#delete_derived_resources
:param public_ids: The base resources (list of public IDs).
:type public_ids: list[str]
:param transformations: The transformations of derived resources, optionally including the format.
:type transformations: list[dict or str]
:param resource_type: The type of the resource. Defaults to "image".
:type resource_type: str
:param type: The upload type. Defaults to "upload".
:type type: str
:param invalidate: (optional) True to invalidate the resources after deletion.
:type invalidate: bool, optional
:param options: Additional optional parameters (none currently recognized).
:return: The result of the command, including the public IDs for which derived resources were deleted.
:rtype: dict
"""
uri = ["resources", resource_type, type]
if not isinstance(public_ids, list):
public_ids = [public_ids]
params = {
"public_ids": public_ids,
"transformations": utils.build_eager(transformations),
"keep_original": True
}
if invalidate is not None:
params['invalidate'] = invalidate
return call_json_api("delete", uri, params, **options)
def delete_backed_up_assets(asset_id, version_ids, **options):
"""
Deletes backed up versions of a resource by asset IDs.
See: https://cloudinary.com/documentation/admin_api#delete_backed_up_versions_of_a_resource
:param asset_id: The asset ID of the asset to update.
:type asset_id: str
:param version_ids: The array of version IDs.
:type version_ids: list[str]
:param options: Additional optional parameters (none currently recognized).
:return: The result of the command.
:rtype: dict
"""
uri = ["resources", "backup", asset_id]
params = {"version_ids": utils.build_array(version_ids)}
return call_json_api("delete", uri, params, **options)
def add_related_assets(public_id, assets_to_relate, resource_type="image", type="upload", **options):
"""
Relates an asset to other assets by public IDs.
See: https://cloudinary.com/documentation/admin_api#add_related_assets
:param public_id: The public ID of the asset to update.
:type public_id: str
:param assets_to_relate: Array of up to 10 fully_qualified_public_ids as resource_type/type/public_id.
:type assets_to_relate: list[str]
:param resource_type: The type of the resource. Defaults to "image".
:type resource_type: str
:param type: The upload type. Defaults to "upload".
:type type: str
:param options: Additional optional parameters (none currently recognized).
:return: The result of the command.
:rtype: dict
"""
uri = ["resources", "related_assets", resource_type, type, public_id]
params = {"assets_to_relate": utils.build_array(assets_to_relate)}
return call_json_api("post", uri, params, **options)
def add_related_assets_by_asset_ids(asset_id, assets_to_relate, **options):
"""
Relates an asset to other assets by asset IDs.
See: https://cloudinary.com/documentation/admin_api#add_related_assets_by_asset_id
:param asset_id: The asset ID of the asset to update.
:type asset_id: str
:param assets_to_relate: The array of up to 10 asset IDs.
:type assets_to_relate: list[str]
:param options: Additional optional parameters (none currently recognized).
:return: The result of the command.
:rtype: dict
"""
uri = ["resources", "related_assets", asset_id]
params = {"assets_to_relate": utils.build_array(assets_to_relate)}
return call_json_api("post", uri, params, **options)
def delete_related_assets(public_id, assets_to_unrelate, resource_type="image", type="upload", **options):
"""
Unrelates an asset from other assets by public IDs.
See: https://cloudinary.com/documentation/admin_api#delete_related_assets
:param public_id: The public ID of the asset to update.
:type public_id: str
:param assets_to_unrelate: Array of up to 10 fully_qualified_public_ids as resource_type/type/public_id.
:type assets_to_unrelate: list[str]
:param resource_type: The type of the resource. Defaults to "image".
:type resource_type: str
:param type: The upload type. Defaults to "upload".
:type type: str
:param options: Additional optional parameters (none currently recognized).
:return: The result of the command.
:rtype: dict
"""
uri = ["resources", "related_assets", resource_type, type, public_id]
params = {"assets_to_unrelate": utils.build_array(assets_to_unrelate)}
return call_json_api("delete", uri, params, **options)
def delete_related_assets_by_asset_ids(asset_id, assets_to_unrelate, **options):
"""
Unrelates an asset from other assets by asset IDs.
See: https://cloudinary.com/documentation/admin_api#delete_related_assets_by_asset_id
:param asset_id: The asset ID of the asset to update.
:type asset_id: str
:param assets_to_unrelate: The array of up to 10 asset IDs.
:type assets_to_unrelate: list[str]
:param options: Additional optional parameters (none currently recognized).
:return: The result of the command.
:rtype: dict
"""
uri = ["resources", "related_assets", asset_id]
params = {"assets_to_unrelate": utils.build_array(assets_to_unrelate)}
return call_json_api("delete", uri, params, **options)
def tags(**options):
"""
Lists all the tags currently used for a specified asset type.
See: https://cloudinary.com/documentation/admin_api#get_tags
:param options: The optional parameters.
:keyword str resource_type: Defaults to "image".
:keyword str prefix: Return only tags that begin with the specified prefix.
:keyword int max_results: Maximum number of tags to return.
:keyword str next_cursor: A string returned when more results are available.
:return: The result of the API call.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
uri = ["tags", resource_type]
return call_json_api("get", uri, only(options, "next_cursor", "max_results", "prefix"), **options)
def transformations(**options):
"""
Lists all transformations.
See: https://cloudinary.com/documentation/admin_api#get_transformations
:param options: The optional parameters.
:keyword bool named: When True, return only named transformations.
:keyword str next_cursor: A string returned when more results are available.
:keyword int max_results: Maximum number of transformations to return.
:return: The list of transformations.
:rtype: Response
"""
uri = ["transformations"]
params = only(options, "named", "next_cursor", "max_results")
return call_json_api("get", uri, params, **options)
def transformation(transformation, **options):
"""
Returns the details of a single transformation.
See: https://cloudinary.com/documentation/admin_api#get_transformation_details
:param transformation: The transformation to retrieve (string or dict).
:type transformation: str or dict
:param options: The optional parameters.
:keyword str next_cursor: A string returned when more results are available.
:keyword int max_results: Maximum number of derived assets to return.
:return: The transformation details.
:rtype: Response
"""
uri = ["transformations"]
params = only(options, "next_cursor", "max_results")
params["transformation"] = utils.build_single_eager(transformation)
return call_json_api("get", uri, params, **options)
def delete_transformation(transformation, **options):
"""
Deletes a transformation.
See: https://cloudinary.com/documentation/admin_api#delete_transformation
:param transformation: The transformation to delete (string or dict).
:type transformation: str or dict
:param options: Additional options (none currently recognized).
:return: The result of the API call.
:rtype: Response
"""
uri = ["transformations"]
params = {"transformation": utils.build_single_eager(transformation)}
return call_json_api("delete", uri, params, **options)
def update_transformation(transformation, **options):
"""
Updates a transformation.
Currently, the only supported update is setting the "allowed_for_strict" flag
and the "unsafe_update" transformation.
See: https://cloudinary.com/documentation/admin_api#update_transformation
:param transformation: The transformation to update (string or dict).
:type transformation: str or dict
:param options: Additional update options.
:keyword bool allowed_for_strict: Whether the transformation is allowed in strict mode.
:keyword dict or str unsafe_update: The transformation to associate under unsafe_update.
:return: The result of the API call.
:rtype: Response
"""
uri = ["transformations"]
updates = only(options, "allowed_for_strict")
if "unsafe_update" in options:
updates["unsafe_update"] = transformation_string(options.get("unsafe_update"))
updates["transformation"] = utils.build_single_eager(transformation)
return call_json_api("put", uri, updates, **options)
def create_transformation(name, definition, **options):
"""
Creates a named transformation based on an existing transformation.
See: https://cloudinary.com/documentation/admin_api#create_a_named_transformation
:param name: The name of the transformation to create.
:type name: str
:param definition: The transformation definition (string or dict).
:type definition: str or dict
:param options: Additional options (none currently recognized).
:return: The result of the API call.
:rtype: Response
"""
uri = ["transformations"]
params = {"name": name, "transformation": utils.build_single_eager(definition)}
return call_json_api("post", uri, params, **options)
def publish_by_ids(public_ids, **options):
"""
Publishes specific assets by their public IDs.
:param public_ids: The list of public IDs to publish.
:type public_ids: list[str]
:param options: Additional options.
:keyword str resource_type: The resource type (e.g. "image").
:keyword str type: The asset type (e.g. "upload").
:keyword bool overwrite: Whether to overwrite existing published assets.
:keyword bool invalidate: Whether to invalidate the CDN.
:return: The result of the publish operation.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
uri = ["resources", resource_type, "publish_resources"]
params = dict(only(options, "type", "overwrite", "invalidate"), public_ids=public_ids)
return call_json_api("post", uri, params, **options)
def publish_by_prefix(prefix, **options):
"""
Publishes assets that have a specified prefix for their public IDs.
:param prefix: The prefix of the public IDs to publish.
:type prefix: str
:param options: Additional options.
:keyword str resource_type: The resource type (e.g. "image").
:keyword str type: The asset type (e.g. "upload").
:keyword bool overwrite: Whether to overwrite existing published assets.
:keyword bool invalidate: Whether to invalidate the CDN.
:return: The result of the publish operation.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
uri = ["resources", resource_type, "publish_resources"]
params = dict(only(options, "type", "overwrite", "invalidate"), prefix=prefix)
return call_json_api("post", uri, params, **options)
def publish_by_tag(tag, **options):
"""
Publishes assets that contain a specified tag.
:param tag: The tag whose associated resources should be published.
:type tag: str
:param options: Additional options.
:keyword str resource_type: The resource type (e.g. "image").
:keyword str type: The asset type (e.g. "upload").
:keyword bool overwrite: Whether to overwrite existing published assets.
:keyword bool invalidate: Whether to invalidate the CDN.
:return: The result of the publish operation.
:rtype: Response
"""
resource_type = options.pop("resource_type", "image")
uri = ["resources", resource_type, "publish_resources"]
params = dict(only(options, "type", "overwrite", "invalidate"), tag=tag)
return call_json_api("post", uri, params, **options)
def upload_presets(**options):
"""
Lists all upload presets.
See: https://cloudinary.com/documentation/admin_api#get_upload_presets
:param options: Additional options.
:keyword str next_cursor: A string returned when more results are available.
:keyword int max_results: Maximum number of presets to return.
:return: A list of upload presets.
:rtype: Response
"""
uri = ["upload_presets"]
return call_json_api("get", uri, only(options, "next_cursor", "max_results"), **options)
def upload_preset(name, **options):
"""
Retrieves the details of a single upload preset.
See: https://cloudinary.com/documentation/admin_api#get_the_details_of_a_single_upload_preset
:param name: The name of the upload preset.
:type name: str
:param options: Additional options.
:keyword int max_results: Maximum number of details to return (if relevant).
:return: The upload preset details.
:rtype: Response
"""
uri = ["upload_presets", name]
return call_json_api("get", uri, only(options, "max_results"), **options)
def delete_upload_preset(name, **options):
"""
Deletes an upload preset by name.
See: https://cloudinary.com/documentation/admin_api#delete_an_upload_preset
:param name: The name of the upload preset to delete.
:type name: str
:param options: Additional options (none currently recognized).
:return: The result of the deletion.
:rtype: Response
"""
uri = ["upload_presets", name]
return call_json_api("delete", uri, {}, **options)
def update_upload_preset(name, **options):
"""
Updates an existing upload preset.
See: https://cloudinary.com/documentation/admin_api#update_an_upload_preset
:param name: The name of the upload preset to update.
:type name: str
:param options: The parameters to update for the preset (e.g., folder, tags).
:keyword bool unsigned: Whether this preset is unsigned (public).
:keyword bool disallow_public_id: When True, the public ID cannot be overridden during upload.
:keyword bool live: Whether this preset is for live (video) usage.
:return: The updated upload preset.
:rtype: Response
"""