Skip to content

Commit a5e8b7d

Browse files
authored
fix(routing): Update hosts to point to right ingestion host (#115)
1 parent efb0ccf commit a5e8b7d

File tree

6 files changed

+61
-15
lines changed

6 files changed

+61
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.4.0 - 2024-02-05
2+
3+
1. Point given hosts to new ingestion hosts
4+
15
## 3.3.4 - 2024-01-30
26

37
1. Update type hints for module variables to work with newer versions of mypy

posthog/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from posthog.consumer import Consumer
1111
from posthog.feature_flags import InconclusiveMatchError, match_feature_flag_properties
1212
from posthog.poller import Poller
13-
from posthog.request import APIError, batch_post, decide, get
13+
from posthog.request import APIError, batch_post, decide, determine_server_host, get
1414
from posthog.utils import SizeLimitedDict, clean, guess_timezone
1515
from posthog.version import VERSION
1616

@@ -61,7 +61,7 @@ def __init__(
6161
self.debug = debug
6262
self.send = send
6363
self.sync_mode = sync_mode
64-
self.host = host
64+
self.host = determine_server_host(host)
6565
self.gzip = gzip
6666
self.timeout = timeout
6767
self.feature_flags = None

posthog/request.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,24 @@
1313

1414
_session = requests.sessions.Session()
1515

16-
DEFAULT_HOST = "https://app.posthog.com"
16+
US_INGESTION_ENDPOINT = "https://us-api.i.posthog.com"
17+
EU_INGESTION_ENDPOINT = "https://eu-api.i.posthog.com"
18+
DEFAULT_HOST = US_INGESTION_ENDPOINT
1719
USER_AGENT = "posthog-python/" + VERSION
1820

1921

22+
def determine_server_host(host: Optional[str]) -> str:
23+
"""Determines the server host to use."""
24+
host_or_default = host or DEFAULT_HOST
25+
trimmed_host = remove_trailing_slash(host_or_default)
26+
if trimmed_host in ("https://app.posthog.com", "https://us.posthog.com"):
27+
return US_INGESTION_ENDPOINT
28+
elif trimmed_host == "https://eu.posthog.com":
29+
return EU_INGESTION_ENDPOINT
30+
else:
31+
return host_or_default
32+
33+
2034
def post(
2135
api_key: str, host: Optional[str] = None, path=None, gzip: bool = False, timeout: int = 15, **kwargs
2236
) -> requests.Response:

posthog/test/test_client.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def test_basic_capture_with_feature_flags_returns_active_only(self, patch_decide
314314
self.assertEqual(patch_decide.call_count, 1)
315315
patch_decide.assert_called_with(
316316
"random_key",
317-
None,
317+
"https://us-api.i.posthog.com",
318318
timeout=10,
319319
distinct_id="distinct_id",
320320
groups={},
@@ -330,7 +330,11 @@ def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(se
330330
}
331331

332332
client = Client(
333-
FAKE_TEST_API_KEY, on_error=self.set_fail, personal_api_key=FAKE_TEST_API_KEY, disable_geoip=True
333+
FAKE_TEST_API_KEY,
334+
host="https://app.posthog.com",
335+
on_error=self.set_fail,
336+
personal_api_key=FAKE_TEST_API_KEY,
337+
disable_geoip=True,
334338
)
335339
success, msg = client.capture("distinct_id", "python test event", send_feature_flags=True, disable_geoip=False)
336340
client.flush()
@@ -351,7 +355,7 @@ def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(se
351355
self.assertEqual(patch_decide.call_count, 1)
352356
patch_decide.assert_called_with(
353357
"random_key",
354-
None,
358+
"https://us-api.i.posthog.com",
355359
timeout=10,
356360
distinct_id="distinct_id",
357361
groups={},
@@ -779,7 +783,7 @@ def test_disable_geoip_default_on_decide(self, patch_decide):
779783
client.get_feature_flag("random_key", "some_id", disable_geoip=True)
780784
patch_decide.assert_called_with(
781785
"random_key",
782-
None,
786+
"https://us-api.i.posthog.com",
783787
timeout=10,
784788
distinct_id="some_id",
785789
groups={},
@@ -791,7 +795,7 @@ def test_disable_geoip_default_on_decide(self, patch_decide):
791795
client.feature_enabled("random_key", "feature_enabled_distinct_id", disable_geoip=True)
792796
patch_decide.assert_called_with(
793797
"random_key",
794-
None,
798+
"https://us-api.i.posthog.com",
795799
timeout=10,
796800
distinct_id="feature_enabled_distinct_id",
797801
groups={},
@@ -803,7 +807,7 @@ def test_disable_geoip_default_on_decide(self, patch_decide):
803807
client.get_all_flags_and_payloads("all_flags_payloads_id")
804808
patch_decide.assert_called_with(
805809
"random_key",
806-
None,
810+
"https://us-api.i.posthog.com",
807811
timeout=10,
808812
distinct_id="all_flags_payloads_id",
809813
groups={},
@@ -829,7 +833,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
829833
patch_decide.return_value = {
830834
"featureFlags": {"beta-feature": "random-variant", "alpha-feature": True, "off-feature": False}
831835
}
832-
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disable_geoip=False)
836+
client = Client(FAKE_TEST_API_KEY, host="http://app2.posthog.com", on_error=self.set_fail, disable_geoip=False)
833837
client.get_feature_flag(
834838
"random_key",
835839
"some_id",
@@ -839,7 +843,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
839843
)
840844
patch_decide.assert_called_with(
841845
"random_key",
842-
None,
846+
"http://app2.posthog.com",
843847
timeout=10,
844848
distinct_id="some_id",
845849
groups={"company": "id:5", "instance": "app.posthog.com"},
@@ -865,7 +869,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
865869
)
866870
patch_decide.assert_called_with(
867871
"random_key",
868-
None,
872+
"http://app2.posthog.com",
869873
timeout=10,
870874
distinct_id="some_id",
871875
groups={"company": "id:5", "instance": "app.posthog.com"},
@@ -882,7 +886,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
882886
client.get_all_flags_and_payloads("some_id", groups={}, person_properties=None, group_properties=None)
883887
patch_decide.assert_called_with(
884888
"random_key",
885-
None,
889+
"http://app2.posthog.com",
886890
timeout=10,
887891
distinct_id="some_id",
888892
groups={},

posthog/test/test_request.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import unittest
33
from datetime import date, datetime
44

5+
import pytest
56
import requests
67

7-
from posthog.request import DatetimeSerializer, batch_post
8+
from posthog.request import DatetimeSerializer, batch_post, determine_server_host
89
from posthog.test.test_utils import TEST_API_KEY
910

1011

@@ -42,3 +43,26 @@ def test_should_timeout(self):
4243
batch_post(
4344
"key", batch=[{"distinct_id": "distinct_id", "event": "python event", "type": "track"}], timeout=0.0001
4445
)
46+
47+
48+
@pytest.mark.parametrize(
49+
"host, expected",
50+
[
51+
("https://t.posthog.com", "https://t.posthog.com"),
52+
("https://t.posthog.com/", "https://t.posthog.com/"),
53+
("t.posthog.com", "t.posthog.com"),
54+
("t.posthog.com/", "t.posthog.com/"),
55+
("https://us.posthog.com.rg.proxy.com", "https://us.posthog.com.rg.proxy.com"),
56+
("app.posthog.com", "app.posthog.com"),
57+
("eu.posthog.com", "eu.posthog.com"),
58+
("https://app.posthog.com", "https://us-api.i.posthog.com"),
59+
("https://eu.posthog.com", "https://eu-api.i.posthog.com"),
60+
("https://us.posthog.com", "https://us-api.i.posthog.com"),
61+
("https://app.posthog.com/", "https://us-api.i.posthog.com"),
62+
("https://eu.posthog.com/", "https://eu-api.i.posthog.com"),
63+
("https://us.posthog.com/", "https://us-api.i.posthog.com"),
64+
(None, "https://us-api.i.posthog.com"),
65+
],
66+
)
67+
def test_routing_to_custom_host(host, expected):
68+
assert determine_server_host(host) == expected

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "3.3.4"
1+
VERSION = "3.4.0"
22

33
if __name__ == "__main__":
44
print(VERSION, end="") # noqa: T201

0 commit comments

Comments
 (0)