user-context-remote 0.0.95__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.95
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.95', # 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,11 +61,15 @@ 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
77
- user.__user_jwt = user_jwt
69
+ if user_jwt: # override _user_jwt defined in __login
70
+ user._user_jwt = user_jwt
71
+ elif "_user_jwt" not in user.__dict__:
72
+ user._user_jwt = None
78
73
  global_user = user
79
74
  return user
80
75
 
@@ -92,15 +87,13 @@ class UserContext(metaclass=GetterSetterGenerator):
92
87
  LOGIN_USING_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME = "login_using_user_identification_and_password"
93
88
  logger.start(LOGIN_USING_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME,
94
89
  object={"user_identifier": user_identifier, "password": password})
95
- if user_identifier is None:
96
- user_identifier = our_get_env("PRODUCT_USER_IDENTIFIER")
97
- if password is None:
98
- 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")
99
92
  if not user_identifier or not password:
100
93
  # To support cases with no PRODUCT_USER_IDENTIFIER and PRODUCT_PASSWORD in the deployment.
101
- logger.warning(
102
- "Warning: missing PRODUCT_USER_IDENTIFIER or PRODUCT_PASSWORD in .env file - WARNING -")
103
- 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)
104
97
  try:
105
98
  user = UserContext.__new__(cls, user_identifier=user_identifier, password=password)
106
99
  except Exception as e:
@@ -134,16 +127,16 @@ class UserContext(metaclass=GetterSetterGenerator):
134
127
 
135
128
  def get_effective_subscription_id(self) -> int:
136
129
  GET_EFFECTIVE_SUBSCRIPTION_ID_METHOD_NAME = "get_effective_subscription_id"
137
- logger.warning("user-context-python-package " + GET_EFFECTIVE_SUBSCRIPTION_ID_METHOD_NAME +
138
- " Please make sure get_effective_subscription_id() subscription_id=" + str(
139
- self.subscription_id))
130
+ logger.start(GET_EFFECTIVE_SUBSCRIPTION_ID_METHOD_NAME)
140
131
  warnings.warn(
141
132
  "Warning: autogenerated getter method is get_subscription_id, please use that method. - WARNING -",
142
133
  DeprecationWarning)
143
- 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
144
137
 
145
138
  def get_user_jwt(self) -> str:
146
- return self.__user_jwt
139
+ return self._user_jwt
147
140
 
148
141
  def __authenticate_by_user_identification_and_password(
149
142
  self, *, user_identifier: str, password: str) -> requests.Response:
@@ -171,12 +164,9 @@ class UserContext(metaclass=GetterSetterGenerator):
171
164
  " authentication_login_response_json.status_code != HTTPStatus.OK " + authentication_login_response_json.text)
172
165
  raise Exception(authentication_login_response_json.text)
173
166
  # Maybe there is no "data"
174
- logger.info("authentication_login_response_json.json()" + str(authentication_login_response_json))
175
- logger.info("authentication_login_response_json.json() with json()" + str(
176
- authentication_login_response_json.json()))
177
- self.__process_response(authentication_login_response_json)
178
167
  logger.end(AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME, object={
179
- "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()})
180
170
  return authentication_login_response_json
181
171
  # TODO Catch KeyError: 'data'
182
172
  except Exception as exception:
@@ -185,28 +175,6 @@ class UserContext(metaclass=GetterSetterGenerator):
185
175
  logger.end(AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME)
186
176
  raise exception
187
177
 
188
- def __process_response(self, authentication_login_response_json: requests.Response) -> None:
189
- try:
190
- response_json = authentication_login_response_json.json()
191
- data = response_json.get("data")
192
- if data is None:
193
- error_message = "Error: 'data' not found in authentication_login_response_json.json()"
194
- logger.error(error_message,
195
- object={"authentication_login_response_json": authentication_login_response_json.text,
196
- "response_json": response_json})
197
- raise Exception(error_message)
198
-
199
- else:
200
- self.user_jwt = data.get("userJwt")
201
- if self.user_jwt is None:
202
- logger.error("Error: 'userJwt' not found in 'data'.", object={
203
- "data": data, "authentication_login_response_json": authentication_login_response_json.text})
204
- except Exception as exception:
205
- error_message = "Error(Exception): user-context-remote-python _authenticate()"
206
- logger.exception(error_message, object={
207
- "authentication_login_response_json": authentication_login_response_json.text, "exception": exception})
208
- raise Exception(error_message)
209
-
210
178
  def __authenticate_by_user_jwt(self, user_jwt: str) -> requests.Response:
211
179
  AUTHENTICATE_BY_USER_JWT_METHOD_NAME = "_authenticate_by_user_jwt"
212
180
  logger.start(AUTHENTICATE_BY_USER_JWT_METHOD_NAME,
@@ -243,7 +211,7 @@ class UserContext(metaclass=GetterSetterGenerator):
243
211
  validate_user_jwt_data_dict = authenticate_product_user_response.json()
244
212
  data_dict = validate_user_jwt_data_dict.get('data')
245
213
  if "userJwt" in data_dict:
246
- self.__user_jwt = data_dict.get("userJwt")
214
+ self._user_jwt = data_dict.get("userJwt")
247
215
  first_name = last_name = None
248
216
  if not data_dict:
249
217
  logger.error(_GET_USER_DATA_LOGIN_RESPONSE_METHOD_NAME + " data from authenticate_product_user_response",
@@ -256,32 +224,29 @@ class UserContext(metaclass=GetterSetterGenerator):
256
224
  user_details: dict = data_dict.get("userDetails")
257
225
 
258
226
  if "profileId" in user_details:
259
- profile_id = user_details.get("profileId")
260
- self._set_real_profile_id(int(profile_id)) # TODO: set directly
261
- 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
262
230
 
263
231
  if "userId" in user_details:
264
- user_id = user_details.get("userId")
265
- self._set_effective_user_id(int(user_id))
266
- 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
267
235
 
268
236
  if "profilePreferredLangCode" in user_details:
269
237
  lang_code = user_details.get("profilePreferredLangCode")
270
- self._set_lang_code_str(lang_code or DEFAULT_LANG_CODE.value)
238
+ self.lang_code_str = lang_code or DEFAULT_LANG_CODE.value
271
239
  if "firstName" in user_details:
272
240
  first_name = user_details.get("firstName")
273
- self._set_real_first_name(first_name)
241
+ self.real_first_name = first_name
274
242
 
275
243
  if "lastName" in user_details:
276
244
  last_name = user_details.get("lastName")
277
- self._set_real_last_name(last_name)
245
+ self.real_last_name = last_name
278
246
 
279
247
  if "subscriptionId" in user_details:
280
248
  subscription_id = user_details.get("subscriptionId")
281
- logger.end(
282
- _GET_USER_DATA_LOGIN_RESPONSE_METHOD_NAME + " Got subscription_id from the cloud. subscription_id=" +
283
- str(subscription_id), object={"subscription_id": subscription_id})
284
- self._set_subscription_id(subscription_id)
249
+ self.subscription_id = subscription_id
285
250
 
286
251
  if first_name is not None and last_name is not None:
287
252
  name = first_name + " " + last_name
@@ -289,7 +254,7 @@ class UserContext(metaclass=GetterSetterGenerator):
289
254
  # If first_name and last_name are not available, use the email as the name
290
255
  name = user_details.get("email")
291
256
 
292
- self._set_real_name(name)
257
+ self.real_name = name
293
258
 
294
259
  def get_country_code(self) -> int: # TODO: temp solution
295
260
  return 972
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: user-context-remote
3
- Version: 0.0.95
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