Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize into correct v2 EventData types #1414

Merged
merged 4 commits into from
Oct 18, 2024
Merged
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
29 changes: 28 additions & 1 deletion stripe/events/_v1_billing_meter_error_report_triggered_event.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-
# File generated from our OpenAPI spec
from stripe._api_mode import ApiMode
from stripe._api_requestor import _APIRequestor
from stripe._stripe_object import StripeObject
from stripe._stripe_response import StripeResponse
from stripe.billing._meter import Meter
from stripe.v2._event import Event
from typing import List, cast
from typing import Any, Dict, List, Optional, cast
from typing_extensions import Literal


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

@classmethod
def _construct_from(
cls,
*,
values: Dict[str, Any],
last_response: Optional[StripeResponse] = None,
requestor: "_APIRequestor",
api_mode: ApiMode,
) -> "V1BillingMeterErrorReportTriggeredEvent":
evt = super()._construct_from(
values=values,
last_response=last_response,
requestor=requestor,
api_mode=api_mode,
)
if hasattr(evt, "data"):
evt.data = V1BillingMeterErrorReportTriggeredEvent.V1BillingMeterErrorReportTriggeredEventData._construct_from(
values=evt.data,
last_response=last_response,
requestor=requestor,
api_mode=api_mode,
)
return evt

class RelatedObject(StripeObject):
id: str
"""
Expand Down
29 changes: 28 additions & 1 deletion stripe/events/_v1_billing_meter_no_meter_found_event.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
# File generated from our OpenAPI spec
from stripe._api_mode import ApiMode
from stripe._api_requestor import _APIRequestor
from stripe._stripe_object import StripeObject
from stripe._stripe_response import StripeResponse
from stripe.v2._event import Event
from typing import List
from typing import Any, Dict, List, Optional
from typing_extensions import Literal


Expand Down Expand Up @@ -86,3 +89,27 @@ class Request(StripeObject):
"""
Data for the v1.billing.meter.no_meter_found event
"""

@classmethod
def _construct_from(
cls,
*,
values: Dict[str, Any],
last_response: Optional[StripeResponse] = None,
requestor: "_APIRequestor",
api_mode: ApiMode,
) -> "V1BillingMeterNoMeterFoundEvent":
evt = super()._construct_from(
values=values,
last_response=last_response,
requestor=requestor,
api_mode=api_mode,
)
if hasattr(evt, "data"):
evt.data = V1BillingMeterNoMeterFoundEvent.V1BillingMeterNoMeterFoundEventData._construct_from(
values=evt.data,
last_response=last_response,
requestor=requestor,
api_mode=api_mode,
)
return evt
60 changes: 47 additions & 13 deletions tests/test_v2_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import stripe
from stripe import ThinEvent
from stripe.events._v1_billing_meter_error_report_triggered_event import (
V1BillingMeterErrorReportTriggeredEvent,
)
from tests.test_webhook import DUMMY_WEBHOOK_SECRET, generate_header

EventParser = Callable[[str], ThinEvent]
Expand All @@ -17,13 +20,13 @@ def v2_payload_no_data(self):
{
"id": "evt_234",
"object": "v2.core.event",
"type": "financial_account.balance.opened",
"type": "v1.billing.meter.error_report_triggered",
"livemode": True,
"created": "2022-02-15T00:27:45.330Z",
"related_object": {
"id": "fa_123",
"type": "financial_account",
"url": "/v2/financial_accounts/fa_123",
"id": "mtr_123",
"type": "billing.meter",
"url": "/v1/billing/meters/mtr_123",
"stripe_context": "acct_123",
},
"reason": {
Expand All @@ -39,19 +42,19 @@ def v2_payload_with_data(self):
{
"id": "evt_234",
"object": "v2.core.event",
"type": "financial_account.balance.opened",
"type": "v1.billing.meter.error_report_triggered",
"livemode": False,
"created": "2022-02-15T00:27:45.330Z",
"context": "acct_123",
"related_object": {
"id": "fa_123",
"type": "financial_account",
"url": "/v2/financial_accounts/fa_123",
"stripe_context": "acct_123",
"id": "mtr_123",
"type": "billing.meter",
"url": "/v1/billing/meters/mtr_123",
},
"data": {
"containing_compartment_id": "compid",
"id": "foo",
"type": "bufo",
"reason": {
"error_count": 1,
}
},
}
)
Expand Down Expand Up @@ -89,7 +92,7 @@ def test_parses_thin_event(
assert event.id == "evt_234"

assert event.related_object
assert event.related_object.id == "fa_123"
assert event.related_object.id == "mtr_123"

assert event.reason
assert event.reason.id == "foo"
Expand All @@ -110,3 +113,34 @@ def test_validates_signature(
stripe_client.parse_thin_event(
v2_payload_no_data, "bad header", DUMMY_WEBHOOK_SECRET
)

def test_v2_events_data_type(self, http_client_mock, v2_payload_with_data):
method = "get"
path = "/v2/core/events/evt_123"
http_client_mock.stub_request(
method,
path=path,
rbody=v2_payload_with_data,
rcode=200,
rheaders={},
)
client = stripe.StripeClient(
api_key="keyinfo_test_123",
http_client=http_client_mock.get_mock_http_client(),
)
event = client.v2.core.events.retrieve("evt_123")

http_client_mock.assert_requested(
method,
api_base=stripe.DEFAULT_API_BASE,
path=path,
api_key="keyinfo_test_123",
)
assert event.id is not None
assert isinstance(event, V1BillingMeterErrorReportTriggeredEvent)
assert event.data is not None
assert isinstance(
event.data,
V1BillingMeterErrorReportTriggeredEvent.V1BillingMeterErrorReportTriggeredEventData,
)
assert event.data.reason.error_count == 1
Loading