1import json 2 3import cloudinary 4from cloudinary.api_client.execute_request import execute_request 5from cloudinary.utils import get_http_connector 6 7 8logger = cloudinary.logger 9_http = get_http_connector(cloudinary.config(), cloudinary.CERT_KWARGS) 10 11 12def call_metadata_api(method, uri, params, **options): 13 """Private function that assists with performing an API call to the 14 metadata_fields part of the Admin API 15 :param method: The HTTP method. Valid methods: get, post, put, delete 16 :param uri: REST endpoint of the API (without 'metadata_fields') 17 :param params: Query/body parameters passed to the method 18 :param options: Additional options 19 :rtype: Response 20 """ 21 uri = ["metadata_fields"] + (uri or []) 22 return call_json_api(method, uri, params, **options) 23 24 25def call_json_api(method, uri, json_body, **options): 26 data = json.dumps(json_body).encode('utf-8') 27 return _call_api(method, uri, body=data, headers={'Content-Type': 'application/json'}, **options) 28 29 30def call_api(method, uri, params, **options): 31 return _call_api(method, uri, params=params, **options) 32 33 34def _call_api(method, uri, params=None, body=None, headers=None, **options): 35 prefix = options.pop("upload_prefix", 36 cloudinary.config().upload_prefix) or "https://api.cloudinary.com" 37 cloud_name = options.pop("cloud_name", cloudinary.config().cloud_name) 38 if not cloud_name: 39 raise Exception("Must supply cloud_name") 40 41 api_key = options.pop("api_key", cloudinary.config().api_key) 42 api_secret = options.pop("api_secret", cloudinary.config().api_secret) 43 oauth_token = options.pop("oauth_token", cloudinary.config().oauth_token) 44 45 _validate_authorization(api_key, api_secret, oauth_token) 46 47 api_url = "/".join([prefix, cloudinary.API_VERSION, cloud_name] + uri) 48 auth = {"key": api_key, "secret": api_secret, "oauth_token": oauth_token} 49 50 if body is not None: 51 options["body"] = body 52 53 return execute_request(http_connector=_http, 54 method=method, 55 params=params, 56 headers=headers, 57 auth=auth, 58 api_url=api_url, 59 **options) 60 61 62def _validate_authorization(api_key, api_secret, oauth_token): 63 if oauth_token: 64 return 65 66 if not api_key: 67 raise Exception("Must supply api_key") 68 69 if not api_secret: 70 raise Exception("Must supply api_secret") 71