Skip to content

Commit

Permalink
Fix BaseStream cast method to handle msgpack.ext.Timestamp (#346)
Browse files Browse the repository at this point in the history
* fix: timestamp handler in stream

* fix: cast msgpack timestamp

* fix: formatting

* websocket params as base model

* fix: use timestamp param on msgpack.unpackb

* fix: reverted unrelated changes

* fix: comments

* fix: removed unused imports

---------

Co-authored-by: hiohiohio <[email protected]>
  • Loading branch information
alessiocastrica and hiohiohio authored Oct 4, 2023
1 parent bfc6aa9 commit 0b084aa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,6 @@ dmypy.json
#IDE configs
.idea
.vscode/

# Cloudflare stuff
Cloudflare_CA.pem
6 changes: 4 additions & 2 deletions alpaca/common/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ def _cast(self, msg_type: str, msg: Dict) -> Union[BaseModel, RawData]:
"""
result = msg
if not self._raw_data:
# convert msgpack timestamp to nanoseconds

if "t" in msg:
msg["t"] = msg["t"].seconds * int(1e9) + msg["t"].nanoseconds
msg["t"] = msg["t"].to_datetime()

if "S" not in msg:
return msg
Expand Down Expand Up @@ -443,6 +443,8 @@ def run(self) -> None:
except KeyboardInterrupt:
print("keyboard interrupt, bye")
pass
finally:
self.stop()

def stop(self) -> None:
"""Stops the websocket connection."""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "alpaca-py"
version = "0.11.0"
version = "0.11.1"
description = "The Official Python SDK for Alpaca APIs"
authors = [
"Rahul Chowdhury <[email protected]>",
Expand Down
26 changes: 12 additions & 14 deletions tests/data/test_websockets.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
import pytest
from msgpack.ext import Timestamp

from alpaca.common.websocket import BaseStream
from alpaca.data.enums import Exchange
from alpaca.data.models import Bar, Trade


@pytest.fixture
def ws_client():
client = BaseStream("endpoint", "key-id", "secret-key")
return client
def ws_client() -> BaseStream:
"""Socket client fixture with pydantic models as output."""
return BaseStream("endpoint", "key-id", "secret-key")


@pytest.fixture
def raw_ws_client():
raw_client = BaseStream("endpoint", "key-id", "secret-key", raw_data=True)
return raw_client
def raw_ws_client() -> BaseStream:
"""Socket client fixture with raw data output."""
return BaseStream("endpoint", "key-id", "secret-key", raw_data=True)


@pytest.fixture
def timestamp():
class MockTimestamp:
def __init__(self, _seconds, _nanoseconds):
self.seconds = _seconds
self.nanoseconds = _nanoseconds
def timestamp() -> Timestamp:
"""Msgpack mock timestamp."""
return Timestamp(seconds=10, nanoseconds=10)

return MockTimestamp(0, 0)


def test_cast(ws_client, raw_ws_client, timestamp):
def test_cast(ws_client: BaseStream, raw_ws_client: BaseStream, timestamp: Timestamp):
"""Test the value error in case there's a different timestamp type."""
# Bar
bar_msg_type = "b"
bar_msg_dict = {
Expand Down

0 comments on commit 0b084aa

Please sign in to comment.