mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-29 21:42:22 +00:00
change return obj
This commit is contained in:
@@ -62,13 +62,13 @@ def login_prompt(welcome=True):
|
||||
def main():
|
||||
"""Main function"""
|
||||
login_info = get_login_info()
|
||||
if login_info == Failure.UNKNOWN_ERROR or not login_info:
|
||||
if not login_info:
|
||||
login_prompt()
|
||||
else:
|
||||
status = get_login_status(login_info=login_info)
|
||||
if status == Success.VALID_LOGIN:
|
||||
if isinstance(status, Success):
|
||||
launch_terminal(login_info=login_info)
|
||||
if status == Failure.INVALID_LOGIN:
|
||||
if isinstance(status, Failure):
|
||||
login_prompt(welcome=False)
|
||||
|
||||
|
||||
|
||||
@@ -67,19 +67,16 @@ def process_response(response: requests.Response, save: bool) -> Union[dict, Fai
|
||||
The login info, or an error.
|
||||
"""
|
||||
if response.status_code == 200:
|
||||
console.print(Success.VALID_LOGIN.value)
|
||||
console.print("\nLogin successful.", style="green")
|
||||
login = response.json()
|
||||
if save:
|
||||
save_login_info(login, SETTINGS_DIRECTORY / "login.json")
|
||||
return login
|
||||
if response.status_code == 401:
|
||||
console.print(Failure.WRONG_CREDENTIALS.value)
|
||||
return Failure.WRONG_CREDENTIALS
|
||||
return Failure("[red]\nWrong credentials.[/red]")
|
||||
if response.status_code == 403:
|
||||
console.print(Failure.UNVERIFIED_EMAIL.value)
|
||||
return Failure.UNVERIFIED_EMAIL
|
||||
console.print("Failed to login.", style="red")
|
||||
return Failure.UNKNOWN_ERROR
|
||||
return Failure("[red]\nUnverified email.[/red]")
|
||||
return Failure(("[red]\nFailed to login.[/red]"))
|
||||
|
||||
|
||||
def request_login_info(email: str, password: str, save: bool) -> Union[dict, Failure]:
|
||||
@@ -108,15 +105,13 @@ def request_login_info(email: str, password: str, save: bool) -> Union[dict, Fai
|
||||
try:
|
||||
response = requests.post(BASE_URL + "login", json=data)
|
||||
except requests.exceptions.ConnectionError:
|
||||
console.print(Failure.CONNECTION_ERROR.value)
|
||||
return Failure.CONNECTION_ERROR
|
||||
return Failure("[red]\nConnection error.[/red]")
|
||||
except Exception:
|
||||
console.print("Failed to request login info.", style="red")
|
||||
return Failure.UNKNOWN_ERROR
|
||||
return Failure("[red]\nFailed to request login info.[/red]")
|
||||
return process_response(response, save)
|
||||
|
||||
|
||||
def get_login_info() -> Union[dict, Failure]:
|
||||
def get_login_info() -> dict:
|
||||
"""Get the login info from the file.
|
||||
|
||||
Returns
|
||||
@@ -131,8 +126,7 @@ def get_login_info() -> Union[dict, Failure]:
|
||||
with open(file_path) as file:
|
||||
return json.load(file)
|
||||
except Exception:
|
||||
console.print("Failed to get login info.", style="red")
|
||||
return Failure.UNKNOWN_ERROR
|
||||
console.print("[red]\nFailed to get login info.[/red]")
|
||||
return {}
|
||||
|
||||
|
||||
@@ -160,13 +154,10 @@ def get_login_status(login_info: dict) -> Union[Success, Failure]:
|
||||
).status_code
|
||||
== 200
|
||||
):
|
||||
console.print(Success.VALID_LOGIN.value)
|
||||
return Success.VALID_LOGIN
|
||||
return Success("[green]\nLogin successful.[/green]")
|
||||
except requests.exceptions.ConnectionError:
|
||||
console.print(Failure.CONNECTION_ERROR.value)
|
||||
return Failure.CONNECTION_ERROR
|
||||
return Failure("[red]\nConnection error.[/red]")
|
||||
except Exception:
|
||||
console.print("Failed to get login status.", style="red")
|
||||
return Failure.UNKNOWN_ERROR
|
||||
return Failure("[red]\nFailed to get login status.[/red]")
|
||||
|
||||
return Failure.INVALID_LOGIN
|
||||
return Failure("[red]\nInvalid login.[/red]")
|
||||
|
||||
@@ -25,16 +25,12 @@ def remote_logout() -> Union[Success, Failure]:
|
||||
).status_code
|
||||
== 200
|
||||
):
|
||||
console.print(Success.REMOTE_LOGOUT.value)
|
||||
return Success.REMOTE_LOGOUT
|
||||
console.print("Failed to logout remotely.", style="red")
|
||||
return Failure.UNKNOWN_ERROR
|
||||
return Success("[green]\nLogged out remotely.[/green]")
|
||||
return Failure("[red]\nFailed to logout remotely.[/red]")
|
||||
except requests.exceptions.ConnectionError:
|
||||
console.print(Failure.CONNECTION_ERROR.value)
|
||||
return Failure.CONNECTION_ERROR
|
||||
return Failure("[red]\nConnection error.[/red]")
|
||||
except Exception:
|
||||
console.print("Failed to logout remotely.", style="red")
|
||||
return Failure.UNKNOWN_ERROR
|
||||
return Failure("[red]Failed to logout remotely.[/red]")
|
||||
|
||||
|
||||
def remove_login_file() -> Union[Success, Failure]:
|
||||
@@ -50,11 +46,9 @@ def remove_login_file() -> Union[Success, Failure]:
|
||||
file_path = SETTINGS_DIRECTORY / "login.json"
|
||||
if os.path.isfile(file_path):
|
||||
os.remove(file_path)
|
||||
console.print(Success.LOCAL_LOGOUT.value)
|
||||
return Success.LOCAL_LOGOUT
|
||||
return Success("[green]\nRemoved login info.[/green]")
|
||||
except Exception:
|
||||
console.print("Failed to remove login file.", style="red")
|
||||
return Failure.UNKNOWN_ERROR
|
||||
return Failure("[red]Failed to remove login file.[/red]")
|
||||
|
||||
|
||||
def logout():
|
||||
|
||||
@@ -1,21 +1,32 @@
|
||||
from enum import Enum
|
||||
from dataclasses import dataclass
|
||||
from openbb_terminal.rich_config import console
|
||||
|
||||
BASE_URL = "http://127.0.0.1:8000/"
|
||||
|
||||
|
||||
class Success(Enum):
|
||||
VALID_LOGIN = "[green]\nLogin successful.[/green]"
|
||||
REMOTE_LOGOUT = "[green]\nLogged out remotely.[/green]"
|
||||
LOCAL_LOGOUT = "[green]\nRemoved login info.[/green]"
|
||||
# This might be simpler if use a class with msg instead of enum
|
||||
|
||||
|
||||
class Failure(Enum):
|
||||
INVALID_LOGIN = "[red]\nInvalid login.[/red]"
|
||||
CONNECTION_ERROR = "[red]\nConnection error.[/red]"
|
||||
WRONG_CREDENTIALS = "[red]\nWrong credentials.[/red]"
|
||||
UNVERIFIED_EMAIL = "[red]\nUnverified email.[/red]"
|
||||
UNKNOWN_ERROR = "[red]\nUnknown error.[/red]"
|
||||
class Success:
|
||||
def __init__(self, msg):
|
||||
self.msg = msg
|
||||
console.print(msg)
|
||||
|
||||
# VALID_LOGIN = "[green]\nLogin successful.[/green]"
|
||||
# REMOTE_LOGOUT = "[green]\nLogged out remotely.[/green]"
|
||||
# LOCAL_LOGOUT = "[green]\nRemoved login info.[/green]"
|
||||
|
||||
|
||||
class Failure:
|
||||
def __init__(self, msg):
|
||||
self.msg = msg
|
||||
console.print(msg)
|
||||
|
||||
# INVALID_LOGIN = "[red]\nInvalid login.[/red]"
|
||||
# CONNECTION_ERROR = "[red]\nConnection error.[/red]"
|
||||
# WRONG_CREDENTIALS = "[red]\nWrong credentials.[/red]"
|
||||
# UNVERIFIED_EMAIL = "[red]\nUnverified email.[/red]"
|
||||
# UNKNOWN_ERROR = "[red]\nUnknown error.[/red]"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
||||
Reference in New Issue
Block a user