Return None on failed symbol lookups

kivy_mainline_and_py3.8
Tyler Goodlet 2018-03-20 21:01:55 -04:00
parent 04fa3c7ca4
commit 48fe280e0c
3 changed files with 23 additions and 7 deletions

View File

@ -37,7 +37,12 @@ async def quote(brokermod: ModuleType, tickers: [str]) -> dict:
"""Return quotes dict for ``tickers``. """Return quotes dict for ``tickers``.
""" """
async with brokermod.get_client() as client: async with brokermod.get_client() as client:
return await client.quote(tickers) results = await client.quote(tickers)
for key, val in results.items():
if val is None:
brokermod.log.warn(f"Could not find symbol {key}?")
return results
async def poll_tickers( async def poll_tickers(

View File

@ -152,7 +152,15 @@ class Client:
t2ids = await self.tickers2ids(tickers) t2ids = await self.tickers2ids(tickers)
ids = ','.join(map(str, t2ids.values())) ids = ','.join(map(str, t2ids.values()))
results = (await self.api.quotes(ids=ids))['quotes'] results = (await self.api.quotes(ids=ids))['quotes']
return {sym: quote for sym, quote in zip(tickers, results)} quotes = {quote['symbol']: quote for quote in results}
# set None for all symbols not found
if len(t2ids) < len(tickers):
for ticker in tickers:
if ticker not in quotes:
quotes[ticker] = None
return quotes
async def symbols(self, tickers): async def symbols(self, tickers):
"""Return quotes for each ticker in ``tickers``. """Return quotes for each ticker in ``tickers``.
@ -275,6 +283,8 @@ async def get_client() -> Client:
@asynccontextmanager @asynccontextmanager
async def quoter(client: Client, tickers: [str]): async def quoter(client: Client, tickers: [str]):
"""Quoter context.
"""
t2ids = await client.tickers2ids(tickers) t2ids = await client.tickers2ids(tickers)
ids = ','.join(map(str, t2ids.values())) ids = ','.join(map(str, t2ids.values()))

View File

@ -1,8 +1,8 @@
""" """
Robinhood API backend. Robinhood API backend.
""" """
import asks
from async_generator import asynccontextmanager from async_generator import asynccontextmanager
import asks
from ..log import get_logger from ..log import get_logger
from ._util import resproc from ._util import resproc
@ -27,7 +27,8 @@ class _API:
return await self._request('quotes/', params={'symbols': symbols}) return await self._request('quotes/', params={'symbols': symbols})
async def fundamentals(self, symbols: str) -> dict: async def fundamentals(self, symbols: str) -> dict:
return await self._request('fundamentals/', params={'symbols': symbols}) return await self._request(
'fundamentals/', params={'symbols': symbols})
class Client: class Client:
@ -40,9 +41,9 @@ class Client:
self.api = _API(self._sess) self.api = _API(self._sess)
async def quote(self, symbols: [str]): async def quote(self, symbols: [str]):
resp = await self.api.quotes(','.join(symbols)) results = (await self.api.quotes(','.join(symbols)))['results']
results = resp['results'] return {quote['symbol'] if quote else sym: quote
return {sym: quote for sym, quote in zip(symbols, results)} for sym, quote in zip(symbols, results)}
@asynccontextmanager @asynccontextmanager