4545)
4646from posthog .poller import Poller
4747from posthog .request import (
48- DEFAULT_HOST ,
4948 APIError ,
5049 QuotaLimitError ,
5150 RequestsConnectionError ,
5453 determine_server_host ,
5554 flags ,
5655 get ,
56+ normalize_host ,
5757 remote_config ,
5858)
5959from posthog .types import (
@@ -221,14 +221,14 @@ def __init__(
221221 self .queue = queue .Queue (max_queue_size )
222222
223223 # api_key: This should be the Team API Key (token), public
224- self .api_key = project_api_key
224+ self .api_key = project_api_key . strip ()
225225
226226 self .on_error = on_error
227227 self .debug = debug
228228 self .send = send
229229 self .sync_mode = sync_mode
230230 # Used for session replay URL generation - we don't want the server host here.
231- self .raw_host = host or DEFAULT_HOST
231+ self .raw_host = normalize_host ( host )
232232 self .host = determine_server_host (host )
233233 self .gzip = gzip
234234 self .timeout = timeout
@@ -278,7 +278,11 @@ def __init__(
278278 self .project_root = project_root
279279
280280 # personal_api_key: This should be a generated Personal API Key, private
281- self .personal_api_key = personal_api_key
281+ self .personal_api_key = (
282+ personal_api_key .strip ()
283+ if isinstance (personal_api_key , str )
284+ else personal_api_key
285+ ) or None
282286 if debug :
283287 # Ensures that debug level messages are logged when debug mode is on.
284288 # Otherwise, defaults to WARNING level. See https://docs.python.org/3/howto/logging.html#what-happens-if-no-configuration-is-provided
@@ -287,6 +291,11 @@ def __init__(
287291 else :
288292 self .log .setLevel (logging .WARNING )
289293
294+ if not self .api_key :
295+ self .log .error (
296+ "api_key is empty after trimming whitespace; check your project API key"
297+ )
298+
290299 self ._set_before_send (before_send )
291300
292301 if self .enable_exception_autocapture :
@@ -1288,12 +1297,19 @@ def _load_feature_flags(self):
12881297
12891298 def _fetch_feature_flags_from_api (self ):
12901299 """Fetch feature flags from the PostHog API."""
1300+ personal_api_key = self .personal_api_key
1301+ if personal_api_key is None :
1302+ self .log .warning (
1303+ "[FEATURE FLAGS] You have to specify a personal_api_key to use feature flags."
1304+ )
1305+ return
1306+
12911307 try :
12921308 # Store old flags to detect changes
12931309 old_flags_by_key : dict [str , dict ] = self .feature_flags_by_key or {}
12941310
12951311 response = get (
1296- self . personal_api_key ,
1312+ personal_api_key ,
12971313 f"/flags/definitions?token={ self .api_key } &send_cohorts" ,
12981314 self .host ,
12991315 timeout = 10 ,
0 commit comments