diff --git a/generate_sdk.py b/generate_sdk.py index d0524186c15..0d82c807180 100644 --- a/generate_sdk.py +++ b/generate_sdk.py @@ -63,7 +63,7 @@ class SDKLogger: def _try_to_login(sdk: "OpenBBSDK"): if is_local() and is_auth_enabled(): try: - sdk.login() + sdk.login(silent=True) except Exception: pass diff --git a/openbb_terminal/core/session/sdk_session.py b/openbb_terminal/core/session/sdk_session.py index d4129ab90ae..8f99ac12e86 100644 --- a/openbb_terminal/core/session/sdk_session.py +++ b/openbb_terminal/core/session/sdk_session.py @@ -14,27 +14,38 @@ from openbb_terminal.terminal_helper import print_guest_block_msg logger = logging.getLogger(__name__) +console_print = console.print + + +def set_silent_console_print(silent: bool = False): + global console_print # pylint: disable=global-statement # noqa + console_print = console.print if not silent else lambda *args, **kwargs: None + def get_session(email: str, password: str, token: str, save: bool) -> dict: session = dict() if token: - console.print("Creating session from token.") + console_print("Creating session from token.") session = session_model.create_session_from_token(token, save) # type: ignore if not session and email: - console.print("Creating session from email and password.") + console_print("Creating session from email and password.") session = session_model.create_session(email, password, save) # type: ignore if not (isinstance(session, dict) and session): - raise Exception("Failed to create session.") + console_print("[red]Failed to create session.[/red]") return session @log_start_end(log=logger) def login( - email: str = "", password: str = "", token: str = "", keep_session: bool = False + email: str = "", + password: str = "", + token: str = "", + keep_session: bool = False, + silent: bool = False, ): """ Login and load user info. @@ -53,12 +64,16 @@ def login( keep_session : bool Keep the session, i.e., next time the user logs in, there is no need to enter the email and password or the token. + silent : bool + If True, the console print will be silent. Examples -------- >>> from openbb_terminal.sdk import openbb >>> openbb.login(email="your_email", password="your_password") """ + set_silent_console_print(silent=silent) + session = {} if not (email or token): session = Local.get_session() @@ -66,13 +81,16 @@ def login( if not session: session = get_session(email, password, token, keep_session) else: - console.print("Using local session to login.") + console_print("Using local session to login.") - status = session_model.login(session) - if status != session_model.LoginStatus.SUCCESS: - raise Exception(f"Login failed with status `{status.value}`.") - - console.print("[green]Login successful.[/green]") + if not session: + console_print("[red]Failed to obtain session.[/red]") + else: + status = session_model.login(session) + if status != session_model.LoginStatus.SUCCESS: + console_print(f"[red]Login failed with status `{status.value}`.[/red]") + else: + console_print("[green]Login successful.[/green]") @log_start_end(log=logger) @@ -102,10 +120,12 @@ def whoami(): >>> from openbb_terminal.sdk import openbb >>> openbb.whoami() """ + set_silent_console_print(silent=False) + current_user = get_current_user() local_user = is_local() if not local_user: - console.print(f"[info]email:[/info] {current_user.profile.email}") - console.print(f"[info]uuid:[/info] {current_user.profile.uuid}") + console_print(f"[info]email:[/info] {current_user.profile.email}") + console_print(f"[info]uuid:[/info] {current_user.profile.uuid}") else: print_guest_block_msg() diff --git a/openbb_terminal/sdk.py b/openbb_terminal/sdk.py index 847f88b6cf9..dc4a271f746 100644 --- a/openbb_terminal/sdk.py +++ b/openbb_terminal/sdk.py @@ -611,7 +611,7 @@ class SDKLogger: def _try_to_login(sdk: "OpenBBSDK"): if is_local() and is_auth_enabled(): try: - sdk.login() + sdk.login(silent=True) except Exception: pass diff --git a/tests/openbb_terminal/session/test_sdk_session.py b/tests/openbb_terminal/session/test_sdk_session.py index d5ebb7c9fe9..222fdcfa2b7 100644 --- a/tests/openbb_terminal/session/test_sdk_session.py +++ b/tests/openbb_terminal/session/test_sdk_session.py @@ -33,35 +33,6 @@ def fixture_test_user(): ) -@pytest.mark.parametrize( - "email, password, token, save", - [ - ( - "test_email", - "test_pass", - "test_token", - True, - ), - ], -) -def test_get_session_exception(email: str, password: str, token: str, save: bool): - path = "openbb_terminal.core.session.sdk_session." - with patch(path + "session_model.create_session") as mock_create_session: - with patch( - path + "session_model.create_session_from_token" - ) as mock_create_session_from_token: - mock_create_session.return_value = {} - mock_create_session_from_token.return_value = {} - - with pytest.raises(Exception): - sdk_session.get_session( - email=email, password=password, token=token, save=save - ) - - mock_create_session.assert_called_once_with(email, password, save) - mock_create_session_from_token.assert_called_once_with(token, save) - - @pytest.mark.parametrize( "email, password, token, save", [ @@ -149,18 +120,9 @@ def test_login( mock_hub_get_session.return_value = TEST_SESSION mock_login.return_value = status - if status in [LoginStatus.FAILED, LoginStatus.NO_RESPONSE]: - with pytest.raises(Exception): - sdk_session.login( - email=email, - password=password, - token=token, - keep_session=keep_session, - ) - else: - sdk_session.login( - email=email, password=password, token=token, keep_session=keep_session - ) + sdk_session.login( + email=email, password=password, token=token, keep_session=keep_session + ) if has_session: mock_hub_get_session.assert_not_called()