mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-03 16:02:04 +00:00
* import order * pylint * no login, go to prompt * add logout and whoami * clear before prompt * black * these are unecessary * error * improve login prompt message * login does not follow the controller pattern, so will name it differently * nop * move logout and change to results * change return obj * ruff * pyupgrade * remove user from statics * message * fetch configs at start * remove unecessary func * unecessary dataclass * change user vars * change login flow * remote logout should not be used * simplify * fix double exit logout bug * wrong exception * apply keys * change controller * mypy * save if no internet * pylint * logout everywhere * breaking changes: move to local/hub models * unused import * fix logout bug * whoami in guest mode * add todo * small bug guest allowed * limit flair length * fix bug on user pull * logout working * handle no connection * logout user when logout * remove keys push * split logger funcs * commant apply configs for now * account to session * rename user * clear sounds better * breaking changes: splitting guest from logged users * pull imports up * delete session model * move logout func * add ability to save and apply keys from remote * no sensitive keys * add support for feature flags * unecessary print * allow settings from differnt modules * change setff to static * change way to set ff * fix bug in applying configs * simplify obbff application * bug on setting configs * allow text ff * obbff in settings * add upload to cfg_plot 1 * data sources + backend * this solves circular import? * move create_session to model * rever logs changes, not needed for now * sync optionally * outline account controller interface * don't sync when guest * block guest on installer * or not and * start routines * fix bug on ff, settings * is installer * implementing chavi suggestions * fix colors on installer prompt * Logging login (#4046) * basic logging for the login * removing unused import * hacking the logs custom filter to not filter the email * black * cooping with the new changes * removing redundant method * ruff * ruff * mypy * SDK login (#4051) * sdk login * adding to the trail map * minor fix * improve sdk expirience * adapting to new changes * add docstrings * block guest from account * change guest msg * change guest messages * add sync on/off * display onoff * missing paragraph * fix glitch on logout * add sys clear * filtering password * filtering the email from user * avoid writing to multiple log files if a user logs out * fix bug, guest allowed in installer without connection * log user on the sdk * split login from lauch * add clear and change messages on account * docstrings * start pull cmd * change account menu description * Fix login bug and decouple some logic (#4056) * fix bug, guest allowed in installer without connection * split login from lauch * improvements on the sdk login --------- Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt> * sync fix * change account controller description * ruff * add help on account cmds * update clear prompt * fix email not in jwt * remove jwt from requiremnts * pull command output * remove character * pull cmd when no data * add space below whoami * fix log filenotfound exception * changed arg name * delete .hist on logout * add asteriscs to password * pylint * uncomment rich config * show only diffs on pull * black# * warn when no changes# * pull msg was switched * apply diff only * breaking?: strbool change * reduce some code * add timeout to hub requests * black * add remote logout * success msg only if success * remove colors in prompt * update docstrings * keep timeout outside funcs * update clear * clear and patch * delete session * routine upload * move .his file to .openbb folder * change apply configs * refactor logout * remove mixed portfolio changes * fix bug in lock file * fix bug pwidth, pheight are floats * fix sources command & add choices * refactor timezone setting * userdata * fix flair * need to cast paths * cast on diff * fix flair default * save session test * get session * data * test remove * test remove with assert * add bad test cases * add exception cases to unittests * test remove cli hist file * black * test apply configs * test apply with sync flag * test create session, session model * login tests, session model * some tests for the hub_model * test logout user * black * test clear vars, reload * test create session * update user-json to user * update user class * add user tests * tests sdk session * tests for hub_model * start account controller tsts * unused import * tests account controller: clear, pull * tests for session_controller * logout success bugfix * add integration test for account * black * move routine related commands to other PR * test for the user input * completed tests for session controller * convert to hist to str) * fix import errors * ruff * ruff tests * fix qa * add help to settings cmds * ruff * fix pull keys bug * point to dev * account model tests * black * ruff * fix for empty feature settings * ruff * don't display config msg if wrong token * general msgs * remove print * ruff * fix sources not updated real time * missing return doc * add guest if no email * Routine remote storage (#4143) * add upload routine command * remove prints * point to local again * add commands * add delete * add download command and upload 409 * list comprehension * add download & save plus upload fixe * add user uuid folder * add list command * ruff * en.yml detail * provide choices to delete & download * add choices to upload * change menu text * starting adding unit tests * bad english * Update test_print_help.txt * genearl msgs * remove print * add docstrings * add unit tests to account controller * change parser * fix routine upload test * add to be implemented tests hub * add to be implemented tests local * add total pages to title * split list into model and view * add docstring * print msg if character * add local model tests * hub tests routines * fix test * point to dev and glob * fix small bug on upload * missing return * log decorators missing * black * ruff * change prompt display * rewrite test * rewrite expected * rewrite call * try passing git action * change .yml everywhere --------- Co-authored-by: Henrique Joaquim <h.joaquim@campus.fct.unl.pt> Co-authored-by: James Maslek <jmaslek11@gmail.com>
491 lines
14 KiB
Python
491 lines
14 KiB
Python
from typing import Dict, Optional
|
|
|
|
import requests
|
|
|
|
from openbb_terminal.rich_config import console
|
|
|
|
BASE_URL = "https://payments.openbb.dev/"
|
|
TIMEOUT = 15
|
|
|
|
CONNECTION_ERROR_MSG = "[red]Connection error.[/red]"
|
|
CONNECTION_TIMEOUT_MSG = "[red]Connection timeout.[/red]"
|
|
|
|
|
|
def create_session(
|
|
email: str, password: str, base_url: str = BASE_URL, timeout: int = TIMEOUT
|
|
) -> Optional[requests.Response]:
|
|
"""Create a session.
|
|
|
|
Parameters
|
|
----------
|
|
email : str
|
|
The email.
|
|
password : str
|
|
The password.
|
|
base_url : str
|
|
The base url, by default BASE_URL
|
|
timeout : int
|
|
The timeout, by default TIMEOUT
|
|
|
|
Returns
|
|
-------
|
|
Optional[requests.Response]
|
|
The response from the login request.
|
|
"""
|
|
try:
|
|
data = {
|
|
"email": email,
|
|
"password": password,
|
|
"remember": True,
|
|
}
|
|
return requests.post(url=base_url + "login", json=data, timeout=timeout)
|
|
except requests.exceptions.ConnectionError:
|
|
console.print(f"\n{CONNECTION_ERROR_MSG}")
|
|
return None
|
|
except requests.exceptions.Timeout:
|
|
console.print(f"\n{CONNECTION_TIMEOUT_MSG}")
|
|
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]:
|
|
"""Delete the session.
|
|
|
|
Parameters
|
|
----------
|
|
auth_header : str
|
|
The authorization header, e.g. "Bearer <token>".
|
|
token : str
|
|
The token to delete.
|
|
base_url : str
|
|
The base url, by default BASE_URL
|
|
timeout : int
|
|
The timeout, by default TIMEOUT
|
|
|
|
Returns
|
|
-------
|
|
Optional[requests.Response]
|
|
The response from the logout request.
|
|
"""
|
|
try:
|
|
response = requests.post(
|
|
url=base_url + "logout",
|
|
headers={"Authorization": auth_header},
|
|
json={"token": token},
|
|
timeout=timeout,
|
|
)
|
|
if response.status_code != 200:
|
|
console.print("[red]Failed to delete server session.[/red]")
|
|
return response
|
|
except requests.exceptions.ConnectionError:
|
|
console.print(f"\n{CONNECTION_ERROR_MSG}")
|
|
return None
|
|
except requests.exceptions.Timeout:
|
|
console.print(f"\n{CONNECTION_TIMEOUT_MSG}")
|
|
return None
|
|
except Exception:
|
|
console.print("[red]Failed to delete server session.[/red]")
|
|
return None
|
|
|
|
|
|
def process_session_response(response: requests.Response) -> dict:
|
|
"""Process the response from the login request.
|
|
|
|
Parameters
|
|
----------
|
|
response : requests.Response
|
|
The response from the login request.
|
|
|
|
Returns
|
|
-------
|
|
dict
|
|
The login info.
|
|
"""
|
|
if response.status_code == 200:
|
|
login = response.json()
|
|
return login
|
|
if response.status_code == 401:
|
|
console.print("\n[red]Wrong credentials.[/red]")
|
|
return {}
|
|
if response.status_code == 403:
|
|
console.print("\n[red]Unverified email.[/red]")
|
|
return {}
|
|
console.print("\n[red]Failed to login.[/red]")
|
|
return {}
|
|
|
|
|
|
def get_session(email: str, password: str) -> dict:
|
|
"""Get the session info.
|
|
|
|
Parameters
|
|
----------
|
|
email : str
|
|
The email.
|
|
password : str
|
|
The password.
|
|
|
|
Returns
|
|
-------
|
|
dict
|
|
The session info.
|
|
"""
|
|
response = create_session(email, password)
|
|
if response is None:
|
|
return {}
|
|
return process_session_response(response)
|
|
|
|
|
|
def fetch_user_configs(
|
|
session: dict, base_url: str = BASE_URL, timeout: int = TIMEOUT
|
|
) -> Optional[requests.Response]:
|
|
"""Fetch user configurations.
|
|
|
|
Parameters
|
|
----------
|
|
session : dict
|
|
The session info.
|
|
base_url : str
|
|
The base url, by default BASE_URL
|
|
timeout : int
|
|
The timeout, by default TIMEOUT
|
|
|
|
Returns
|
|
-------
|
|
Optional[requests.Response]
|
|
The response from the get request.
|
|
"""
|
|
token_type = session.get("token_type", "")
|
|
token = session.get("access_token", "")
|
|
|
|
try:
|
|
response = requests.get(
|
|
url=base_url + "terminal/user",
|
|
headers={"Authorization": f"{token_type.title()} {token}"},
|
|
timeout=timeout,
|
|
)
|
|
if response.status_code not in [200, 401]: # 401: invalid token
|
|
console.print("[red]\nFailed to fetch configurations.[/red]")
|
|
return response
|
|
except requests.exceptions.ConnectionError:
|
|
console.print(f"\n{CONNECTION_ERROR_MSG}")
|
|
return None
|
|
except requests.exceptions.Timeout:
|
|
console.print(f"\n{CONNECTION_TIMEOUT_MSG}")
|
|
return None
|
|
except Exception:
|
|
console.print("[red]\nFailed to fetch configurations.[/red]")
|
|
return None
|
|
|
|
|
|
def patch_user_configs(
|
|
key: str,
|
|
value: str,
|
|
type_: str,
|
|
auth_header: str,
|
|
base_url: str = BASE_URL,
|
|
timeout: int = TIMEOUT,
|
|
) -> Optional[requests.Response]:
|
|
"""Patch user configurations to the server.
|
|
|
|
Parameters
|
|
----------
|
|
key : str
|
|
The key to patch.
|
|
value : str
|
|
The value to patch.
|
|
type_ : str
|
|
The type of the patch, either "keys" or "settings".
|
|
auth_header : str
|
|
The authorization header, e.g. "Bearer <token>".
|
|
base_url : str
|
|
The base url, by default BASE_URL
|
|
timeout : int
|
|
The timeout, by default TIMEOUT
|
|
|
|
Returns
|
|
-------
|
|
Optional[requests.Response]
|
|
The response from the patch request.
|
|
"""
|
|
if type_ not in ["keys", "settings"]:
|
|
console.print("[red]\nInvalid patch type.[/red]")
|
|
return None
|
|
|
|
data = {"key": f"features_{type_}.{key}", "value": value}
|
|
|
|
try:
|
|
response = requests.patch(
|
|
url=base_url + "terminal/user",
|
|
headers={"Authorization": auth_header},
|
|
json=data,
|
|
timeout=timeout,
|
|
)
|
|
if response.status_code == 200:
|
|
console.print("[green]Saved remotely.[/green]")
|
|
else:
|
|
console.print("[red]Failed to save remotely.[/red]")
|
|
return response
|
|
except requests.exceptions.ConnectionError:
|
|
console.print(f"\n{CONNECTION_ERROR_MSG}")
|
|
return None
|
|
except requests.exceptions.Timeout:
|
|
console.print(f"\n{CONNECTION_TIMEOUT_MSG}")
|
|
return None
|
|
except Exception:
|
|
console.print("[red]Failed to save remotely.[/red]")
|
|
return None
|
|
|
|
|
|
def clear_user_configs(
|
|
auth_header: str, base_url: str = BASE_URL, timeout: int = TIMEOUT
|
|
) -> Optional[requests.Response]:
|
|
"""Clear user configurations to the server.
|
|
|
|
Parameters
|
|
----------
|
|
auth_header : str
|
|
The authorization header, e.g. "Bearer <token>".
|
|
base_url : str
|
|
The base url, by default BASE_URL
|
|
timeout : int
|
|
The timeout, by default TIMEOUT
|
|
|
|
Returns
|
|
-------
|
|
Optional[requests.Response]
|
|
The response from the put request.
|
|
"""
|
|
data: Dict[str, dict] = {"features_keys": {}, "features_settings": {}}
|
|
|
|
try:
|
|
response = requests.put(
|
|
url=base_url + "terminal/user",
|
|
headers={"Authorization": auth_header},
|
|
json=data,
|
|
timeout=timeout,
|
|
)
|
|
if response.status_code == 200:
|
|
console.print("[green]Cleared configurations.[/green]")
|
|
else:
|
|
console.print("[red]Failed to clear configurations.[/red]")
|
|
return response
|
|
except requests.exceptions.ConnectionError:
|
|
console.print(f"\n{CONNECTION_ERROR_MSG}")
|
|
return None
|
|
except requests.exceptions.Timeout:
|
|
console.print(f"\n{CONNECTION_TIMEOUT_MSG}")
|
|
return None
|
|
except Exception:
|
|
console.print("[red]Failed to clear configurations.[/red]")
|
|
return None
|
|
|
|
|
|
def upload_routine(
|
|
auth_header: str,
|
|
name: str = "",
|
|
description: str = "",
|
|
routine: str = "",
|
|
override: bool = False,
|
|
base_url=BASE_URL,
|
|
timeout: int = TIMEOUT,
|
|
) -> Optional[requests.Response]:
|
|
"""Send a routine to the server.
|
|
|
|
Parameters
|
|
----------
|
|
auth_header : str
|
|
The authorization header, e.g. "Bearer <token>".
|
|
name : str
|
|
The name of the routine.
|
|
routine : str
|
|
The routine.
|
|
override : bool
|
|
Whether to override the routine if it already exists.
|
|
base_url : str
|
|
The base url, by default BASE_URL
|
|
timeout : int
|
|
The timeout, by default TIMEOUT
|
|
|
|
Returns
|
|
-------
|
|
Optional[requests.Response]
|
|
The response from the post request.
|
|
"""
|
|
data = {
|
|
"name": name,
|
|
"description": description,
|
|
"script": routine,
|
|
"override": override,
|
|
}
|
|
|
|
try:
|
|
response = requests.post(
|
|
headers={"Authorization": auth_header},
|
|
url=base_url + "terminal/script",
|
|
json=data,
|
|
timeout=timeout,
|
|
)
|
|
if response.status_code == 200:
|
|
console.print("[green]Successfully uploaded your routine.[/green]")
|
|
elif response.status_code != 409: # 409: routine already exists
|
|
console.print(
|
|
"[red]" + response.json().get("detail", "Unknown error.") + "[/red]"
|
|
)
|
|
return response
|
|
except requests.exceptions.ConnectionError:
|
|
console.print(f"\n{CONNECTION_ERROR_MSG}")
|
|
return None
|
|
except requests.exceptions.Timeout:
|
|
console.print(f"\n{CONNECTION_TIMEOUT_MSG}")
|
|
return None
|
|
except Exception:
|
|
console.print("[red]Failed to upload your routine.[/red]")
|
|
return None
|
|
|
|
|
|
def download_routine(
|
|
auth_header: str,
|
|
name: str = "",
|
|
base_url=BASE_URL,
|
|
timeout: int = TIMEOUT,
|
|
) -> Optional[requests.Response]:
|
|
"""Download a routine from the server.
|
|
|
|
Parameters
|
|
----------
|
|
auth_header : str
|
|
The authorization header, e.g. "Bearer <token>".
|
|
name : str
|
|
The name of the routine.
|
|
base_url : str
|
|
The base url, by default BASE_URL
|
|
timeout : int
|
|
The timeout, by default TIMEOUT
|
|
|
|
Returns
|
|
-------
|
|
Optional[requests.Response]
|
|
The response from the get request.
|
|
"""
|
|
try:
|
|
response = requests.get(
|
|
headers={"Authorization": auth_header},
|
|
url=base_url + "terminal/script/" + name,
|
|
timeout=timeout,
|
|
)
|
|
if response.status_code == 404:
|
|
console.print("[red]Routine not found.[/red]")
|
|
elif response.status_code != 200:
|
|
console.print("[red]Failed to download your routine.[/red]")
|
|
return response
|
|
except requests.exceptions.ConnectionError:
|
|
console.print(f"\n{CONNECTION_ERROR_MSG}")
|
|
return None
|
|
except requests.exceptions.Timeout:
|
|
console.print(f"\n{CONNECTION_TIMEOUT_MSG}")
|
|
return None
|
|
except Exception:
|
|
console.print("[red]Failed to download your routine.[/red]")
|
|
return None
|
|
|
|
|
|
def delete_routine(
|
|
auth_header: str,
|
|
name: str = "",
|
|
base_url=BASE_URL,
|
|
timeout: int = TIMEOUT,
|
|
) -> Optional[requests.Response]:
|
|
"""Delete a routine from the server.
|
|
|
|
Parameters
|
|
----------
|
|
auth_header : str
|
|
The authorization header, e.g. "Bearer <token>".
|
|
name : str
|
|
The name of the routine.
|
|
base_url : str
|
|
The base url, by default BASE_URL
|
|
timeout : int
|
|
The timeout, by default TIMEOUT
|
|
|
|
Returns
|
|
-------
|
|
Optional[requests.Response]
|
|
The response from the delete request.
|
|
"""
|
|
try:
|
|
response = requests.delete(
|
|
headers={"Authorization": auth_header},
|
|
url=base_url + "terminal/script/" + name,
|
|
timeout=timeout,
|
|
)
|
|
if response.status_code == 200:
|
|
console.print("[green]Successfully deleted your routine.[/green]")
|
|
elif response.status_code == 404:
|
|
console.print("[red]Routine not found.[/red]")
|
|
else:
|
|
console.print("[red]Failed to delete your routine.[/red]")
|
|
return response
|
|
except requests.exceptions.ConnectionError:
|
|
console.print(f"\n{CONNECTION_ERROR_MSG}")
|
|
return None
|
|
except requests.exceptions.Timeout:
|
|
console.print(f"\n{CONNECTION_TIMEOUT_MSG}")
|
|
return None
|
|
except Exception:
|
|
console.print("[red]Failed to delete your routine.[/red]")
|
|
return None
|
|
|
|
|
|
def list_routines(
|
|
auth_header: str,
|
|
page: int = 1,
|
|
size: int = 10,
|
|
base_url=BASE_URL,
|
|
timeout: int = TIMEOUT,
|
|
) -> Optional[requests.Response]:
|
|
"""List all routines from the server.
|
|
|
|
Parameters
|
|
----------
|
|
auth_header : str
|
|
The authorization header, e.g. "Bearer <token>".
|
|
page : int
|
|
The page number.
|
|
size : int
|
|
The number of routines per page.
|
|
base_url : str
|
|
The base url, by default BASE_URL
|
|
timeout : int
|
|
The timeout, by default TIMEOUT
|
|
|
|
Returns
|
|
-------
|
|
Optional[requests.Response]
|
|
The response from the get request.
|
|
"""
|
|
fields = "name%2Cdescription"
|
|
|
|
try:
|
|
response = requests.get(
|
|
headers={"Authorization": auth_header},
|
|
url=f"{base_url}terminal/script?fields={fields}&page={page}&size={size}",
|
|
timeout=timeout,
|
|
)
|
|
if response.status_code != 200:
|
|
console.print("[red]Failed to list your routines.[/red]")
|
|
return response
|
|
except requests.exceptions.ConnectionError:
|
|
console.print(f"\n{CONNECTION_ERROR_MSG}")
|
|
return None
|
|
except requests.exceptions.Timeout:
|
|
console.print(f"\n{CONNECTION_TIMEOUT_MSG}")
|
|
return None
|
|
except Exception:
|
|
console.print("[red]Failed to list your routines.[/red]")
|
|
return None
|