From 3a439fc99dd208a91e575a3cf6e83810cd591ca7 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sun, 17 Mar 2019 23:03:45 -0400 Subject: [PATCH] Fix ask use logic for testing/CI --- piker/brokers/questrade.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/piker/brokers/questrade.py b/piker/brokers/questrade.py index 0856537b..1c82f12f 100644 --- a/piker/brokers/questrade.py +++ b/piker/brokers/questrade.py @@ -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)