Update cloudinary-1.26.0

This commit is contained in:
JonnyWong16 2021-10-14 21:18:46 -07:00
parent ebffd124f6
commit 4b28040d59
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
17 changed files with 1169 additions and 307 deletions

View file

View file

@ -0,0 +1,35 @@
import cloudinary
from cloudinary.api_client.execute_request import execute_request
from cloudinary.provisioning.account_config import account_config
from cloudinary.utils import get_http_connector
PROVISIONING_SUB_PATH = "provisioning"
ACCOUNT_SUB_PATH = "accounts"
_http = get_http_connector(account_config(), cloudinary.CERT_KWARGS)
def _call_account_api(method, uri, params=None, headers=None, **options):
prefix = options.pop("upload_prefix",
cloudinary.config().upload_prefix) or "https://api.cloudinary.com"
account_id = options.pop("account_id", account_config().account_id)
if not account_id:
raise Exception("Must supply account_id")
provisioning_api_key = options.pop("provisioning_api_key", account_config().provisioning_api_key)
if not provisioning_api_key:
raise Exception("Must supply provisioning_api_key")
provisioning_api_secret = options.pop("provisioning_api_secret",
account_config().provisioning_api_secret)
if not provisioning_api_secret:
raise Exception("Must supply provisioning_api_secret")
provisioning_api_url = "/".join(
[prefix, cloudinary.API_VERSION, PROVISIONING_SUB_PATH, ACCOUNT_SUB_PATH, account_id] + uri)
auth = {"key": provisioning_api_key, "secret": provisioning_api_secret}
return execute_request(http_connector=_http,
method=method,
params=params,
headers=headers,
auth=auth,
api_url=provisioning_api_url,
**options)

View file

@ -0,0 +1,70 @@
import json
import cloudinary
from cloudinary.api_client.execute_request import execute_request
from cloudinary.utils import get_http_connector
logger = cloudinary.logger
_http = get_http_connector(cloudinary.config(), cloudinary.CERT_KWARGS)
def call_metadata_api(method, uri, params, **options):
"""Private function that assists with performing an API call to the
metadata_fields part of the Admin API
:param method: The HTTP method. Valid methods: get, post, put, delete
:param uri: REST endpoint of the API (without 'metadata_fields')
:param params: Query/body parameters passed to the method
:param options: Additional options
:rtype: Response
"""
uri = ["metadata_fields"] + (uri or [])
return call_json_api(method, uri, params, **options)
def call_json_api(method, uri, json_body, **options):
data = json.dumps(json_body).encode('utf-8')
return _call_api(method, uri, body=data, headers={'Content-Type': 'application/json'}, **options)
def call_api(method, uri, params, **options):
return _call_api(method, uri, params=params, **options)
def _call_api(method, uri, params=None, body=None, headers=None, **options):
prefix = options.pop("upload_prefix",
cloudinary.config().upload_prefix) or "https://api.cloudinary.com"
cloud_name = options.pop("cloud_name", cloudinary.config().cloud_name)
if not cloud_name:
raise Exception("Must supply cloud_name")
api_key = options.pop("api_key", cloudinary.config().api_key)
api_secret = options.pop("api_secret", cloudinary.config().api_secret)
oauth_token = options.pop("oauth_token", cloudinary.config().oauth_token)
_validate_authorization(api_key, api_secret, oauth_token)
api_url = "/".join([prefix, cloudinary.API_VERSION, cloud_name] + uri)
auth = {"key": api_key, "secret": api_secret, "oauth_token": oauth_token}
if body is not None:
options["body"] = body
return execute_request(http_connector=_http,
method=method,
params=params,
headers=headers,
auth=auth,
api_url=api_url,
**options)
def _validate_authorization(api_key, api_secret, oauth_token):
if oauth_token:
return
if not api_key:
raise Exception("Must supply api_key")
if not api_secret:
raise Exception("Must supply api_secret")

View file

@ -0,0 +1,86 @@
import email.utils
import json
import socket
import urllib3
from urllib3.exceptions import HTTPError
import cloudinary
from cloudinary.exceptions import (
BadRequest,
AuthorizationRequired,
NotAllowed,
NotFound,
AlreadyExists,
RateLimited,
GeneralError
)
from cloudinary.utils import process_params, safe_cast, smart_escape, unquote
EXCEPTION_CODES = {
400: BadRequest,
401: AuthorizationRequired,
403: NotAllowed,
404: NotFound,
409: AlreadyExists,
420: RateLimited,
500: GeneralError
}
class Response(dict):
def __init__(self, result, response, **kwargs):
super(Response, self).__init__(**kwargs)
self.update(result)
self.rate_limit_allowed = safe_cast(response.headers.get("x-featureratelimit-limit"), int)
self.rate_limit_reset_at = safe_cast(response.headers.get("x-featureratelimit-reset"), email.utils.parsedate)
self.rate_limit_remaining = safe_cast(response.headers.get("x-featureratelimit-remaining"), int)
def execute_request(http_connector, method, params, headers, auth, api_url, **options):
# authentication
key = auth.get("key")
secret = auth.get("secret")
oauth_token = auth.get("oauth_token")
req_headers = urllib3.make_headers(
user_agent=cloudinary.get_user_agent()
)
if oauth_token:
req_headers["authorization"] = "Bearer {}".format(oauth_token)
else:
req_headers.update(urllib3.make_headers(basic_auth="{0}:{1}".format(key, secret)))
if headers is not None:
req_headers.update(headers)
kw = {}
if "timeout" in options:
kw["timeout"] = options["timeout"]
if "body" in options:
kw["body"] = options["body"]
processed_params = process_params(params)
api_url = smart_escape(unquote(api_url))
try:
response = http_connector.request(method.upper(), api_url, processed_params, req_headers, **kw)
body = response.data
except HTTPError as e:
raise GeneralError("Unexpected error {0}", e.message)
except socket.error as e:
raise GeneralError("Socket Error: %s" % (str(e)))
try:
result = json.loads(body.decode('utf-8'))
except Exception as e:
# Error is parsing json
raise GeneralError("Error parsing server response (%d) - %s. Got - %s" % (response.status, body, e))
if "error" in result:
exception_class = EXCEPTION_CODES.get(response.status) or Exception
exception_class = exception_class
raise exception_class("Error {0} - {1}".format(response.status, result["error"]["message"]))
return Response(result, response)