Skip to content

Commit

Permalink
MOD: Stream compressed for corporate actions API
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Aug 13, 2024
1 parent a10edad commit 44b745f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 8 additions & 1 deletion databento/reference/api/corporate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

from collections.abc import Iterable
from datetime import date
from io import BytesIO
from io import StringIO

import pandas as pd
import zstandard
from databento_dbn import Compression
from databento_dbn import SType

from databento.common import API_VERSION
Expand Down Expand Up @@ -112,6 +115,7 @@ def get_range(
"events": ",".join(events) if events else None,
"countries": ",".join(countries) if countries else None,
"security_types": ",".join(security_types) if security_types else None,
"compression": str(Compression.ZSTD), # Always request zstd
}

response = self._post(
Expand All @@ -120,7 +124,10 @@ def get_range(
basic_auth=True,
)

df = pd.read_json(StringIO(response.text), lines=True)
decompressor = zstandard.ZstdDecompressor()
decompressed_content = decompressor.stream_reader(BytesIO(response.content)).read()

df = pd.read_json(StringIO(decompressed_content.decode()), lines=True)
if df.empty:
return df

Expand Down
12 changes: 7 additions & 5 deletions tests/test_reference_corporate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pandas as pd
import pytest
import requests
import zstandard
from databento.reference.client import Reference

from tests import TESTS_ROOT
Expand Down Expand Up @@ -77,7 +78,7 @@ def test_corporate_actions_get_range_sends_expected_request(
) -> None:
# Arrange
mock_response = MagicMock()
mock_response.text = "{}"
mock_response.content = zstandard.compress(b"{}")
mock_response.__enter__.return_value = mock_response
mock_response.__exit__ = MagicMock()
monkeypatch.setattr(requests, "post", mock_post := MagicMock(return_value=mock_response))
Expand Down Expand Up @@ -110,6 +111,7 @@ def test_corporate_actions_get_range_sends_expected_request(
"events": expected_events,
"countries": expected_countries,
"security_types": expected_security_types,
"compression": "zstd",
}
assert call["timeout"] == (100, 100)
assert isinstance(call["auth"], requests.auth.HTTPBasicAuth)
Expand All @@ -122,7 +124,7 @@ def test_corporate_actions_get_range_response_parsing_as_pit(
# Arrange
data_path = Path(TESTS_ROOT) / "data" / "REFERENCE" / "test_data.corporate-actions.ndjson"
mock_response = MagicMock()
mock_response.text = data_path.read_text()
mock_response.content = zstandard.compress(data_path.read_bytes())
mock_response.__enter__.return_value = mock_response
mock_response.__exit__ = MagicMock()
monkeypatch.setattr(requests, "post", MagicMock(return_value=mock_response))
Expand Down Expand Up @@ -152,7 +154,7 @@ def test_corporate_actions_get_range_response(
# Arrange
data_path = Path(TESTS_ROOT) / "data" / "REFERENCE" / "test_data.corporate-actions-pit.ndjson"
mock_response = MagicMock()
mock_response.text = data_path.read_text()
mock_response.content = zstandard.compress(data_path.read_bytes())
mock_response.__enter__.return_value = mock_response
mock_response.__exit__ = MagicMock()
monkeypatch.setattr(requests, "post", MagicMock(return_value=mock_response))
Expand All @@ -178,7 +180,7 @@ def test_corporate_actions_get_range_with_ts_record_index(
# Arrange
data_path = Path(TESTS_ROOT) / "data" / "REFERENCE" / "test_data.corporate-actions.ndjson"
mock_response = MagicMock()
mock_response.text = data_path.read_text()
mock_response.content = zstandard.compress(data_path.read_bytes())
mock_response.__enter__.return_value = mock_response
mock_response.__exit__ = MagicMock()
monkeypatch.setattr(requests, "post", MagicMock(return_value=mock_response))
Expand Down Expand Up @@ -212,7 +214,7 @@ def test_corporate_actions_get_range_without_flattening(
# Arrange
data_path = Path(TESTS_ROOT) / "data" / "REFERENCE" / "test_data.corporate-actions.ndjson"
mock_response = MagicMock()
mock_response.text = data_path.read_text()
mock_response.content = zstandard.compress(data_path.read_bytes())
mock_response.__enter__.return_value = mock_response
mock_response.__exit__ = MagicMock()
monkeypatch.setattr(requests, "post", MagicMock(return_value=mock_response))
Expand Down

0 comments on commit 44b745f

Please sign in to comment.