Skip to content

Commit

Permalink
Merge pull request #50 from alpacahq/fixPep8Stream2
Browse files Browse the repository at this point in the history
Fix flake8 errors
  • Loading branch information
ttt733 authored Dec 24, 2018
2 parents 722443d + c27d8d3 commit 888eff0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 6 additions & 3 deletions alpaca_trade_api/stream2.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ async def _connect(self):
r = r.decode('utf-8')
msg = json.loads(r)

if not 'data' in msg or msg['data']['status'] != 'authorized':
raise ValueError("Invalid Alpaca API credentials, Failed to authenticate: {}".format(msg))
if 'data' not in msg or msg['data']['status'] != 'authorized':
raise ValueError(
("Invalid Alpaca API credentials, Failed to authenticate: {}"
.format(msg))
)

self._ws = ws
await self._dispatch('authenticated', msg)
await self._dispatch('authorized', msg)

asyncio.ensure_future(self._consume_msg())
return ws
Expand Down
8 changes: 4 additions & 4 deletions tests/test_stream2.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ def test_stream(websockets):
ws.recv = AsyncMock(return_value=json.dumps({
'stream': 'authentication',
'data': {
'status': 'authenticated',
'status': 'authorized',
}
}).encode())

conn = StreamConn('key-id', 'secret-key')
conn._consume_msg = AsyncMock()

@conn.on('authenticated')
@conn.on('authorized')
async def on_auth(conn, stream, msg):
on_auth.msg = msg
_run(conn._connect())
assert on_auth.msg.status == 'authenticated'
assert on_auth.msg.status == 'authorized'
assert conn._consume_msg.mock.called

conn.deregister('authenticated')
conn.deregister('authorized')
assert len(conn._handlers) == 0

with pytest.raises(ValueError):
Expand Down

0 comments on commit 888eff0

Please sign in to comment.