:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(appsflyer): tell users when the daily report quota is used up (#130728)
Tom Owersf53c0ca49d7b
diff --git a/products/warehouse_sources/sources/appsflyer/appsflyer.py b/products/warehouse_sources/sources/appsflyer/appsflyer.pyindex 9b39512b61a1..8546f469b31e 100644--- a/products/warehouse_sources/sources/appsflyer/appsflyer.py+++ b/products/warehouse_sources/sources/appsflyer/appsflyer.py@@ -24,6 +24,7 @@ from products.warehouse_sources.sources.sdk import ()APPSFLYER_BASE_URL = "https://hq1.appsflyer.com"+QUOTA_EXHAUSTED_MARKER = "limit reached"# Aggregate pull requests cap the date range at ~1000 days.MAX_WINDOW_DAYS = 999# AppsFlyer doesn't finalize data until ~48h after the UTC day; re-fetch the@@ -151,6 +152,11 @@ def _credentials_error(message: str, response: requests.Response, api_token: strreturn AppsFlyerCredentialsError(f"{message} AppsFlyer said: {reason}" if reason else message)+def _is_quota_exhausted(response: requests.Response) -> bool:+ """AppsFlyer answers 403 (sometimes 429) with a "limit reached" body once the daily report quota is spent."""+ return response.status_code in (403, 429) and QUOTA_EXHAUSTED_MARKER in response.text.lower()++def _normalize_header(header: str) -> str:"""CSV headers like 'Media Source (pid)' become stable snake_case columns."""return re.sub(r"[^0-9a-zA-Z]+", "_", header).strip("_").lower()@@ -266,7 +272,7 @@ def _request_windows(kind: AppsFlyerReportKind, start: date, end: date) -> Iteradef _open_report(session: requests.Session, url: str, logger: FilteringBoundLogger) -> requests.Response:response = session.get(url, timeout=REQUEST_TIMEOUT_SECONDS, stream=True)- if response.status_code == 429 or response.status_code >= 500:+ if response.status_code == 429 or response.status_code >= 500 or _is_quota_exhausted(response):response.close()raise AppsFlyerRetryableError(f"AppsFlyer API error (retryable): status={response.status_code}, url={url}")@@ -321,6 +327,11 @@ def validate_credentials(api_token: str, app_id: str) -> bool:f"?{urlencode({'from': today, 'to': today})}",timeout=30,)+ if _is_quota_exhausted(response):+ raise AppsFlyerCredentialsError(+ "Your AppsFlyer account has used up its daily report API quota. "+ "Try connecting again after your daily quota resets."+ )if response.status_code == 429 or response.status_code >= 500:raise AppsFlyerRetryableError(f"AppsFlyer API error (retryable): status={response.status_code}")if response.status_code == 200:diff --git a/products/warehouse_sources/sources/appsflyer/tests/test_appsflyer.py b/products/warehouse_sources/sources/appsflyer/tests/test_appsflyer.pyindex b9347113e8ab..e5bb4dd60011 100644--- a/products/warehouse_sources/sources/appsflyer/tests/test_appsflyer.py+++ b/products/warehouse_sources/sources/appsflyer/tests/test_appsflyer.py@@ -3,6 +3,8 @@ from datetime import UTC, date, datetime, timedeltaimport pytestfrom unittest import mock+import requests+from products.warehouse_sources.sources.appsflyer._config import AppsFlyerSourceConfigfrom products.warehouse_sources.sources.appsflyer.appsflyer import (CHUNK_SIZE,@@ -147,6 +149,17 @@ class TestValidateCredentials:assert message.endswith(expected_reason)assert "secret-token" not in message+ @pytest.mark.parametrize("status_code", [403, 429])+ @pytest.mark.parametrize("body", ["Limit reached for daily-report", " LIMIT REACHED for geo-report\n"])+ def test_validate_reports_used_up_quota_instead_of_access_denied(self, status_code: int, body: str) -> None:+ with scripted_network(always(ScriptedResponse(status=status_code, body=body.encode()))):+ with pytest.raises(AppsFlyerCredentialsError) as exc:+ validate_credentials("token", "id123")+ message = str(exc.value)+ assert "used up its daily report API quota" in message+ assert "denied access" not in message+ assert "AppsFlyer said" not in message+@pytest.mark.parametrize("api_token", [" token ", "Bearer token", "bearer token"])def test_validate_credentials_sends_a_clean_token_and_ios_app_id(self, api_token: str) -> None:with scripted_network([ScriptedResponse(status=200)]) as network:@@ -253,6 +266,20 @@ class TestGetRows:assert headers["Accept"] == "text/csv"+class TestUsedUpQuota:+ @pytest.mark.parametrize("status_code", [403, 429])+ def test_quota_response_is_retryable(self, status_code: int) -> None:+ result = _driver().run("daily_report", always(ScriptedResponse(status=status_code, body=b"Limit reached")))++ assert isinstance(result.raised, AppsFlyerRetryableError)++ def test_forbidden_without_quota_body_stays_a_permanent_error(self) -> None:+ result = _driver().run("daily_report", always(ScriptedResponse(status=403, body=b"Forbidden")))++ assert isinstance(result.raised, requests.HTTPError)+ assert "403 Client Error" in str(result.raised)++class TestRawAndMasterWindows:def test_raw_pull_walks_contiguous_windows_inside_the_lookback_limit(self) -> None:# AppsFlyer refuses a raw window starting more than 90 days back, and the windows have to