diff --git a/.hark/changes/2026-11-16_ajaykr0905_async-event-fetch-usage.change.md b/.hark/changes/2026-11-16_ajaykr0905_async-event-fetch-usage.change.md new file mode 100644 index 000000000..27d6971c6 --- /dev/null +++ b/.hark/changes/2026-11-16_ajaykr0905_async-event-fetch-usage.change.md @@ -0,0 +1,6 @@ +--- +title: Fix `fetch_related_object_async()` and `fetch_event_async()` sending the internal `usage` telemetry to the API as a request parameter +semver_level: patch +github_issues_resolved: +- https://github.com/stripe/stripe-python/issues/1948 +--- diff --git a/stripe/_stripe_client.py b/stripe/_stripe_client.py index d6c669358..da99833e8 100644 --- a/stripe/_stripe_client.py +++ b/stripe/_stripe_client.py @@ -311,6 +311,14 @@ async def raw_request_async(self, method_: str, url_: str, **params): api_mode = get_api_mode(url_) base_address = params.pop("base", "api") + # we manually pass usage in event internals, so use those if available. + # Those are always lists. Any other `usage` (e.g. a SetupIntent's + # "off_session") is a real API param, and request_raw_async can't + # append "async" to it. + usage = ["raw_request"] + if isinstance(params.get("usage"), list): + usage = params.pop("usage") + rbody, rcode, rheaders = await self._requestor.request_raw_async( method_, url_, @@ -318,7 +326,7 @@ async def raw_request_async(self, method_: str, url_: str, **params): options=options, base_address=base_address, api_mode=api_mode, - usage=["raw_request"], + usage=usage, ) return self._requestor._interpret_response( diff --git a/tests/test_raw_request.py b/tests/test_raw_request.py index 28e1ff869..d3f0dc9fb 100644 --- a/tests/test_raw_request.py +++ b/tests/test_raw_request.py @@ -2,6 +2,8 @@ import datetime +import pytest + import stripe from tests.test_api_requestor import GMT1 @@ -172,28 +174,80 @@ def test_v2_request_overridden_api_version( is_json=True, ) - # TODO(jar) this test is not applicable yet, but may be some day - # @pytest.mark.anyio - # async def test_form_request_get_async( - # self, http_client_mock, stripe_mock_stripe_client - # ): - # http_client_mock.stub_request( - # "get", - # path=self.GET_REL_URL, - # rbody='{"id": "acct_123", "object": "account"}', - # rcode=200, - # rheaders={}, - # ) - # - # resp = await stripe_mock_stripe_client.raw_request_async( - # "get", self.GET_REL_URL - # ) - # - # http_client_mock.assert_requested("get", path=self.GET_REL_URL) - # - # deserialized = stripe_mock_stripe_client.deserialize(resp) - # assert isinstance(deserialized, stripe.Account) - # + @pytest.mark.anyio + async def test_form_request_get_async( + self, http_client_mock, stripe_mock_stripe_client + ): + http_client_mock.stub_request( + "get", + path=self.GET_REL_URL, + rbody='{"id": "acct_123", "object": "account"}', + rcode=200, + rheaders={}, + ) + + resp = await stripe_mock_stripe_client.raw_request_async( + "get", self.GET_REL_URL + ) + + http_client_mock.assert_requested( + "get", path=self.GET_REL_URL, usage=["raw_request", "async"] + ) + + deserialized = stripe_mock_stripe_client.deserialize( + resp, api_mode="V1" + ) + assert isinstance(deserialized, stripe.Account) + + @pytest.mark.anyio + async def test_raw_request_async_reports_passed_usage( + self, http_client_mock, stripe_mock_stripe_client + ): + http_client_mock.stub_request( + "post", + path=self.POST_REL_URL, + rbody='{"id": "acct_123", "object": "account"}', + rcode=200, + rheaders={}, + ) + + await stripe_mock_stripe_client.raw_request_async( + "post", + self.POST_REL_URL, + usage=["fetch_related_object"], + **self.ENCODE_INPUTS, + ) + + http_client_mock.assert_requested( + "post", + path=self.POST_REL_URL, + post_data="type=standard&int=123&datetime=1356994801", + usage=["fetch_related_object", "async"], + ) + + @pytest.mark.anyio + async def test_raw_request_async_sends_non_list_usage_as_param( + self, http_client_mock, stripe_mock_stripe_client + ): + http_client_mock.stub_request( + "post", + path="/v1/setup_intents", + rbody='{"id": "seti_123", "object": "setup_intent"}', + rcode=200, + rheaders={}, + ) + + await stripe_mock_stripe_client.raw_request_async( + "post", "/v1/setup_intents", usage="off_session" + ) + + http_client_mock.assert_requested( + "post", + path="/v1/setup_intents", + post_data="usage=off_session", + usage=["raw_request", "async"], + ) + def test_raw_request_usage_reported( self, http_client_mock, stripe_mock_stripe_client ): diff --git a/tests/test_v2_event.py b/tests/test_v2_event.py index 8e93df42a..1fd08aa13 100644 --- a/tests/test_v2_event.py +++ b/tests/test_v2_event.py @@ -431,6 +431,45 @@ def test_v2_events_integration( assert isinstance(meter, Meter) assert meter.event_name == "cool event" + @pytest.mark.anyio + async def test_async_fetches_send_usage_only_as_telemetry( + self, + http_client_mock: HTTPClientMock, + v2_payload_no_data, + v2_payload_with_data, + parse_event_notif: EventParser, + ): + path = "/v2/core/events/evt_234" + meter_path = "/v1/billing/meters/mtr_123" + + # stubs only match requests without a query string, so a `usage` + # that leaks into the request params fails the fetch + http_client_mock.stub_request( + "get", path=path, rbody=v2_payload_with_data + ) + http_client_mock.stub_request( + "get", + path=meter_path, + rbody=json.dumps({"id": "mtr_123", "object": "billing.meter"}), + ) + + event_notif = parse_event_notif(v2_payload_no_data) + assert event_notif.type == "v1.billing.meter.error_report_triggered" + + event = await event_notif.fetch_event_async() + meter = await event_notif.fetch_related_object_async() + + assert isinstance(event, V1BillingMeterErrorReportTriggeredEvent) + assert isinstance(meter, Meter) + http_client_mock.assert_requested( + "get", + path=path, + usage=["pushed_event_pull", "pushed_event_pull_async", "async"], + ) + http_client_mock.assert_requested( + "get", path=meter_path, usage=["fetch_related_object", "async"] + ) + def test_related_object_retains_event_context( self, http_client_mock: HTTPClientMock ): @@ -510,7 +549,6 @@ async def test_related_object_retains_event_context_async( http_client_mock.stub_request( "get", path=path, - query_string="usage[0]=fetch_related_object", rbody=json.dumps({"id": "prod_shared", "object": "product"}), ) http_client_mock.stub_request(