Change `Client.symbols()` to `symbol_data()`

Make `symbol_data()` a common backend API method for looking up
auxiliary ticker/symbol data from brokers. It seems most have such
a call/endpoint.
kivy_mainline_and_py3.8
Tyler Goodlet 2018-03-27 16:24:45 -04:00
parent 8fc74c5085
commit 8544c22cdc
2 changed files with 23 additions and 12 deletions

View File

@ -17,7 +17,7 @@ from ..log import get_logger, colorize_json
import asks import asks
asks.init('trio') asks.init('trio')
log = get_logger('questrade') log = get_logger(__name__)
_refresh_token_ep = 'https://login.questrade.com/oauth2/' _refresh_token_ep = 'https://login.questrade.com/oauth2/'
_version = 'v1' _version = 'v1'
@ -165,8 +165,8 @@ class Client:
return quotes return quotes
async def symbols(self, tickers): async def symbol_data(self, tickers: [str]):
"""Return quotes for each ticker in ``tickers``. """Return symbol data for ``tickers``.
""" """
t2ids = await self.tickers2ids(tickers) t2ids = await self.tickers2ids(tickers)
ids = ','.join(map(str, t2ids.values())) ids = ','.join(map(str, t2ids.values()))

View File

@ -4,14 +4,15 @@ Robinhood API backend.
from functools import partial from functools import partial
from async_generator import asynccontextmanager from async_generator import asynccontextmanager
# TODO: move to urllib3/requests once supported
import asks import asks
from ..log import get_logger from ..log import get_logger
from ._util import resproc from ._util import resproc
from ..calc import percent_change from ..calc import percent_change
log = get_logger('robinhood') asks.init('trio')
log = get_logger(__name__)
_service_ep = 'https://api.robinhood.com' _service_ep = 'https://api.robinhood.com'
@ -43,15 +44,25 @@ class Client:
self._sess.base_location = _service_ep self._sess.base_location = _service_ep
self.api = _API(self._sess) self.api = _API(self._sess)
async def quote(self, symbols: [str]): def _zip_in_order(self, symbols: [str], results_dict: dict):
results = (await self.api.quotes(','.join(symbols)))['results'] return {quote.get('symbol', sym) if quote else sym: quote
return {quote['symbol'] if quote else sym: quote for sym, quote in zip(symbols, results_dict)}
for sym, quote in zip(symbols, results)}
async def symbols(self, tickers: [str]): async def quote(self, symbols: [str]):
"""Placeholder for the watchlist calling code... """Retrieve quotes for a list of ``symbols``.
""" """
return {} return self._zip_in_order(
symbols,
(await self.api.quotes(','.join(symbols)))['results']
)
async def symbol_data(self, symbols: [str]):
"""Retrieve symbol data via the ``fundmentals`` endpoint.
"""
return self._zip_in_order(
symbols,
(await self.api.fundamentals(','.join(symbols)))['results']
)
@asynccontextmanager @asynccontextmanager