Skip to content

Commit acd1bcc

Browse files
Deserialize into correct v2 EventData types (#1414)
* Add event data deserialization * Fix python attribute checking * fix pr feedback
1 parent 62e89af commit acd1bcc

File tree

3 files changed

+103
-15
lines changed

3 files changed

+103
-15
lines changed

stripe/events/_v1_billing_meter_error_report_triggered_event.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# -*- coding: utf-8 -*-
22
# File generated from our OpenAPI spec
3+
from stripe._api_mode import ApiMode
4+
from stripe._api_requestor import _APIRequestor
35
from stripe._stripe_object import StripeObject
6+
from stripe._stripe_response import StripeResponse
47
from stripe.billing._meter import Meter
58
from stripe.v2._event import Event
6-
from typing import List, cast
9+
from typing import Any, Dict, List, Optional, cast
710
from typing_extensions import Literal
811

912

@@ -88,6 +91,30 @@ class Request(StripeObject):
8891
Data for the v1.billing.meter.error_report_triggered event
8992
"""
9093

94+
@classmethod
95+
def _construct_from(
96+
cls,
97+
*,
98+
values: Dict[str, Any],
99+
last_response: Optional[StripeResponse] = None,
100+
requestor: "_APIRequestor",
101+
api_mode: ApiMode,
102+
) -> "V1BillingMeterErrorReportTriggeredEvent":
103+
evt = super()._construct_from(
104+
values=values,
105+
last_response=last_response,
106+
requestor=requestor,
107+
api_mode=api_mode,
108+
)
109+
if hasattr(evt, "data"):
110+
evt.data = V1BillingMeterErrorReportTriggeredEvent.V1BillingMeterErrorReportTriggeredEventData._construct_from(
111+
values=evt.data,
112+
last_response=last_response,
113+
requestor=requestor,
114+
api_mode=api_mode,
115+
)
116+
return evt
117+
91118
class RelatedObject(StripeObject):
92119
id: str
93120
"""

stripe/events/_v1_billing_meter_no_meter_found_event.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# -*- coding: utf-8 -*-
22
# File generated from our OpenAPI spec
3+
from stripe._api_mode import ApiMode
4+
from stripe._api_requestor import _APIRequestor
35
from stripe._stripe_object import StripeObject
6+
from stripe._stripe_response import StripeResponse
47
from stripe.v2._event import Event
5-
from typing import List
8+
from typing import Any, Dict, List, Optional
69
from typing_extensions import Literal
710

811

@@ -86,3 +89,27 @@ class Request(StripeObject):
8689
"""
8790
Data for the v1.billing.meter.no_meter_found event
8891
"""
92+
93+
@classmethod
94+
def _construct_from(
95+
cls,
96+
*,
97+
values: Dict[str, Any],
98+
last_response: Optional[StripeResponse] = None,
99+
requestor: "_APIRequestor",
100+
api_mode: ApiMode,
101+
) -> "V1BillingMeterNoMeterFoundEvent":
102+
evt = super()._construct_from(
103+
values=values,
104+
last_response=last_response,
105+
requestor=requestor,
106+
api_mode=api_mode,
107+
)
108+
if hasattr(evt, "data"):
109+
evt.data = V1BillingMeterNoMeterFoundEvent.V1BillingMeterNoMeterFoundEventData._construct_from(
110+
values=evt.data,
111+
last_response=last_response,
112+
requestor=requestor,
113+
api_mode=api_mode,
114+
)
115+
return evt

tests/test_v2_event.py

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import stripe
77
from stripe import ThinEvent
8+
from stripe.events._v1_billing_meter_error_report_triggered_event import (
9+
V1BillingMeterErrorReportTriggeredEvent,
10+
)
811
from tests.test_webhook import DUMMY_WEBHOOK_SECRET, generate_header
912

1013
EventParser = Callable[[str], ThinEvent]
@@ -17,13 +20,13 @@ def v2_payload_no_data(self):
1720
{
1821
"id": "evt_234",
1922
"object": "v2.core.event",
20-
"type": "financial_account.balance.opened",
23+
"type": "v1.billing.meter.error_report_triggered",
2124
"livemode": True,
2225
"created": "2022-02-15T00:27:45.330Z",
2326
"related_object": {
24-
"id": "fa_123",
25-
"type": "financial_account",
26-
"url": "/v2/financial_accounts/fa_123",
27+
"id": "mtr_123",
28+
"type": "billing.meter",
29+
"url": "/v1/billing/meters/mtr_123",
2730
"stripe_context": "acct_123",
2831
},
2932
"reason": {
@@ -39,19 +42,19 @@ def v2_payload_with_data(self):
3942
{
4043
"id": "evt_234",
4144
"object": "v2.core.event",
42-
"type": "financial_account.balance.opened",
45+
"type": "v1.billing.meter.error_report_triggered",
4346
"livemode": False,
4447
"created": "2022-02-15T00:27:45.330Z",
48+
"context": "acct_123",
4549
"related_object": {
46-
"id": "fa_123",
47-
"type": "financial_account",
48-
"url": "/v2/financial_accounts/fa_123",
49-
"stripe_context": "acct_123",
50+
"id": "mtr_123",
51+
"type": "billing.meter",
52+
"url": "/v1/billing/meters/mtr_123",
5053
},
5154
"data": {
52-
"containing_compartment_id": "compid",
53-
"id": "foo",
54-
"type": "bufo",
55+
"reason": {
56+
"error_count": 1,
57+
}
5558
},
5659
}
5760
)
@@ -89,7 +92,7 @@ def test_parses_thin_event(
8992
assert event.id == "evt_234"
9093

9194
assert event.related_object
92-
assert event.related_object.id == "fa_123"
95+
assert event.related_object.id == "mtr_123"
9396

9497
assert event.reason
9598
assert event.reason.id == "foo"
@@ -110,3 +113,34 @@ def test_validates_signature(
110113
stripe_client.parse_thin_event(
111114
v2_payload_no_data, "bad header", DUMMY_WEBHOOK_SECRET
112115
)
116+
117+
def test_v2_events_data_type(self, http_client_mock, v2_payload_with_data):
118+
method = "get"
119+
path = "/v2/core/events/evt_123"
120+
http_client_mock.stub_request(
121+
method,
122+
path=path,
123+
rbody=v2_payload_with_data,
124+
rcode=200,
125+
rheaders={},
126+
)
127+
client = stripe.StripeClient(
128+
api_key="keyinfo_test_123",
129+
http_client=http_client_mock.get_mock_http_client(),
130+
)
131+
event = client.v2.core.events.retrieve("evt_123")
132+
133+
http_client_mock.assert_requested(
134+
method,
135+
api_base=stripe.DEFAULT_API_BASE,
136+
path=path,
137+
api_key="keyinfo_test_123",
138+
)
139+
assert event.id is not None
140+
assert isinstance(event, V1BillingMeterErrorReportTriggeredEvent)
141+
assert event.data is not None
142+
assert isinstance(
143+
event.data,
144+
V1BillingMeterErrorReportTriggeredEvent.V1BillingMeterErrorReportTriggeredEventData,
145+
)
146+
assert event.data.reason.error_count == 1

0 commit comments

Comments
 (0)