From 58d2ea163799363913cca882d4b6b503c489d16f Mon Sep 17 00:00:00 2001 From: Henrique Joaquim Date: Wed, 22 Feb 2023 14:40:01 +0000 Subject: [PATCH] Auto login on the SDK if local session is found (#4297) * token generation on the terminal * revoke and show commands * auto login on the sdk if local session is found * fix tests --------- Co-authored-by: Luqman Co-authored-by: James Maslek --- openbb_terminal/sdk.py | 9 +++++++++ openbb_terminal/session/sdk_session.py | 7 +++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/openbb_terminal/sdk.py b/openbb_terminal/sdk.py index 6e5b69548a8..f9a9ed7d82a 100644 --- a/openbb_terminal/sdk.py +++ b/openbb_terminal/sdk.py @@ -22,6 +22,7 @@ from openbb_terminal.sdk_core import ( ) from openbb_terminal import feature_flags as obbff from openbb_terminal.session.user import User +from openbb_terminal.terminal_helper import is_auth_enabled if User.is_guest(): load_dotenv_and_reload_configs() @@ -48,6 +49,14 @@ class OpenBBSDK: self.logout = lib.sdk_session.logout self.news = lib.common_feedparser_model.get_news self.whoami = lib.sdk_session.whoami + self._try_to_login() + + def _try_to_login(self): + if User.is_guest() and is_auth_enabled(): + try: + self.login() + except Exception: + pass @property def alt(self): diff --git a/openbb_terminal/session/sdk_session.py b/openbb_terminal/session/sdk_session.py index 51fa4008f4f..13084c3fd9a 100644 --- a/openbb_terminal/session/sdk_session.py +++ b/openbb_terminal/session/sdk_session.py @@ -18,7 +18,7 @@ def get_session(email: str, password: str, token: str, save: bool): console.print("Creating session from token.") session = session_model.create_session_from_token(token, save) # type: ignore - if not session: + if not session and email: console.print("Creating session from email and password.") session = session_model.create_session(email, password, save) # type: ignore @@ -55,11 +55,10 @@ def login( >>> from openbb_terminal.sdk import openbb >>> openbb.login(email="your_email", password="your_password") """ - - session = Local.get_session() + if not (email or token): + session = Local.get_session() if not session: - console.print("No local session found. Creating new session.") session = get_session(email, password, token, keep_session) else: console.print("Using local session to login.")