Skip to content

Commit 2ed146c

Browse files
Add support for analyze API
1 parent 2d0bc89 commit 2ed146c

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

cloudinary/api.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from cloudinary.api_client.call_api import (
1515
call_api,
1616
call_metadata_api,
17-
call_json_api
17+
call_json_api,
18+
_call_v2_api
1819
)
1920
from cloudinary.exceptions import (
2021
BadRequest,
@@ -840,3 +841,18 @@ def reorder_metadata_fields(order_by, direction=None, **options):
840841
uri = ['order']
841842
params = {'order_by': order_by, 'direction': direction}
842843
return call_metadata_api('put', uri, params, **options)
844+
845+
846+
def analyze(input_type, analysis_type, uri=None, **options):
847+
"""Analyzes an asset with the requested analysis type.
848+
849+
:param input_type: The type of input for the asset to analyze ('uri').
850+
:param analysis_type: The type of analysis to run ('google_tagging', 'captioning', 'fashion').
851+
:param uri: The URI of the asset to analyze.
852+
:param options: Additional options.
853+
854+
:rtype: Response
855+
"""
856+
api_uri = ['analysis', 'analyze']
857+
params = {'input_type': input_type, 'analysis_type': analysis_type, 'uri': uri}
858+
return _call_v2_api('post', api_uri, params, **options)

cloudinary/api_client/call_api.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def call_json_api(method, uri, json_body, **options):
2626
return _call_api(method, uri, body=data, headers={'Content-Type': 'application/json'}, **options)
2727

2828

29+
def _call_v2_api(method, uri, json_body, **options):
30+
return call_json_api(method, uri, json_body=json_body, api_version='v2', **options)
31+
32+
2933
def call_api(method, uri, params, **options):
3034
return _call_api(method, uri, params=params, **options)
3135

@@ -42,10 +46,11 @@ def _call_api(method, uri, params=None, body=None, headers=None, extra_headers=N
4246
oauth_token = options.pop("oauth_token", cloudinary.config().oauth_token)
4347

4448
_validate_authorization(api_key, api_secret, oauth_token)
45-
46-
api_url = "/".join([prefix, cloudinary.API_VERSION, cloud_name] + uri)
4749
auth = {"key": api_key, "secret": api_secret, "oauth_token": oauth_token}
4850

51+
api_version = options.pop("api_version", cloudinary.API_VERSION)
52+
api_url = "/".join([prefix, api_version, cloud_name] + uri)
53+
4954
if body is not None:
5055
options["body"] = body
5156

test/helper_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
# urllib3 2.x support
4949
# noinspection PyProtectedMember
5050
import urllib3._request_methods
51+
5152
URLLIB3_REQUEST = "urllib3._request_methods.RequestMethods.request"
5253
except ImportError:
5354
URLLIB3_REQUEST = "urllib3.request.RequestMethods.request"
@@ -140,10 +141,10 @@ def http_response_mock(body="", headers=None, status=200):
140141
return HTTPResponse(body, HTTPHeaderDict(headers), status=status)
141142

142143

143-
def api_response_mock():
144-
return http_response_mock('{"foo":"bar"}', {"x-featureratelimit-limit": '0',
145-
"x-featureratelimit-reset": 'Sat, 01 Apr 2017 22:00:00 GMT',
146-
"x-featureratelimit-remaining": '0'})
144+
def api_response_mock(body='{"foo":"bar"}'):
145+
return http_response_mock(body, {"x-featureratelimit-limit": '0',
146+
"x-featureratelimit-reset": 'Sat, 01 Apr 2017 22:00:00 GMT',
147+
"x-featureratelimit-remaining": '0'})
147148

148149

149150
def uploader_response_mock():

test/test_api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,26 @@ def test_structured_metadata_in_resources_by_moderation_api(self):
12181218
for resource in result["resources"]:
12191219
self.assertNotIn("metadata", resource)
12201220

1221+
@patch(URLLIB3_REQUEST)
1222+
def test_analyze(self, mocker):
1223+
mocker.return_value = MOCK_RESPONSE
1224+
1225+
options = {
1226+
"input_type": "uri",
1227+
"analysis_type": "captioning",
1228+
"uri": "https://res.cloudinary.com/demo/image/upload/dog",
1229+
}
1230+
1231+
api.analyze(**options)
1232+
1233+
uri = get_uri(mocker)
1234+
self.assertIn("/v2/", uri)
1235+
self.assertTrue(uri.endswith("/analysis/analyze"))
1236+
1237+
params = get_json_body(mocker)
1238+
for param in options.keys():
1239+
self.assertIn(param, params)
1240+
12211241

12221242
if __name__ == '__main__':
12231243
unittest.main()

0 commit comments

Comments
 (0)