user-context-remote 0.0.96__tar.gz → 0.0.97__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: user-context-remote
3
- Version: 0.0.96
3
+ Version: 0.0.97
4
4
  Summary: PyPI Package for Circles User Context Local/Remote Python
5
5
  Home-page: https://github.com/circles-zone/user-context-remote-python-package
6
6
  Author: Circles
@@ -5,7 +5,7 @@ package_dir = PACKAGE_NAME.replace("-", "_")
5
5
 
6
6
  setuptools.setup(
7
7
  name=PACKAGE_NAME,
8
- version='0.0.96', # https://pypi.org/project/user-context-remote/
8
+ version='0.0.97', # https://pypi.org/project/user-context-remote/
9
9
  author="Circles",
10
10
  author_email="info@circlez.ai",
11
11
  description="PyPI Package for Circles User Context Local/Remote Python",
@@ -1,4 +1,5 @@
1
1
  import json
2
+ import warnings
2
3
  from http import HTTPStatus
3
4
 
4
5
  import requests
@@ -7,8 +8,6 @@ from python_sdk_remote.mini_logger import MiniLogger as logger
7
8
  from python_sdk_remote.utilities import get_brand_name, get_environment_name, our_get_env
8
9
  from url_remote import action_name_enum, component_name_enum, entity_name_enum
9
10
  from url_remote.our_url import OurUrl
10
- from dataclasses import dataclass, field
11
- import warnings
12
11
 
13
12
  # TODO Shall we use authentication-remote-python-package and not directly authentication-local-restapi-typescript?
14
13
 
@@ -23,22 +22,15 @@ DEFAULT_LANG_CODE = LangCode.ENGLISH
23
22
 
24
23
  # MetaClass that dynamically genrates getters & setters.
25
24
  class GetterSetterGenerator(type):
26
- def __new__(cls, name, bases, dct):
27
- def make_getter(attr):
28
- def getter(self):
25
+ def __new__(cls, name: str, bases: tuple, dct: dict) -> type:
26
+ def make_getter(attr: str) -> callable:
27
+ def getter(self) -> any:
29
28
  return getattr(self, attr)
30
29
 
31
30
  return getter
32
31
 
33
- def make_setter(attr):
34
- def setter(self, value):
35
- setattr(self, attr, value)
36
-
37
- return setter
38
-
39
32
  for attr in dct.get('__annotations__', {}):
40
33
  dct[f'get_{attr}'] = make_getter(attr)
41
- dct[f'_set_{attr}'] = make_setter(attr) # TODO: get rid of those setters and block all outside setters
42
34
 
43
35
  return super().__new__(cls, name, bases, dct)
44
36
 
@@ -49,17 +41,16 @@ global_user = None
49
41
 
50
42
 
51
43
  # TODO: Impersonation methot which changes the effective user only upon login (but not the real one)
52
- @dataclass
53
44
  class UserContext(metaclass=GetterSetterGenerator):
54
- real_user_id: int = field(default=None)
55
- real_profile_id: int = field(default=None)
56
- effective_user_id: int = field(default=None)
57
- effective_profile_id: int = field(default=None)
58
- lang_code_str: str or None = field(default=None)
59
- real_first_name: str = field(default=None)
60
- real_last_name: str = field(default=None)
61
- real_name: str = field(default=None)
62
- subscription_id: int = field(default=5) # TODO temp solution, so we can debug
45
+ real_user_id: int = None
46
+ real_profile_id: int = None
47
+ effective_user_id: int = None
48
+ effective_profile_id: int = None
49
+ lang_code_str: str or None = None
50
+ real_first_name: str = None
51
+ real_last_name: str = None
52
+ real_name: str = None
53
+ subscription_id: int = 5 # TODO temp solution, so we can debug
63
54
 
64
55
  def __new__(cls, user_identifier: str = None, password: str = None, user_jwt: str = None):
65
56
  global global_user
@@ -70,7 +61,8 @@ class UserContext(metaclass=GetterSetterGenerator):
70
61
  user_identifier = user_identifier or our_get_env("PRODUCT_USER_IDENTIFIER")
71
62
  password = password or our_get_env("PRODUCT_PASSWORD")
72
63
  if not global_user or any(
73
- [user_identifier != user.__user_identifier, password != user.__password, user_jwt != user._user_jwt]):
64
+ [user_identifier != user.__user_identifier, password != user.__password,
65
+ (user_jwt and user_jwt != user._user_jwt)]):
74
66
  user.__login(user_identifier=user_identifier, password=password, user_jwt=user_jwt)
75
67
  user.__user_identifier = user_identifier
76
68
  user.__password = password
@@ -95,15 +87,13 @@ class UserContext(metaclass=GetterSetterGenerator):
95
87
  LOGIN_USING_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME = "login_using_user_identification_and_password"
96
88
  logger.start(LOGIN_USING_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME,
97
89
  object={"user_identifier": user_identifier, "password": password})
98
- if user_identifier is None:
99
- user_identifier = our_get_env("PRODUCT_USER_IDENTIFIER")
100
- if password is None:
101
- password = our_get_env("PRODUCT_PASSWORD")
90
+ user_identifier = user_identifier or our_get_env("PRODUCT_USER_IDENTIFIER")
91
+ password = password or our_get_env("PRODUCT_PASSWORD")
102
92
  if not user_identifier or not password:
103
93
  # To support cases with no PRODUCT_USER_IDENTIFIER and PRODUCT_PASSWORD in the deployment.
104
- logger.warning(
105
- "Warning: missing PRODUCT_USER_IDENTIFIER or PRODUCT_PASSWORD in .env file - WARNING -")
106
- return None
94
+ error_message = "login failed, check PRODUCT_USER_IDENTIFIER and PRODUCT_PASSWORD in .env file"
95
+ logger.error(error_message)
96
+ raise Exception(error_message)
107
97
  try:
108
98
  user = UserContext.__new__(cls, user_identifier=user_identifier, password=password)
109
99
  except Exception as e:
@@ -137,13 +127,13 @@ class UserContext(metaclass=GetterSetterGenerator):
137
127
 
138
128
  def get_effective_subscription_id(self) -> int:
139
129
  GET_EFFECTIVE_SUBSCRIPTION_ID_METHOD_NAME = "get_effective_subscription_id"
140
- logger.warning("user-context-python-package " + GET_EFFECTIVE_SUBSCRIPTION_ID_METHOD_NAME +
141
- " Please make sure get_effective_subscription_id() subscription_id=" + str(
142
- self.subscription_id))
130
+ logger.start(GET_EFFECTIVE_SUBSCRIPTION_ID_METHOD_NAME)
143
131
  warnings.warn(
144
132
  "Warning: autogenerated getter method is get_subscription_id, please use that method. - WARNING -",
145
133
  DeprecationWarning)
146
- return self.subscription_id
134
+ subscription_id = self.subscription_id
135
+ logger.end(GET_EFFECTIVE_SUBSCRIPTION_ID_METHOD_NAME, object={"subscription_id": subscription_id})
136
+ return subscription_id
147
137
 
148
138
  def get_user_jwt(self) -> str:
149
139
  return self._user_jwt
@@ -174,12 +164,9 @@ class UserContext(metaclass=GetterSetterGenerator):
174
164
  " authentication_login_response_json.status_code != HTTPStatus.OK " + authentication_login_response_json.text)
175
165
  raise Exception(authentication_login_response_json.text)
176
166
  # Maybe there is no "data"
177
- logger.info("authentication_login_response_json.json()" + str(authentication_login_response_json))
178
- logger.info("authentication_login_response_json.json() with json()" + str(
179
- authentication_login_response_json.json()))
180
- self.__process_response(authentication_login_response_json)
181
167
  logger.end(AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME, object={
182
- "user_jwt": self.user_jwt})
168
+ "authentication_login_response_json": authentication_login_response_json,
169
+ "authentication_login_response_json.json()": authentication_login_response_json.json()})
183
170
  return authentication_login_response_json
184
171
  # TODO Catch KeyError: 'data'
185
172
  except Exception as exception:
@@ -188,28 +175,6 @@ class UserContext(metaclass=GetterSetterGenerator):
188
175
  logger.end(AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME)
189
176
  raise exception
190
177
 
191
- def __process_response(self, authentication_login_response_json: requests.Response) -> None:
192
- try:
193
- response_json = authentication_login_response_json.json()
194
- data = response_json.get("data")
195
- if data is None:
196
- error_message = "Error: 'data' not found in authentication_login_response_json.json()"
197
- logger.error(error_message,
198
- object={"authentication_login_response_json": authentication_login_response_json.text,
199
- "response_json": response_json})
200
- raise Exception(error_message)
201
-
202
- else:
203
- self.user_jwt = data.get("userJwt")
204
- if self.user_jwt is None:
205
- logger.error("Error: 'userJwt' not found in 'data'.", object={
206
- "data": data, "authentication_login_response_json": authentication_login_response_json.text})
207
- except Exception as exception:
208
- error_message = "Error(Exception): user-context-remote-python _authenticate()"
209
- logger.exception(error_message, object={
210
- "authentication_login_response_json": authentication_login_response_json.text, "exception": exception})
211
- raise Exception(error_message)
212
-
213
178
  def __authenticate_by_user_jwt(self, user_jwt: str) -> requests.Response:
214
179
  AUTHENTICATE_BY_USER_JWT_METHOD_NAME = "_authenticate_by_user_jwt"
215
180
  logger.start(AUTHENTICATE_BY_USER_JWT_METHOD_NAME,
@@ -259,32 +224,29 @@ class UserContext(metaclass=GetterSetterGenerator):
259
224
  user_details: dict = data_dict.get("userDetails")
260
225
 
261
226
  if "profileId" in user_details:
262
- profile_id = user_details.get("profileId")
263
- self._set_real_profile_id(int(profile_id)) # TODO: set directly
264
- self._set_effective_profile_id(int(profile_id))
227
+ profile_id = int(user_details.get("profileId"))
228
+ self.real_profile_id = profile_id
229
+ self.effective_profile_id = profile_id
265
230
 
266
231
  if "userId" in user_details:
267
- user_id = user_details.get("userId")
268
- self._set_effective_user_id(int(user_id))
269
- self._set_real_user_id(int(user_id))
232
+ user_id = int(user_details.get("userId"))
233
+ self.effective_user_id = user_id
234
+ self.real_user_id = user_id
270
235
 
271
236
  if "profilePreferredLangCode" in user_details:
272
237
  lang_code = user_details.get("profilePreferredLangCode")
273
- self._set_lang_code_str(lang_code or DEFAULT_LANG_CODE.value)
238
+ self.lang_code_str = lang_code or DEFAULT_LANG_CODE.value
274
239
  if "firstName" in user_details:
275
240
  first_name = user_details.get("firstName")
276
- self._set_real_first_name(first_name)
241
+ self.real_first_name = first_name
277
242
 
278
243
  if "lastName" in user_details:
279
244
  last_name = user_details.get("lastName")
280
- self._set_real_last_name(last_name)
245
+ self.real_last_name = last_name
281
246
 
282
247
  if "subscriptionId" in user_details:
283
248
  subscription_id = user_details.get("subscriptionId")
284
- logger.end(
285
- _GET_USER_DATA_LOGIN_RESPONSE_METHOD_NAME + " Got subscription_id from the cloud. subscription_id=" +
286
- str(subscription_id), object={"subscription_id": subscription_id})
287
- self._set_subscription_id(subscription_id)
249
+ self.subscription_id = subscription_id
288
250
 
289
251
  if first_name is not None and last_name is not None:
290
252
  name = first_name + " " + last_name
@@ -292,7 +254,7 @@ class UserContext(metaclass=GetterSetterGenerator):
292
254
  # If first_name and last_name are not available, use the email as the name
293
255
  name = user_details.get("email")
294
256
 
295
- self._set_real_name(name)
257
+ self.real_name = name
296
258
 
297
259
  def get_country_code(self) -> int: # TODO: temp solution
298
260
  return 972
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: user-context-remote
3
- Version: 0.0.96
3
+ Version: 0.0.97
4
4
  Summary: PyPI Package for Circles User Context Local/Remote Python
5
5
  Home-page: https://github.com/circles-zone/user-context-remote-python-package
6
6
  Author: Circles