: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(data-imports): tolerate Apple's misleading 400 on subscription reports (#75783)
Tom Owers655520477c9a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.py b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.pyindex a1e6501cf3fe..ed48581fbe9d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.py@@ -404,11 +404,12 @@ def _fetch_report(params=params,accept=REPORT_ACCEPT,timeout=REPORT_TIMEOUT_SECONDS,- tolerate=(404,),+ tolerate=config.missing_report_status_codes,)- if response.status_code == 404:+ if response.status_code in config.missing_report_status_codes:# Apple 404s any date with no activity at all — normal for quiet days and for dates before the- # app shipped — so a missing day is not an error.+ # app shipped — so a missing day is not an error. Subscription-family report types 400 for the+ # same condition instead (see `missing_report_status_codes`).return []return _parse_report(response.content, report_date)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/settings.pyindex 0b4c10aa9d00..411c3a6f0908 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/settings.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/settings.py@@ -51,6 +51,11 @@ class AppStoreConnectEndpointConfig:# the DAILY/SUMMARY combination of each report type.report_version: str = ""report_frequency: str = "DAILY"+ # Apple 404s a SALES report request for a date with no data. Subscription-family report types+ # (SUBSCRIPTION, SUBSCRIPTION_EVENT) instead 400 with a misleading "Invalid vendor number+ # specified" error for that same condition — a longstanding, publicly reported Apple API quirk,+ # not an actual credentials problem. Treat both as "no report for this day" for those types.+ missing_report_status_codes: tuple[int, ...] = (404,)_REPORT_DATE_FIELD: IncrementalField = incremental_field("report_date", IncrementalFieldType.Date)@@ -139,6 +144,7 @@ APP_STORE_CONNECT_ENDPOINTS: dict[str, AppStoreConnectEndpointConfig] = {incremental_fields=[_REPORT_DATE_FIELD],partition_key="report_date",should_sync_default=False,+ missing_report_status_codes=(404, 400),),# Daily subscription lifecycle events (renewals, cancellations, upgrades)."subscription_event_reports": AppStoreConnectEndpointConfig(@@ -151,6 +157,7 @@ APP_STORE_CONNECT_ENDPOINTS: dict[str, AppStoreConnectEndpointConfig] = {incremental_fields=[_REPORT_DATE_FIELD],partition_key="report_date",should_sync_default=False,+ missing_report_status_codes=(404, 400),),}diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.py b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.pyindex eceb06aab2fc..fc6efef84bc4 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.py@@ -142,14 +142,15 @@ class _FakeApi:class _FakeReportApi:"""Serves `/v1/salesReports` per requested report date; an unknown date 404s like Apple's does."""- def __init__(self, tsv_by_date: dict[str, str]) -> None:+ def __init__(self, tsv_by_date: dict[str, str], missing_status_code: int = 404) -> None:self.tsv_by_date = tsv_by_date+ self.missing_status_code = missing_status_codeself.calls: list[tuple[str, dict[str, Any]]] = []def get(self, url: str, **kwargs: Any) -> MagicMock:params: dict[str, Any] = kwargs.get("params") or {}self.calls.append((url, params))- return _report_response(self.tsv_by_date.get(params["filter[reportDate]"]))+ return _report_response(self.tsv_by_date.get(params["filter[reportDate]"]), self.missing_status_code)def _collect(@@ -535,6 +536,48 @@ class TestSalesReports:assert [(row["report_date"], row["units"]) for row in rows] == [("2026-03-02", "1"), ("2026-03-04", "2")]assert [params["filter[reportDate]"] for _, params in api.calls] == ["2026-03-02", "2026-03-03", "2026-03-04"]+ @freeze_time("2026-03-05 09:00:00")+ def test_subscription_report_tolerates_apples_misleading_400(self) -> None:+ # Apple 400s (instead of 404) a subscription-family report request for a date with no report+ # available yet — a documented Apple API quirk, not a real credentials failure. It must not+ # poison-pill the walk by raising on every retry of the same day.+ api = _FakeReportApi({"2026-03-04": "SKU\tUnits\nacme\t1\n"}, missing_status_code=400)++ rows = _collect(+ "subscription_reports",+ api,+ _FakeManager(),+ vendor_number="85234567",+ should_use_incremental_field=True,+ db_incremental_field_last_value=date(2026, 3, 2),+ )++ assert [(row["report_date"], row["units"]) for row in rows] == [("2026-03-04", "1")]+ assert [params["filter[reportDate]"] for _, params in api.calls] == ["2026-03-02", "2026-03-03", "2026-03-04"]++ @freeze_time("2026-03-05 09:00:00")+ def test_sales_report_400_is_not_tolerated(self) -> None:+ # SALES reports don't carry the subscription-family quirk, so a 400 there is a real error and+ # must still surface rather than being silently treated as an empty day.+ session = MagicMock()+ bad_request = _report_response(None, missing_status_code=400)+ bad_request.raise_for_status.side_effect = Exception("400 Client Error: Bad Request")+ session.get.return_value = bad_request++ with patch(f"{MODULE}._make_session", return_value=session):+ with pytest.raises(Exception, match="400"):+ list(+ get_rows(+ issuer_id="issuer",+ key_id="KEY123",+ private_key=PRIVATE_KEY_PEM,+ vendor_number="85234567",+ endpoint="sales_reports",+ logger=MagicMock(),+ resumable_source_manager=_FakeManager(),+ )+ )+@freeze_time("2026-03-05 09:00:00")def test_sends_the_report_type_filters_from_settings(self) -> None:api = self._api({"2026-03-04": "SKU\tUnits\nacme\t1\n"})