Suppressing login related prints on the SDK initialization (#4893)

* suppressing the prints on the sdk init

* removing exceptions from tests
This commit is contained in:
Henrique Joaquim
2023-04-25 14:15:36 +01:00
committed by GitHub
parent 8ce8380ef3
commit 3cf8aea940
4 changed files with 37 additions and 55 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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

View File

@@ -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()