Skip to content
Open
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
23 changes: 10 additions & 13 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@
)
from posthog.version import VERSION

try:
import queue
except ImportError:
import Queue as queue

from queue import Queue, Full


MAX_DICT_SIZE = 50_000
Expand Down Expand Up @@ -279,7 +277,7 @@ def __init__(
Initialization
"""
self._max_queue_size = max_queue_size
self.queue = queue.Queue(max_queue_size)
self.queue: Queue = Queue(max_queue_size)

# api_key: This should be the Team API Key (token), public
self.api_key = (project_api_key or "").strip()
Expand All @@ -293,8 +291,10 @@ def __init__(
self.host = determine_server_host(host)
self.gzip = gzip
self.timeout = timeout
self._feature_flags = None # private variable to store flags
self.feature_flags_by_key = None
self._feature_flags: Optional[list[Any]] = (
None # private variable to store flags
)
self.feature_flags_by_key: Optional[dict[str, Any]] = None
self.group_type_mapping: Optional[dict[str, str]] = None
self.cohorts: Optional[dict[str, Any]] = None
self.poll_interval = poll_interval
Expand Down Expand Up @@ -1245,7 +1245,7 @@ def _reinit_after_fork(self):
as they'll be handled by the parent process's consumers.
"""
if self.consumers:
self.queue = queue.Queue(self._max_queue_size)
self.queue = Queue(self._max_queue_size)

new_consumers = []
for old in self.consumers:
Expand Down Expand Up @@ -1366,7 +1366,7 @@ def _enqueue(self, msg, disable_geoip):
self.queue.put(msg, block=False)
self.log.debug("enqueued %s.", msg["event"])
return sent_uuid
except queue.Full:
except Full:
self.log.warning("analytics-python queue is full")
return None

Expand Down Expand Up @@ -2795,10 +2795,7 @@ def _initialize_flag_cache(self, cache_url):
if not cache_url:
return None

try:
from urllib.parse import parse_qs, urlparse
except ImportError:
from urlparse import parse_qs, urlparse
from urllib.parse import parse_qs, urlparse

try:
parsed = urlparse(cache_url)
Expand Down
13 changes: 6 additions & 7 deletions posthog/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

from posthog.request import APIError, DatetimeSerializer, batch_post

try:
from queue import Empty
except ImportError:
from Queue import Empty
from queue import Empty


MAX_MSG_SIZE = 900 * 1024 # 900KiB per event
Expand Down Expand Up @@ -133,9 +130,11 @@ def is_retryable(exc):
# retry on server errors and client errors
# with 408 (request timeout) or 429 (rate limited),
# don't retry on other client errors
if exc.status == "N/A":
return False
return not ((400 <= exc.status < 500) and exc.status not in (408, 429))
if isinstance(exc.status, int):
return not (
(400 <= exc.status < 500) and exc.status not in (408, 429)
)
return False
else:
# retry on all other errors (eg. network)
return True
Expand Down
2 changes: 1 addition & 1 deletion posthog/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def compare(lhs, rhs, operator):

parsed_value = None
try:
parsed_value = float(value) # type: ignore
parsed_value = float(value)
except Exception:
pass

Expand Down
9 changes: 5 additions & 4 deletions posthog/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import Any, List, Optional, Tuple, Union

import requests
from requests.adapters import HTTPAdapter # type: ignore[import-untyped]
from requests.adapters import HTTPAdapter
Comment thread
mishrak5j marked this conversation as resolved.
from urllib3.connection import HTTPConnection
from urllib3.util.retry import Retry

Expand Down Expand Up @@ -219,7 +219,7 @@
def post(
api_key: str,
host: Optional[str] = None,
path=None,
path: str = "",
gzip: bool = False,
timeout: int = 15,
session: Optional[requests.Session] = None,
Expand All @@ -235,18 +235,19 @@
data = json.dumps(body, cls=DatetimeSerializer)
log.debug("making request: %s to url: %s", data, url)
headers = {"Content-Type": "application/json", "User-Agent": USER_AGENT}
payload: str | bytes = data
Comment thread
mishrak5j marked this conversation as resolved.
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(data.encode("utf-8"))
data = buf.getvalue()
payload = buf.getvalue()

res = (session or _get_session()).post(
url, data=data, headers=headers, timeout=timeout
url, data=payload, headers=headers, timeout=timeout
)

Check failure

Code scanning / CodeQL

Full server-side request forgery Critical

The full URL of this request depends on a
user-provided value
.

if res.status_code == 200:
log.debug("data uploaded successfully")
Expand Down
Loading