Files
OpenBB/openbb_terminal/core/session/sdk_session.py
Henrique Joaquim e98e19d2e4 Feature/v4 deprecate everything (#6113)
* comment call_news

* remove news

* remove keys controller

* remove settings controller

* remove ta forex

* remove qa economy

* remove ta stocks

* remove qa stocks

* remove fa stocks

* remove ins stocks

* remove gov stocks

* remove dps stocks

* remove ba stocks

* remove bt stocks

* remove stocks

* remove stocks base controller class

* ruff

* remove qa forex

* remove oanda

* remove forex

* remove forex

* remove forex

* remove mention to fx_tickers

* remove ta crypto

* remove qa crypto

* remove dd crypto

* remove crypto controller

* remove crypto references

* remove economy controller

* remove economy folder

* remoe etf controller

* remove reports controller

* remove dashboards controller

* remove alt controller

* rmeove etf files

* remove alt files

* remove custom reports

* remove dashboard files

* remove econometrics controller

* remove forecast controller

* remove reports files

* remove forecast files

* remove econometrics files

* remove portfolio

* remove futures

* remove fixedincome

* remove funds

* remove sdk and common

* remove all from init

* remove reference to ipykernel

* remove reference to ta_helpers

* remove reference to ta_helpers

* cleanup

* adjustments to choices

* remove old sdk generator

* remove mention to dashboards

* ruff

* cleanup

* remove logging infra

* remove decorators

* remove sdk audit

* remove tought of the day

* remove tought of the day

* fix commands andmenus choices

* fixing lints

* ruffs

* remove old tests
2024-02-23 10:55:12 +00:00

129 lines
3.5 KiB
Python

import logging
from openbb_terminal.core.session import (
local_model as Local,
session_model,
)
from openbb_terminal.core.session.current_user import (
get_current_user,
is_local,
)
from openbb_terminal.rich_config import console
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.")
session = session_model.create_session_from_token(token, save) # type: ignore
if not session and email:
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):
console_print("[red]Failed to create session.[/red]")
return session
def login(
email: str = "",
password: str = "",
token: str = "",
keep_session: bool = False,
silent: bool = False,
):
"""
Login and load user info.
If there is a saved session it will be used (this can be achieved by `keep_session=True`).
If there's not a local session,
the user can use either email and password or the OpenBB Personal Access Token.
Parameters
----------
email : str
The email.
password : str
The password.
token : str
The OpenBB Personal Access Token.
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()
if not session:
session = get_session(email, password, token, keep_session)
else:
console_print("Using local session to login.")
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]")
def logout():
"""
Logout and clear session.
Examples
--------
>>> from openbb_terminal.sdk import openbb
>>> openbb.logout()
"""
current_user = get_current_user()
session_model.logout(
auth_header=current_user.profile.get_auth_header(),
token=current_user.profile.token,
)
def whoami():
"""
Display user info.
Examples
--------
>>> 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]username:[/info] {current_user.profile.username}")
console_print(f"[info]uuid:[/info] {current_user.profile.uuid}")
else:
print_guest_block_msg()