Fix ask use logic for testing/CI

kivy_mainline_and_py3.8
Tyler Goodlet 2019-03-17 23:03:45 -04:00
parent 6857713adf
commit 3a439fc99d
1 changed files with 13 additions and 4 deletions

View File

@ -528,6 +528,7 @@ def _token_from_user(conf: 'configparser.ConfigParser') -> None:
def get_config(
config_path: str = None,
force_from_user: bool = False,
ask_user_on_failure: bool = False,
) -> "configparser.ConfigParser":
"""Load the broker config from disk.
@ -539,11 +540,19 @@ def get_config(
"""
log.debug("Reloading access config data")
conf, path = config.load(config_path)
if force_from_user or (
not conf.get('questrade') or not conf['questrade'].get('refresh_token')
):
# check if the current config has a token
section = conf.get('questrade')
has_token = section.get('refresh_token') if section else False
if force_from_user or ask_user_on_failure and not (section or has_token):
log.warn(f"Forcing manual token auth from user")
_token_from_user(conf)
else:
if not section:
raise ValueError(f"No `questrade` section found in {path}")
if not has_token:
raise ValueError(f"No refresh token found in {path}")
return conf, path
@ -555,7 +564,7 @@ async def get_client(
) -> Client:
"""Spawn a broker client for making requests to the API service.
"""
conf, path = get_config(config_path)
conf, path = get_config(config_path, ask_user_on_failure=ask_user)
log.debug(f"Loaded config:\n{colorize_json(dict(conf['questrade']))}")
client = Client(conf)
await client.ensure_access(ask_user=ask_user)