sync optionally

This commit is contained in:
Diogo Sousa
2023-01-26 12:34:49 +00:00
parent c583021113
commit 7bc106d22c
5 changed files with 50 additions and 25 deletions

View File

@@ -105,7 +105,7 @@ class FeatureFlagsController(BaseController):
setattr(obbff, name, value)
# Send feature flag to server
if not User.is_guest():
if User.is_sync_enabled():
patch_user_configs(
key=name,
value=str(value),

View File

@@ -241,7 +241,7 @@ def set_key(env_var_name: str, env_var_value: str, persist: bool = False) -> Non
# Send api key to server
if (
not User.is_guest()
User.is_sync_enabled()
and env_var_name not in cfg.SENSITIVE_KEYS
and env_var_name.startswith("API_")
):

View File

@@ -137,7 +137,7 @@ def patch_user_configs(key: str, value: str, type_: str) -> Optional[requests.Re
try:
response = requests.patch(
url=BASE_URL + "terminal/user-json",
headers={"Authorization": f"{User.TOKEN_TYPE.title()} {User.TOKEN}"},
headers={"Authorization": User.get_token()},
json=data,
)
if response.status_code == 200:
@@ -174,7 +174,7 @@ def logout_everywhere() -> Optional[requests.Response]:
try:
response = requests.get(
url=BASE_URL + "logout-everywhere",
headers={"Authorization": f"{User.TOKEN_TYPE.title()} {User.TOKEN}"},
headers={"Authorization": User.get_token()},
)
if response.status_code == 200:
console.print("[green]\nLogged out remotely.[/green]")

View File

@@ -6,10 +6,11 @@ from openbb_terminal.rich_config import console
@dataclass
class User:
TOKEN_TYPE: str = ""
TOKEN: str = ""
EMAIL: str = ""
UUID: str = ""
_TOKEN_TYPE: str = ""
_TOKEN: str = ""
_EMAIL: str = ""
_UUID: str = ""
_SYNC_ENABLED: bool = True
@classmethod
def load_user_info(cls, session: dict):
@@ -20,39 +21,63 @@ class User:
session : dict
The login info.
"""
User.TOKEN_TYPE = session.get("token_type", "")
User.TOKEN = session.get("access_token", "")
User.UUID = session.get("uuid", "")
User._TOKEN_TYPE = session.get("token_type", "")
User._TOKEN = session.get("access_token", "")
User._UUID = session.get("uuid", "")
if User.TOKEN:
decoded_info = jwt.decode(User.TOKEN, options={"verify_signature": False})
User.EMAIL = decoded_info.get("sub", "")
if User._TOKEN:
decoded_info = jwt.decode(User._TOKEN, options={"verify_signature": False})
User._EMAIL = decoded_info.get("sub", "")
MAX_FLAIR_LEN = 20
if obbff.USE_FLAIR == ":openbb":
username = User.EMAIL[: User.EMAIL.find("@")]
setattr(obbff, "USE_FLAIR", "[" + username[:MAX_FLAIR_LEN] + "] 🦋")
username = User._EMAIL[: User._EMAIL.find("@")]
if User._SYNC_ENABLED:
flair = "[" + username[:MAX_FLAIR_LEN] + "] 🟢"
else:
flair = "[" + username[:MAX_FLAIR_LEN] + "] 🔴"
setattr(obbff, "USE_FLAIR", flair)
@classmethod
def whoami(cls):
"""Display user info."""
if User.UUID:
console.print(f"[info]email:[/info] {User.EMAIL}")
console.print(f"[info]uuid:[/info] {User.UUID}\n")
if User._UUID:
console.print(f"[info]email:[/info] {User._EMAIL}")
console.print(f"[info]uuid:[/info] {User._UUID}")
if User._SYNC_ENABLED:
sync = "enabled"
else:
sync = "disabled"
console.print(f"[info]sync:[/info] {sync}\n")
else:
console.print("[info]Only you know...[/info]\n")
@classmethod
def clear(cls):
"""Clear user info."""
User.TOKEN_TYPE = ""
User.TOKEN = ""
User.EMAIL = ""
User.UUID = ""
User._TOKEN_TYPE = ""
User._TOKEN = ""
User._EMAIL = ""
User._UUID = ""
obbff.USE_FLAIR = ":openbb"
@classmethod
def is_guest(cls):
"""Check if user is guest."""
return not bool(User.TOKEN)
return not bool(User._TOKEN)
@classmethod
def is_sync_enabled(cls):
"""Check if sync is enabled."""
return User._SYNC_ENABLED
@classmethod
def toggle_sync(cls):
"""Toggle sync."""
User._SYNC_ENABLED = not User._SYNC_ENABLED
@classmethod
def get_token(cls):
"""Get token."""
return f"{User._TOKEN_TYPE.title()} {User._TOKEN}"

View File

@@ -162,7 +162,7 @@ class SettingsController(BaseController):
setattr(cfg_plot, name, value)
# Send feature flag to server
if not User.is_guest():
if User.is_sync_enabled():
patch_user_configs(
key=name,
value=str(value),