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.
- clarifai_grpc/__init__.py +33 -0
- clarifai_grpc/channel/clarifai_channel.py +150 -44
- clarifai_grpc/channel/custom_converters/custom_dict_to_message.py +41 -34
- clarifai_grpc/channel/custom_converters/custom_message_to_dict.py +85 -74
- clarifai_grpc/channel/errors.py +5 -0
- clarifai_grpc/channel/exceptions.py +1 -1
- clarifai_grpc/channel/grpc_json_channel.py +387 -261
- clarifai_grpc/channel/http_client.py +125 -104
- clarifai_grpc/grpc/api/resources_pb2.py +984 -7266
- clarifai_grpc/grpc/api/resources_pb2.pyi +17925 -0
- clarifai_grpc/grpc/api/resources_pb2_grpc.py +1 -0
- clarifai_grpc/grpc/api/service_pb2.py +1578 -8672
- clarifai_grpc/grpc/api/service_pb2.pyi +18269 -0
- clarifai_grpc/grpc/api/service_pb2_grpc.py +9746 -1859
- clarifai_grpc/grpc/api/status/status_code_pb2.py +33 -1334
- clarifai_grpc/grpc/api/status/status_code_pb2.pyi +1210 -0
- clarifai_grpc/grpc/api/status/status_code_pb2_grpc.py +1 -0
- clarifai_grpc/grpc/api/status/status_pb2.py +23 -149
- clarifai_grpc/grpc/api/status/status_pb2.pyi +174 -0
- clarifai_grpc/grpc/api/status/status_pb2_grpc.py +1 -0
- clarifai_grpc/grpc/api/utils/extensions_pb2.py +13 -46
- clarifai_grpc/grpc/api/utils/extensions_pb2.pyi +39 -0
- clarifai_grpc/grpc/api/utils/extensions_pb2_grpc.py +1 -0
- clarifai_grpc/grpc/api/utils/matrix_pb2.py +26 -0
- clarifai_grpc/grpc/api/utils/matrix_pb2.pyi +53 -0
- clarifai_grpc/grpc/api/utils/matrix_pb2_grpc.py +4 -0
- clarifai_grpc/grpc/api/utils/test_proto_pb2.py +17 -158
- clarifai_grpc/grpc/api/utils/test_proto_pb2.pyi +107 -0
- clarifai_grpc/grpc/api/utils/test_proto_pb2_grpc.py +1 -0
- clarifai_grpc/grpc/auth/scope/scope_pb2.py +245 -448
- clarifai_grpc/grpc/auth/scope/scope_pb2.pyi +550 -0
- clarifai_grpc/grpc/auth/scope/scope_pb2_grpc.py +1 -0
- clarifai_grpc/grpc/auth/types/types_pb2.py +11 -62
- clarifai_grpc/grpc/auth/types/types_pb2.pyi +78 -0
- clarifai_grpc/grpc/auth/types/types_pb2_grpc.py +1 -0
- clarifai_grpc/grpc/auth/util/extension_pb2.py +14 -68
- clarifai_grpc/grpc/auth/util/extension_pb2.pyi +68 -0
- clarifai_grpc/grpc/auth/util/extension_pb2_grpc.py +1 -0
- clarifai_grpc-11.10.3.dist-info/METADATA +124 -0
- clarifai_grpc-11.10.3.dist-info/RECORD +53 -0
- {clarifai_grpc-6.4.0.dist-info → clarifai_grpc-11.10.3.dist-info}/WHEEL +1 -1
- {clarifai_grpc-6.4.0.dist-info → clarifai_grpc-11.10.3.dist-info}/top_level.txt +0 -2
- clarifai_grpc-6.4.0.dist-info/METADATA +0 -88
- clarifai_grpc-6.4.0.dist-info/RECORD +0 -46
- scripts/__init__.py +0 -0
- scripts/app_and_key_for_tests.py +0 -180
- tests/__init__.py +0 -0
- tests/helpers.py +0 -105
- tests/test_integration.py +0 -243
- {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
|
|
8
|
-
|
|
7
|
+
from clarifai_grpc import __version__
|
|
9
8
|
from clarifai_grpc.channel.errors import ApiError
|
|
10
9
|
|
|
11
|
-
CLIENT_VERSION =
|
|
10
|
+
CLIENT_VERSION = __version__
|
|
12
11
|
OS_VER = os.sys.platform
|
|
13
|
-
PYTHON_VERSION =
|
|
14
|
-
map(
|
|
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(
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
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
|
-
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|