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
Original file line number Diff line number Diff line change
@@ -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
---
10 changes: 9 additions & 1 deletion stripe/_stripe_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,22 @@ 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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this discards "raw_request" from the usage array, which we need to keep for logging usage through the raw_request mechanism.


rbody, rcode, rheaders = await self._requestor.request_raw_async(
method_,
url_,
params=params,
options=options,
base_address=base_address,
api_mode=api_mode,
usage=["raw_request"],
usage=usage,
)

return self._requestor._interpret_response(
Expand Down
98 changes: 76 additions & 22 deletions tests/test_raw_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import datetime

import pytest

import stripe

from tests.test_api_requestor import GMT1
Expand Down Expand Up @@ -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
):
Expand Down
40 changes: 39 additions & 1 deletion tests/test_v2_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down Expand Up @@ -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(
Expand Down