diff --git a/piker/brokers/cli.py b/piker/brokers/cli.py index b5501633..e9d07dcf 100644 --- a/piker/brokers/cli.py +++ b/piker/brokers/cli.py @@ -6,6 +6,7 @@ from importlib import import_module import click import trio +import pandas as pd from ..log import get_console_log, colorize_json @@ -30,9 +31,11 @@ def cli(): @cli.command() @click.option('--broker', default='questrade', help='Broker backend to use') @click.option('--loglevel', '-l', default='warning', help='Logging level') +@click.option('--keys', '-k', multiple=True, + help='Return results only for these keys') @click.argument('meth', nargs=1) @click.argument('kwargs', nargs=-1) -def api(meth, kwargs, loglevel, broker): +def api(meth, kwargs, loglevel, broker, keys): """client for testing broker API methods with pretty printing of output. """ log = get_console_log(loglevel) @@ -47,7 +50,43 @@ def api(meth, kwargs, loglevel, broker): _kwargs[key] = value data = run(partial(brokermod.api, meth, **_kwargs), loglevel=loglevel) - if data: + + if keys: + # filter to requested keys + filtered = [] + if meth in data: # often a list of dicts + for item in data[meth]: + filtered.append({key: item[key] for key in keys}) + + else: # likely just a dict + filtered.append({key: data[key] for key in keys}) + data = filtered + + click.echo(colorize_json(data)) + + +@cli.command() +@click.option('--broker', default='questrade', help='Broker backend to use') +@click.option('--loglevel', '-l', default='warning', help='Logging level') +@click.option('--df-output', '-df', flag_value=True, + help='Ouput in `pandas.DataFrame` format') +@click.argument('tickers', nargs=-1) +def quote(loglevel, broker, tickers, df_output): + """client for testing broker API methods with pretty printing of output. + """ + brokermod = import_module('.' + broker, 'piker.brokers') + data = run(partial(brokermod.quote, tickers), loglevel=loglevel) + quotes = data['quotes'] + cols = quotes[0].copy() + cols.pop('symbol') + if df_output: + df = pd.DataFrame( + data['quotes'], + index=[item['symbol'] for item in quotes], + columns=cols, + ) + click.echo(df) + else: click.echo(colorize_json(data)) @@ -55,7 +94,7 @@ def api(meth, kwargs, loglevel, broker): @click.option('--broker', default='questrade', help='Broker backend to use') @click.option('--loglevel', '-l', default='info', help='Logging level') @click.argument('tickers', nargs=-1) -def stream(broker, loglevel, tickers): +def stream(broker, loglevel, tickers, keys): # import broker module daemon entry point bm = import_module('.' + broker, 'piker.brokers') run(