user-context-remote 0.0.41__tar.gz → 0.0.43__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.41
3
+ Version: 0.0.43
4
4
  Summary: PyPI Package for Circles User Context Local/Remote Python
5
5
  Home-page: https://github.com/circles/{PACKAGE_NAME}-python-package
6
6
  Author: Circles
@@ -16,5 +16,6 @@ Requires-Dist: language-local>=0.0.6
16
16
  Requires-Dist: url-local>=0.0.15
17
17
  Requires-Dist: requests>=2.31.0
18
18
  Requires-Dist: httpstatus35>=0.0.1
19
+ Requires-Dist: python-sdk-local>=0.0.44
19
20
 
20
21
  This is a package for sharing common user-context-remote functions used in different repositories
@@ -9,7 +9,7 @@ package_dir = PACKAGE_NAME.replace("-", "_")
9
9
 
10
10
  setuptools.setup(
11
11
  name='user-context-remote',
12
- version='0.0.41', # https://pypi.org/project/user-context-remote/
12
+ version='0.0.43', # https://pypi.org/project/user-context-remote/
13
13
  author="Circles",
14
14
  author_email="info@circles.life",
15
15
  # TODO: Please update the description and delete this line
@@ -34,5 +34,6 @@ setuptools.setup(
34
34
  'url-local>=0.0.15',
35
35
  'requests>=2.31.0',
36
36
  'httpstatus35>=0.0.1',
37
+ 'python-sdk-local>=0.0.44',
37
38
  ],
38
39
  )
@@ -1,26 +1,23 @@
1
1
  import os
2
2
  from url_local.url_circlez import OurUrl
3
3
  import json
4
- from typing import Any
5
- import sys
6
4
  from url_local import action_name_enum, entity_name_enum, component_name_enum
7
5
  from language_local.lang_code import LangCode
8
6
  from dotenv import load_dotenv
9
7
  import requests
10
8
  from httpstatus import HTTPStatus
9
+ # TODO Shall we remove the src?
11
10
  from sdk.src.mini_logger import MiniLogger
12
- # from circles_local_database_python.connector import Connector
11
+ from sdk.src.validate import validate_enviroment_variables
13
12
 
14
- # from logger_local.Logger import Logger
15
13
  load_dotenv()
16
-
17
- # TODO Use python-sdk to get and check the environment variables instead of those two statements
18
- BRAND_NAME = os.getenv('BRAND_NAME')
19
- ENVIORNMENT_NAME = os.getenv('ENVIRONMENT_NAME')
14
+ BRAND_NAME = os.getenv("BRAND_NAME")
15
+ ENVIORNMENT_NAME = os.getenv("ENVIRONMENT_NAME")
16
+ validate_enviroment_variables()
20
17
 
21
18
  AUTHENTICATION_API_VERSION = 1
22
19
 
23
-
20
+
24
21
  class UserContext:
25
22
  _instance = None
26
23
 
@@ -43,7 +40,7 @@ class UserContext:
43
40
  self.real_first_name = None
44
41
  self.real_last_name = None
45
42
  self.real_display_name = None
46
- self.subscription_id = 5 # TODO temp solution, so we can debug
43
+ self.subscription_id = 5 # TODO temp solution, so we can debug
47
44
  if (user_identifier is not None and password is not None):
48
45
  self.user_JWT = None
49
46
  data = self._authenticate_by_user_identification_and_password(
@@ -52,113 +49,115 @@ class UserContext:
52
49
  self.user_JWT = user_JWT
53
50
  data = self._authenticate_by_user_JWT(user_JWT=user_JWT)
54
51
  # Populate the private data members with data we receieved from Authentication Local REST-API https://github.com/circles-zone/authentication-local-restapi-typescript-serverless-com/edit/dev/auth-restapi-serverless-com/src/services/auth-service/auth-service-impl.ts
55
- self.get_user_data_login_response(validate_jwt_response=data)
52
+ self._get_user_data_login_response(validate_jwt_response=data)
56
53
 
57
54
  @staticmethod
58
55
  def login_using_user_identification_and_password(user_identifier: str = None, password: str = None):
59
56
  LOGIN_USING_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME = "login_using_user_identification_and_password"
60
- #MiniLogger.start(LOGIN_USING_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME, object={"username": user_identifier, "password": password})
57
+ # MiniLogger.start(LOGIN_USING_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME, object={"username": user_identifier, "password": password})
61
58
  if UserContext._instance is None:
62
59
  if user_identifier is None:
63
60
  try:
64
61
  user_identifier = os.getenv("PRODUCT_USER_IDENTIFIER")
65
62
  except Exception as e:
66
- MiniLogger.error("Exception missing PRODUCT_USER_IDENTIFIER in .env file - ERROR - " + str(e))
63
+ MiniLogger.error(
64
+ "Exception missing PRODUCT_USER_IDENTIFIER in .env file - ERROR - " + str(e))
67
65
  if password is None:
68
66
  try:
69
67
  password = os.getenv("PRODUCT_PASSWORD")
70
68
  except Exception as e:
71
- MiniLogger.error("Exception missing PRODUCT_PASSWORD in .env file - ERROE -" + str(e))
69
+ MiniLogger.error(
70
+ "Exception missing PRODUCT_PASSWORD in .env file - ERROE -" + str(e))
72
71
  if user_identifier is None or password is None or user_identifier == "" or password == "":
73
72
  # To support cases when there is no PRODUCT_USERNAME and PRODUCT_PASSWORD in the deployment.
74
73
  return None
75
74
  UserContext._instance = UserContext(
76
75
  user_identifier=user_identifier, password=password)
77
76
  user = UserContext._instance
78
- #MiniLogger.end(LOGIN_USING_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME,object={"user": str(user)})
77
+ # MiniLogger.end(LOGIN_USING_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME,object={"user": str(user)})
79
78
  return user
80
79
 
81
80
  @staticmethod
82
81
  def login_using_jwt(user_JWT: str = None):
83
82
  LOGIN_USING_JWT_METHOD_NAME = "login_using_jwt"
84
- #MiniLogger.start(LOGIN_USING_JWT_METHOD_NAME ,object={"user_JWT": user_JWT})
83
+ # MiniLogger.start(LOGIN_USING_JWT_METHOD_NAME ,object={"user_JWT": user_JWT})
85
84
  if UserContext._instance is None and user_JWT is not None:
86
85
  UserContext._instance = UserContext(user_JWT=user_JWT)
87
86
  user = UserContext._instance
88
- #MiniLogger.end(LOGIN_USING_JWT_METHOD_NAME,object={"user": str(user)})
87
+ # MiniLogger.end(LOGIN_USING_JWT_METHOD_NAME,object={"user": str(user)})
89
88
  return user
90
89
 
91
90
  def _set_real_user_id(self, user_id: int) -> None:
92
91
  _SET_REAL_USER_ID_METHOD_NAME = "_set_real_user_id"
93
- #MiniLogger.start(_SET_REAL_USER_ID_METHOD_NAME,object={"user_id": user_id})
92
+ # MiniLogger.start(_SET_REAL_USER_ID_METHOD_NAME,object={"user_id": user_id})
94
93
  self.real_user_id = user_id
95
- #MiniLogger.end(_SET_REAL_USER_ID_METHOD_NAME)
94
+ # MiniLogger.end(_SET_REAL_USER_ID_METHOD_NAME)
96
95
 
97
96
  def _set_real_profile_id(self, profile_id: int) -> None:
98
97
  _SET_REAL_PROFILE_ID_METHOD_NAME = "_set_real_profile_id"
99
- #MiniLogger.start(_SET_REAL_PROFILE_ID_METHOD_NAME,object={"profile_id": profile_id})
98
+ # MiniLogger.start(_SET_REAL_PROFILE_ID_METHOD_NAME,object={"profile_id": profile_id})
100
99
  self.real_profile_id = profile_id
101
- #MiniLogger.end(_SET_REAL_PROFILE_ID_METHOD_NAME)
100
+ # MiniLogger.end(_SET_REAL_PROFILE_ID_METHOD_NAME)
102
101
 
103
102
  def get_real_user_id(self) -> int:
104
103
  GET_REAL_USER_ID_METHOD_NAME = "get_real_user_id"
105
- #MiniLogger.start(GET_REAL_USER_ID_METHOD_NAME)
106
- #MiniLogger.end(GET_REAL_USER_ID_METHOD_NAME,object={"user_id": self.real_user_id})
104
+ # MiniLogger.start(GET_REAL_USER_ID_METHOD_NAME)
105
+ # MiniLogger.end(GET_REAL_USER_ID_METHOD_NAME,object={"user_id": self.real_user_id})
107
106
  return self.real_user_id
108
107
 
109
108
  def get_real_profile_id(self) -> int:
110
109
  GET_REAL_PROFILE_ID_METHOD_NAME = "get_real_profile_id"
111
- #MiniLogger.start(GET_REAL_PROFILE_ID_METHOD_NAME)
112
- #MiniLogger.end(GET_REAL_PROFILE_ID_METHOD_NAME,object={"user_id": self.real_profile_id})
110
+ # MiniLogger.start(GET_REAL_PROFILE_ID_METHOD_NAME)
111
+ # MiniLogger.end(GET_REAL_PROFILE_ID_METHOD_NAME,object={"user_id": self.real_profile_id})
113
112
  return self.real_profile_id
114
113
 
115
114
  def get_effective_profile_preferred_lang_code(self) -> str:
116
115
  GET_CURENT_LANG_CODE_METHOD_NAME = "get_effective_profile_preferred_lang_code"
117
- #MiniLogger.start(GET_CURENT_LANG_CODE_METHOD_NAME)
118
- #MiniLogger.end(GET_CURENT_LANG_CODE_METHOD_NAME,object={"profilePreferredLangCode": self.profilePreferredLangCode})
119
- return self.profilePreferredLangCode
116
+ # MiniLogger.start(GET_CURENT_LANG_CODE_METHOD_NAME)
117
+ # MiniLogger.end(GET_CURENT_LANG_CODE_METHOD_NAME,object={"lang_code": self.lang_code})
118
+ return self.lang_code
120
119
 
121
- def _set_effective_profile_preferred_lang_code(self, profilePreferredLangCode: LangCode) -> None:
120
+ def _set_effective_profile_preferred_lang_code(self, lang_code: LangCode) -> None:
122
121
  _SET_CURRENT_LANG_CODE_METHOD_NAME = "_set_current_lang_code"
123
- #MiniLogger.start(_SET_CURRENT_LANG_CODE_METHOD_NAME,object={"profilePreferredLangCode": profilePreferredLangCode.value})
124
- self.profilePreferredLangCode = profilePreferredLangCode.value
125
- #MiniLogger.end(_SET_CURRENT_LANG_CODE_METHOD_NAME)
122
+ # MiniLogger.start(_SET_CURRENT_LANG_CODE_METHOD_NAME,object={"lang_code": lang_code.value})
123
+ self.lang_code = lang_code.value
124
+ # MiniLogger.end(_SET_CURRENT_LANG_CODE_METHOD_NAME)
126
125
 
127
126
  def _set_real_first_name(self, first_name: str) -> None:
128
127
  _SET_REAL_FIRST_NAME_METHOD_NAME = "_set_real_first_name"
129
- #MiniLogger.start(_SET_REAL_FIRST_NAME_METHOD_NAME,object={"first_name": first_name})
128
+ # MiniLogger.start(_SET_REAL_FIRST_NAME_METHOD_NAME,object={"first_name": first_name})
130
129
  self.real_first_name = first_name
131
- #MiniLogger.end(_SET_REAL_FIRST_NAME_METHOD_NAME)
130
+ # MiniLogger.end(_SET_REAL_FIRST_NAME_METHOD_NAME)
132
131
 
133
132
  def _set_real_last_name(self, last_name: str) -> None:
134
133
  _SET_REAL_LAST_NAME_METHOD_NAME = "_set_real_last_name"
135
- #MiniLogger.start(_SET_REAL_LAST_NAME_METHOD_NAME,object={"first_name": last_name})
134
+ # MiniLogger.start(_SET_REAL_LAST_NAME_METHOD_NAME,object={"first_name": last_name})
136
135
  self.real_last_name = last_name
137
- #MiniLogger.end(_SET_REAL_LAST_NAME_METHOD_NAME)
136
+ # MiniLogger.end(_SET_REAL_LAST_NAME_METHOD_NAME)
138
137
 
139
138
  def get_real_first_name(self) -> str:
140
139
  GET_REAL_FIRST_NAME_METHOD_NAME = "get_real_first_name"
141
- #MiniLogger.start(GET_REAL_FIRST_NAME_METHOD_NAME)
142
- #MiniLogger.end(GET_REAL_FIRST_NAME_METHOD_NAME,object={"first_name": self.real_first_name})
140
+ # MiniLogger.start(GET_REAL_FIRST_NAME_METHOD_NAME)
141
+ # MiniLogger.end(GET_REAL_FIRST_NAME_METHOD_NAME,object={"first_name": self.real_first_name})
143
142
  return self.real_first_name
144
143
 
145
144
  def get_real_last_name(self) -> str:
146
- # TODO Change all those to private
145
+ # TODO Change all those to private
147
146
  GET_REAL_LAST_NAME_METHOD_NAME = "get_real_last_name"
148
- #MiniLogger.start(GET_REAL_LAST_NAME_METHOD_NAME)
149
- #MiniLogger.end(GET_REAL_LAST_NAME_METHOD_NAME,object={"last_name": self.real_last_name})
147
+ # MiniLogger.start(GET_REAL_LAST_NAME_METHOD_NAME)
148
+ # MiniLogger.end(GET_REAL_LAST_NAME_METHOD_NAME,object={"last_name": self.real_last_name})
150
149
  return self.real_last_name
151
150
 
152
151
  def _set_real_name(self, name: str) -> None:
153
152
  _SET_REAL_NAME_METHOD_NAME = "_set_real_name"
154
- #MiniLogger.start(_SET_REAL_NAME_METHOD_NAME,object={"first_name": name})
153
+ # MiniLogger.start(_SET_REAL_NAME_METHOD_NAME,object={"first_name": name})
155
154
  self.real_display_name = name
156
- #MiniLogger.end(_SET_REAL_NAME_METHOD_NAME)
155
+ # MiniLogger.end(_SET_REAL_NAME_METHOD_NAME)
157
156
 
158
157
  def get_real_name(self) -> str:
159
158
  GET_REAL_NAME_METHOD_NAME = "get_real_name"
160
- #MiniLogger.start(GET_REAL_NAME_METHOD_NAME)
161
- #MiniLogger.end(GET_REAL_NAME_METHOD_NAME,object={"first_name": self.real_first_name})
159
+ # MiniLogger.start(GET_REAL_NAME_METHOD_NAME)
160
+ # MiniLogger.end(GET_REAL_NAME_METHOD_NAME,object={"first_name": self.real_first_name})
162
161
  return self.real_display_name
163
162
 
164
163
  def get_user_JWT(self) -> str:
@@ -181,12 +180,14 @@ class UserContext:
181
180
 
182
181
  def get_effective_subscription_id(self) -> None:
183
182
  _GET_EFFECTIVE_SUBSCRIPTION_ID_METHOD_NAME = "get_effective_subscription_id"
184
- MiniLogger.warning("user-context-python-package "+_GET_EFFECTIVE_SUBSCRIPTION_ID_METHOD_NAME+" Please make sure get_effective_subscription_id() subscription_id="+self.subscription_id)
183
+ MiniLogger.warning("user-context-python-package "+_GET_EFFECTIVE_SUBSCRIPTION_ID_METHOD_NAME +
184
+ " Please make sure get_effective_subscription_id() subscription_id="+str(self.subscription_id))
185
185
  return self.subscription_id
186
186
 
187
187
  def _authenticate_by_user_identification_and_password(self, user_identifier: str, password: str) -> str:
188
188
  _AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME = "_authenticate_by_user_identification_and_password"
189
- MiniLogger.start(_AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME,object={"email": user_identifier, "password": password})
189
+ MiniLogger.start(_AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME, object={
190
+ "email": user_identifier, "password": password})
190
191
  try:
191
192
  url_circlez = OurUrl()
192
193
  url_jwt = url_circlez.endpoint_url(
@@ -203,20 +204,24 @@ class UserContext:
203
204
  url=url_jwt, data=json.dumps(data, separators=(",", ":")), headers=headers
204
205
  )
205
206
  if output.status_code != HTTPStatus.OK:
206
- MiniLogger.error(_AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME + " output.status_code != HTTPStatus.OK " + output.text)
207
+ MiniLogger.error(_AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME +
208
+ " output.status_code != HTTPStatus.OK " + output.text)
207
209
  raise Exception(output.text)
208
210
  self.user_JWT = output.json()["data"]["token"]
209
- MiniLogger.end(_AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME,object={"user_JWT": self.user_JWT })
211
+ MiniLogger.end(_AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME, object={
212
+ "user_JWT": self.user_JWT})
210
213
  return output
211
214
  except Exception as exception:
212
215
  MiniLogger.exception(
213
216
  "Error(Exception): user-context-remote-python _authenticate() " + str(exception))
214
- MiniLogger.end(_AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME)
217
+ MiniLogger.end(
218
+ _AUTHENTICATE_BY_USER_IDENTIFICATION_AND_PASSWORD_METHOD_NAME)
215
219
  raise
216
220
 
217
221
  def _authenticate_by_user_JWT(self, user_JWT: str) -> str:
218
222
  _AUTHENTICATE_BY_USER_JWT_METHOD_NAME = "_authenticate_by_user_JWT"
219
- MiniLogger.start(_AUTHENTICATE_BY_USER_JWT_METHOD_NAME,object={"user_JWT": user_JWT})
223
+ MiniLogger.start(_AUTHENTICATE_BY_USER_JWT_METHOD_NAME,
224
+ object={"user_JWT": user_JWT})
220
225
  url_circlez = OurUrl()
221
226
  authentication_login_validate_jwt_url = url_circlez.endpoint_url(
222
227
  brand_name=BRAND_NAME,
@@ -234,17 +239,18 @@ class UserContext:
234
239
  if output.status_code != HTTPStatus.OK:
235
240
  MiniLogger.error(
236
241
  "user-context-remote-python-package _authenticate_by_user_JWT() output.status_code != HTTPStatus.OK " + output.text)
237
- #MiniLoger.end()
242
+ # MiniLoger.end()
238
243
  raise Exception(output.text)
239
- MiniLogger.end(_AUTHENTICATE_BY_USER_JWT_METHOD_NAME, object={"validate_jwt_response": output})
244
+ MiniLogger.end(_AUTHENTICATE_BY_USER_JWT_METHOD_NAME,
245
+ object={"validate_jwt_response": output})
240
246
  return output
241
247
 
242
- # TODO Shall we make this private?
243
- def get_user_data_login_response(self, validate_jwt_response: str) -> None:
248
+ def _get_user_data_login_response(self, validate_jwt_response: str) -> None:
244
249
  _GET_USER_DATA_LOGIN_RESPONSE_METHOD_NAME = "get_user_data_login_response"
245
250
  # validate_jwt_response created in Authentication Local REST-API https://github.com/circles-zone/authentication-local-restapi-typescript-serverless-com/edit/dev/auth-restapi-serverless-com/src/services/auth-service/auth-service-impl.ts
246
- if "userDetails" in validate_jwt_response.json()["data"]:
247
- userDetails = validate_jwt_response.json()["data"]["userDetails"]
251
+ data_json = validate_jwt_response.json()['data']
252
+ if "userDetails" in data_json:
253
+ userDetails = data_json["userDetails"]
248
254
 
249
255
  if "profileId" in userDetails:
250
256
  profile_id = userDetails["profileId"]
@@ -256,9 +262,13 @@ class UserContext:
256
262
  self._set_effective_user_id(int(user_id))
257
263
  self._set_real_user_id(int(user_id))
258
264
 
259
- if "lang_code" in userDetails:
260
- lang_code = userDetails["lang_code"]
261
- self._set_current_lang_code(lang_code)
265
+ if "profilePreferredLangCode" in userDetails:
266
+ lang_code = userDetails["profilePreferredLangCode"]
267
+ if lang_code is None: # default lang_code is en
268
+ lang_code = "en"
269
+ # change to lang_code enum object
270
+ lang_code = LangCode(lang_code)
271
+ self._set_effective_profile_preferred_lang_code(lang_code)
262
272
 
263
273
  if "firstName" in userDetails:
264
274
  first_name = userDetails["firstName"]
@@ -270,8 +280,9 @@ class UserContext:
270
280
 
271
281
  if "subscriptionId" in userDetails:
272
282
  subscription_id = userDetails["subscriptionId"]
273
- MiniLogger.end(_GET_USER_DATA_LOGIN_RESPONSE_METHOD_NAME + " Got subscription_id from the cloud. subscription_id="+ subscription_id, object={"subscription_id": subscription_id})
274
- self._set_subscription_id(subscription_id)
283
+ MiniLogger.end(_GET_USER_DATA_LOGIN_RESPONSE_METHOD_NAME + " Got subscription_id from the cloud. subscription_id=" +
284
+ str(subscription_id), object={"subscription_id": subscription_id})
285
+ self._set_effective_subscription_id(subscription_id)
275
286
 
276
287
  if self.real_first_name is not None and self.real_last_name is not None:
277
288
  name = first_name + " " + last_name
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: user-context-remote
3
- Version: 0.0.41
3
+ Version: 0.0.43
4
4
  Summary: PyPI Package for Circles User Context Local/Remote Python
5
5
  Home-page: https://github.com/circles/{PACKAGE_NAME}-python-package
6
6
  Author: Circles
@@ -16,5 +16,6 @@ Requires-Dist: language-local>=0.0.6
16
16
  Requires-Dist: url-local>=0.0.15
17
17
  Requires-Dist: requests>=2.31.0
18
18
  Requires-Dist: httpstatus35>=0.0.1
19
+ Requires-Dist: python-sdk-local>=0.0.44
19
20
 
20
21
  This is a package for sharing common user-context-remote functions used in different repositories
@@ -5,3 +5,4 @@ language-local>=0.0.6
5
5
  url-local>=0.0.15
6
6
  requests>=2.31.0
7
7
  httpstatus35>=0.0.1
8
+ python-sdk-local>=0.0.44