clarifai-grpc 6.4.0__py3-none-any.whl → 11.10.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. clarifai_grpc/__init__.py +33 -0
  2. clarifai_grpc/channel/clarifai_channel.py +150 -44
  3. clarifai_grpc/channel/custom_converters/custom_dict_to_message.py +41 -34
  4. clarifai_grpc/channel/custom_converters/custom_message_to_dict.py +85 -74
  5. clarifai_grpc/channel/errors.py +5 -0
  6. clarifai_grpc/channel/exceptions.py +1 -1
  7. clarifai_grpc/channel/grpc_json_channel.py +387 -261
  8. clarifai_grpc/channel/http_client.py +125 -104
  9. clarifai_grpc/grpc/api/resources_pb2.py +984 -7266
  10. clarifai_grpc/grpc/api/resources_pb2.pyi +17925 -0
  11. clarifai_grpc/grpc/api/resources_pb2_grpc.py +1 -0
  12. clarifai_grpc/grpc/api/service_pb2.py +1578 -8672
  13. clarifai_grpc/grpc/api/service_pb2.pyi +18269 -0
  14. clarifai_grpc/grpc/api/service_pb2_grpc.py +9746 -1859
  15. clarifai_grpc/grpc/api/status/status_code_pb2.py +33 -1334
  16. clarifai_grpc/grpc/api/status/status_code_pb2.pyi +1210 -0
  17. clarifai_grpc/grpc/api/status/status_code_pb2_grpc.py +1 -0
  18. clarifai_grpc/grpc/api/status/status_pb2.py +23 -149
  19. clarifai_grpc/grpc/api/status/status_pb2.pyi +174 -0
  20. clarifai_grpc/grpc/api/status/status_pb2_grpc.py +1 -0
  21. clarifai_grpc/grpc/api/utils/extensions_pb2.py +13 -46
  22. clarifai_grpc/grpc/api/utils/extensions_pb2.pyi +39 -0
  23. clarifai_grpc/grpc/api/utils/extensions_pb2_grpc.py +1 -0
  24. clarifai_grpc/grpc/api/utils/matrix_pb2.py +26 -0
  25. clarifai_grpc/grpc/api/utils/matrix_pb2.pyi +53 -0
  26. clarifai_grpc/grpc/api/utils/matrix_pb2_grpc.py +4 -0
  27. clarifai_grpc/grpc/api/utils/test_proto_pb2.py +17 -158
  28. clarifai_grpc/grpc/api/utils/test_proto_pb2.pyi +107 -0
  29. clarifai_grpc/grpc/api/utils/test_proto_pb2_grpc.py +1 -0
  30. clarifai_grpc/grpc/auth/scope/scope_pb2.py +245 -448
  31. clarifai_grpc/grpc/auth/scope/scope_pb2.pyi +550 -0
  32. clarifai_grpc/grpc/auth/scope/scope_pb2_grpc.py +1 -0
  33. clarifai_grpc/grpc/auth/types/types_pb2.py +11 -62
  34. clarifai_grpc/grpc/auth/types/types_pb2.pyi +78 -0
  35. clarifai_grpc/grpc/auth/types/types_pb2_grpc.py +1 -0
  36. clarifai_grpc/grpc/auth/util/extension_pb2.py +14 -68
  37. clarifai_grpc/grpc/auth/util/extension_pb2.pyi +68 -0
  38. clarifai_grpc/grpc/auth/util/extension_pb2_grpc.py +1 -0
  39. clarifai_grpc-11.10.3.dist-info/METADATA +124 -0
  40. clarifai_grpc-11.10.3.dist-info/RECORD +53 -0
  41. {clarifai_grpc-6.4.0.dist-info → clarifai_grpc-11.10.3.dist-info}/WHEEL +1 -1
  42. {clarifai_grpc-6.4.0.dist-info → clarifai_grpc-11.10.3.dist-info}/top_level.txt +0 -2
  43. clarifai_grpc-6.4.0.dist-info/METADATA +0 -88
  44. clarifai_grpc-6.4.0.dist-info/RECORD +0 -46
  45. scripts/__init__.py +0 -0
  46. scripts/app_and_key_for_tests.py +0 -180
  47. tests/__init__.py +0 -0
  48. tests/helpers.py +0 -105
  49. tests/test_integration.py +0 -243
  50. {clarifai_grpc-6.4.0.dist-info → clarifai_grpc-11.10.3.dist-info/licenses}/LICENSE +0 -0
@@ -4,122 +4,143 @@ import logging
4
4
  import os
5
5
  import typing # noqa
6
6
 
7
- import requests
8
-
7
+ from clarifai_grpc import __version__
9
8
  from clarifai_grpc.channel.errors import ApiError
10
9
 
11
- CLIENT_VERSION = '6.4.0'
10
+ CLIENT_VERSION = __version__
12
11
  OS_VER = os.sys.platform
13
- PYTHON_VERSION = '.'.join(
14
- map(str, [os.sys.version_info.major, os.sys.version_info.minor, os.sys.version_info.micro]))
15
-
12
+ PYTHON_VERSION = ".".join(
13
+ map(
14
+ str,
15
+ [
16
+ os.sys.version_info.major,
17
+ os.sys.version_info.minor,
18
+ os.sys.version_info.micro,
19
+ ],
20
+ )
21
+ )
16
22
 
17
- logger = logging.getLogger('clarifai')
23
+ logger = logging.getLogger("clarifai")
18
24
 
19
25
 
20
26
  class HttpClient:
27
+ def __init__(self, session, auth_string): # type: (requests.Session, str) -> None
28
+ """
29
+ :param session: The requests session object.
30
+ :param auth_string: Either Clarifai's API key or Personal Access Token.
31
+ """
32
+ self._auth_string = auth_string
33
+ self._session = session
21
34
 
22
- def __init__(self, session, auth_string): # type: (requests.Session, str) -> None
23
- """
24
- :param session: The requests session object.
25
- :param auth_string: Either Clarifai's API key or Personal Access Token.
26
- """
27
- self._auth_string = auth_string
28
- self._session = session
35
+ def execute_request(self, method, params, url):
36
+ # type: (str, typing.Optional[dict], str) -> dict
37
+ headers = {
38
+ "Content-Type": "application/json",
39
+ "X-Clarifai-gRPC-Client": "python:%s" % CLIENT_VERSION,
40
+ "Python-Client": "%s:%s" % (OS_VER, PYTHON_VERSION),
41
+ "X-Clarifai-Request-ID-Prefix": f"python-json-{CLIENT_VERSION}",
42
+ "Authorization": "Key %s" % self._auth_string,
43
+ }
44
+ logger.debug("=" * 100)
45
+ succinct_payload = self._mangle_base64_values(params)
46
+ logger.debug(
47
+ "%s %s\nHEADERS:\n%s\nPAYLOAD:\n%s",
48
+ method,
49
+ url,
50
+ json.dumps(headers, indent=2),
51
+ json.dumps(succinct_payload, indent=2),
52
+ )
53
+ # Avoid import at the top so we don't depend on requests in requirements.txt
54
+ import requests # noqa
29
55
 
30
- def execute_request(self, method, params, url):
31
- # type: (str, typing.Optional[dict], str) -> dict
32
- headers = {
33
- 'Content-Type': 'application/json',
34
- 'X-Clarifai-gRPC-Client': 'python:%s' % CLIENT_VERSION,
35
- 'Python-Client': '%s:%s' % (OS_VER, PYTHON_VERSION),
36
- 'Authorization': 'Key %s' % self._auth_string
37
- }
38
- logger.debug("=" * 100)
39
- succinct_payload = self._mangle_base64_values(params)
40
- logger.debug("%s %s\nHEADERS:\n%s\nPAYLOAD:\n%s", method, url, json.dumps(headers, indent=2),
41
- json.dumps(succinct_payload, indent=2))
42
- try:
43
- if method == 'GET':
44
- res = self._session.get(url, params=self._encode_get_params(params), headers=headers)
45
- elif method == "POST":
46
- res = self._session.post(url, data=json.dumps(params), headers=headers)
47
- elif method == "DELETE":
48
- res = self._session.delete(url, data=json.dumps(params), headers=headers)
49
- elif method == "PATCH":
50
- res = self._session.patch(url, data=json.dumps(params), headers=headers)
51
- elif method == "PUT":
52
- res = self._session.put(url, data=json.dumps(params), headers=headers)
53
- else:
54
- raise Exception("Unsupported request type: '%s'" % method)
55
- except requests.RequestException as e:
56
- raise ApiError(url, params, method, e.response)
57
- try:
58
- response_json = json.loads(res.content.decode('utf-8'))
59
- except ValueError:
60
- logger.exception("Could not get valid JSON from server response.")
61
- logger.debug("\nRESULT:\n%s", json.dumps(res.text, indent=2))
62
- error = ApiError(url, params, method, res)
63
- raise error
64
- else:
65
- logger.debug("\nRESULT:\n%s", json.dumps(response_json, indent=2))
66
- return response_json
56
+ try:
57
+ if method == "GET":
58
+ res = self._session.get(
59
+ url, params=self._encode_get_params(params), headers=headers
60
+ )
61
+ elif method == "POST":
62
+ res = self._session.post(url, data=json.dumps(params), headers=headers)
63
+ elif method == "DELETE":
64
+ res = self._session.delete(url, data=json.dumps(params), headers=headers)
65
+ elif method == "PATCH":
66
+ res = self._session.patch(url, data=json.dumps(params), headers=headers)
67
+ elif method == "PUT":
68
+ res = self._session.put(url, data=json.dumps(params), headers=headers)
69
+ else:
70
+ raise Exception("Unsupported request type: '%s'" % method)
71
+ except requests.RequestException as e:
72
+ raise ApiError(url, params, method, e.response)
73
+ try:
74
+ response_json = json.loads(res.content.decode("utf-8"))
75
+ except ValueError:
76
+ logger.exception("Could not get valid JSON from server response.")
77
+ logger.debug("\nRESULT:\n%s", json.dumps(res.text, indent=2))
78
+ error = ApiError(url, params, method, res)
79
+ raise error
80
+ else:
81
+ logger.debug("\nRESULT:\n%s", json.dumps(response_json, indent=2))
82
+ return response_json
67
83
 
68
- def _mangle_base64_values(self, params): # type: (dict) -> dict
69
- """ Mangle (shorten) the base64 values because they are too long for output. """
70
- inputs = (params or {}).get('inputs')
71
- query = (params or {}).get('query')
72
- if inputs and len(inputs) > 0:
73
- return self._mangle_base64_values_in_inputs(params)
74
- if query and query.get('ands'):
75
- return self._mangle_base64_values_in_query(params)
76
- return params
84
+ def _mangle_base64_values(self, params): # type: (dict) -> dict
85
+ """Mangle (shorten) the base64 values because they are too long for output."""
86
+ inputs = (params or {}).get("inputs")
87
+ query = (params or {}).get("query")
88
+ if inputs and len(inputs) > 0:
89
+ return self._mangle_base64_values_in_inputs(params)
90
+ if query and query.get("ands"):
91
+ return self._mangle_base64_values_in_query(params)
92
+ return params
77
93
 
78
- def _mangle_base64_values_in_inputs(self, params): # type: (dict) -> dict
79
- params_copy = copy.deepcopy(params)
80
- for data in params_copy['inputs']:
81
- data = data['data']
82
- image = data.get('image')
83
- if image and image.get('base64'):
84
- image['base64'] = self._shortened_base64_value(image['base64'])
94
+ def _mangle_base64_values_in_inputs(self, params): # type: (dict) -> dict
95
+ params_copy = copy.deepcopy(params)
96
+ for input in params_copy["inputs"]:
97
+ if "data" not in input:
98
+ continue
99
+ data = input["data"]
100
+ image = data.get("image")
101
+ if image and image.get("base64"):
102
+ image["base64"] = self._shortened_base64_value(image["base64"])
85
103
 
86
- video = data.get('video')
87
- if video and video.get('base64'):
88
- video['base64'] = self._shortened_base64_value(video['base64'])
89
- return params_copy
104
+ video = data.get("video")
105
+ if video and video.get("base64"):
106
+ video["base64"] = self._shortened_base64_value(video["base64"])
107
+ return params_copy
90
108
 
91
- def _mangle_base64_values_in_query(self, params): # type: (dict) -> dict
92
- params_copy = copy.deepcopy(params)
93
- queries = params_copy['query']['ands']
94
- for query in queries:
95
- image = query.get('output', {}).get('input', {}).get('data', {}).get('image', {})
96
- base64_val = image.get('base64')
97
- if base64_val:
98
- image['base64'] = self._shortened_base64_value(base64_val)
99
- return params_copy
109
+ def _mangle_base64_values_in_query(self, params): # type: (dict) -> dict
110
+ params_copy = copy.deepcopy(params)
111
+ queries = params_copy["query"]["ands"]
112
+ for query in queries:
113
+ image = query.get("output", {}).get("input", {}).get("data", {}).get("image", {})
114
+ base64_val = image.get("base64")
115
+ if base64_val:
116
+ image["base64"] = self._shortened_base64_value(base64_val)
117
+ return params_copy
100
118
 
101
- def _shortened_base64_value(self, original_base64): # type: (str) -> str
102
- # Shorten the value if larger than what we shorten to (10 + 6 + 10).
103
- if len(original_base64) > 36:
104
- return original_base64[:10] + '......' + original_base64[-10:]
105
- else:
106
- return original_base64
119
+ def _shortened_base64_value(self, original_base64): # type: (str) -> str
120
+ # Shorten the value if larger than what we shorten to (10 + 6 + 10).
121
+ if len(original_base64) > 36:
122
+ return original_base64[:10] + "......" + original_base64[-10:]
123
+ else:
124
+ return original_base64
107
125
 
108
- def _encode_get_params(self, params):
109
- """
110
- Encodes message params into format for use in GET args
111
- """
112
- encoded_params = {}
113
- for (k, v) in params.items():
114
- if isinstance(v, str):
115
- encoded_params[k] = v
116
- elif isinstance(v, bytes):
117
- encoded_params[k] = v.decode("utf-8")
118
- elif isinstance(v, (int, float, bool)):
119
- encoded_params[k] = str(v)
120
- elif isinstance(v, dict):
121
- for (subk, subv) in self._encode_get_params(v).items():
122
- encoded_params[k + '.' + subk] = subv
123
- else:
124
- raise TypeError('Cannot convert type for get params: %s' % type(v))
125
- return encoded_params
126
+ def _encode_get_params(self, params):
127
+ """
128
+ Encodes message params into format for use in GET args
129
+ """
130
+ encoded_params = {}
131
+ for k, v in params.items():
132
+ if isinstance(v, str):
133
+ encoded_params[k] = v
134
+ elif isinstance(v, bytes):
135
+ encoded_params[k] = v.decode("utf-8")
136
+ elif isinstance(v, (int, float, bool)):
137
+ encoded_params[k] = str(v)
138
+ elif isinstance(v, dict):
139
+ for subk, subv in self._encode_get_params(v).items():
140
+ encoded_params[k + "." + subk] = subv
141
+ elif isinstance(v, list):
142
+ if v:
143
+ encoded_params[k] = v
144
+ else:
145
+ raise TypeError("Cannot convert type for get params: %s" % type(v))
146
+ return encoded_params