Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/doughty-princess-loviatar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

Fall back to uncompressed uploads when gzip compression fails
17 changes: 10 additions & 7 deletions posthog/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,16 @@ def post(
log.debug("making request: %s to url: %s", data, url)
headers = {"Content-Type": "application/json", "User-Agent": USER_AGENT}
if gzip:
headers["Content-Encoding"] = "gzip"
buf = BytesIO()
with GzipFile(fileobj=buf, mode="w") as gz:
# 'data' was produced by json.dumps(),
# whose default encoding is utf-8.
gz.write(cast(str, data).encode("utf-8"))
data = buf.getvalue()
try:
buf = BytesIO()
with GzipFile(fileobj=buf, mode="w") as gz:
# 'data' was produced by json.dumps(),
# whose default encoding is utf-8.
gz.write(cast(str, data).encode("utf-8"))
data = buf.getvalue()
headers["Content-Encoding"] = "gzip"
except Exception as exc:
log.warning("failed to gzip request body, sending uncompressed: %s", exc)

res = (session or _get_session()).post(
url, data=data, headers=headers, timeout=timeout
Expand Down
26 changes: 26 additions & 0 deletions posthog/test/test_request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import unittest
import zlib
from datetime import date, datetime
from unittest import mock

Expand Down Expand Up @@ -179,6 +180,31 @@ def test_post_sends_bytes_payload_with_gzip(self):
self.assertIsInstance(data, bytes)
self.assertEqual(headers["Content-Encoding"], "gzip")

def test_post_falls_back_to_uncompressed_payload_when_gzip_fails(self):
for compression_error in [OSError("boom"), zlib.error("boom")]:
with self.subTest(compression_error=type(compression_error)):
mock_response = requests.Response()
mock_response.status_code = 200
mock_session = mock.MagicMock()
mock_session.post.return_value = mock_response

with mock.patch.object(
request_module, "GzipFile", side_effect=compression_error
):
request_module.post(
TEST_API_KEY,
host="https://test.posthog.com",
path="/batch/",
gzip=True,
session=mock_session,
batch=[],
)

data = mock_session.post.call_args.kwargs["data"]
headers = mock_session.post.call_args.kwargs["headers"]
self.assertIsInstance(data, str)
self.assertNotIn("Content-Encoding", headers)

def test_datetime_serialization(self):
data = {"created": datetime(2012, 3, 4, 5, 6, 7, 891011)}
result = json.dumps(data, cls=DatetimeSerializer)
Expand Down
Loading