adding login via token to the sdk

This commit is contained in:
hjoaquim
2023-02-07 10:30:26 +00:00
parent 6610c7384d
commit 93e26fe2d5
7 changed files with 104 additions and 8 deletions

View File

@@ -4,7 +4,7 @@ import requests
from openbb_terminal.rich_config import console
BASE_URL = "https://payments.openbb.dev/"
BASE_URL = "http://127.0.0.1:8000/"
TIMEOUT = 15
@@ -47,6 +47,36 @@ def create_session(
return None
def create_session_from_token(
token: str, base_url: str = BASE_URL, timeout: int = TIMEOUT
):
"""Create a session from token.
Parameters
----------
token : str
The token.
base_url : str
The base url, by default BASE_URL
timeout : int
The timeout, by default TIMEOUT
"""
try:
data = {
"token": token,
}
return requests.post(url=base_url + "sdk/login", json=data, timeout=timeout)
except requests.exceptions.ConnectionError:
console.print("\n[red]Connection error.[/red]")
return None
except requests.exceptions.Timeout:
console.print("\n[red]Connection timeout.[/red]")
return None
except Exception:
console.print("\n[red]Failed to request login info.[/red]")
return None
def delete_session(
auth_header: str, token: str, base_url: str = BASE_URL, timeout: int = TIMEOUT
) -> Optional[requests.Response]:
@@ -136,6 +166,25 @@ def get_session(email: str, password: str) -> dict:
return process_session_response(response)
def get_session_from_token(token: str) -> dict:
"""Get the session info from token.
Parameters
----------
token : str
The token.
Returns
-------
dict
The session info.
"""
response = create_session_from_token(token)
if response.status_code == 200:
return response.json()
return {}
def fetch_user_configs(
session: dict, base_url: str = BASE_URL, timeout: int = TIMEOUT
) -> Optional[requests.Response]:

View File

@@ -4,26 +4,38 @@ from openbb_terminal.session import session_model
from openbb_terminal.session.user import User
def get_session(email: str, password: str, save: bool):
session = session_model.create_session(email, password, save)
def get_session(email: str, password: str, token: str, save: bool):
session = ""
if token:
console.print("Creating session from token.")
session = session_model.create_session_from_token(token, save)
if not session:
console.print("Creating session from email and password.")
session = session_model.create_session(email, password, save)
if not (isinstance(session, dict) and session):
raise Exception("Failed to create session")
raise Exception("Failed to create session.")
return session
def login(email: str = "", password: str = "", keep_session: bool = False):
def login(
email: str = "", password: str = "", token: str = "", keep_session: bool = False
):
session = Local.get_session()
if not session:
session = get_session(email, password, keep_session)
session = get_session(email, password, token, keep_session)
if session_model.login(session) in [
session_model.LoginStatus.FAILED,
session_model.LoginStatus.NO_RESPONSE,
]:
raise Exception("Login failed")
raise Exception("Login failed.")
console.print("[green]Login successful[/green]")
console.print("[green]Login successful.[/green]")
def logout():
@@ -32,3 +44,7 @@ def logout():
token=User.get_token(),
guest=User.is_guest(),
)
def whoami():
User.whoami()

View File

@@ -42,6 +42,23 @@ def create_session(email: str, password: str, save: bool) -> dict:
return session
def create_session_from_token(token: str, save: bool) -> dict:
"""Create a session from token.
Parameters
----------
token : str
The token.
save : bool
Save the session.
"""
session = Hub.get_session_from_token(token)
if session and save:
Local.save_session(session)
return session
def login(session: dict) -> LoginStatus:
"""Login and load user info.

View File

@@ -495,3 +495,7 @@ def test_upload_routine_exception():
):
result = hub_model.upload_routine("auth_header", "name", "routine")
assert result is None
# TODO: Add tests to create_session_from_token
# TODO: Add tests to get_session_from_token

View File

@@ -137,3 +137,7 @@ def test_logout():
token=User.get_token(),
guest=User.is_guest(),
)
# TODO: add test for whoami
# TODO: update tests to consider the login using the token

View File

@@ -171,3 +171,6 @@ def test_reload_openbb_modules():
session_model.reload_openbb_modules()
mock_reload.assert_called()
# TODO: Add tests to create_session_from_token

View File

@@ -246,3 +246,6 @@ def test_call_tokenterminal(other):
@pytest.mark.parametrize("other", [[], ["-k", "1234", "-t", "456"]])
def test_call_shroom(other):
controller.call_shroom(other)
# TODO: Add tests from openbb key