-
Notifications
You must be signed in to change notification settings - Fork 458
⚠️ Add strongly typed EventNotifications #1538
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
af8b3c2
wip generating push payload events
xavdid-stripe 4a4527c
improve generation
xavdid-stripe 73d401f
generate related object method
xavdid-stripe 6ef23e0
Return a union from parse thin event & add UnknownThinEvent
xavdid-stripe 46f5a3c
fix unused import
xavdid-stripe d98d2ea
fix related_object generation and add big test
xavdid-stripe 8bdfff1
Merge branch 'master' into DEVSDK-2662
xavdid-stripe d09d7fa
Some name cleanup
xavdid-stripe 2c30ab4
rename thin_event
xavdid-stripe c3e7b59
small fixes
xavdid-stripe b6e2623
export UnknownEventNotification
xavdid-stripe d9871c7
fix reason parsing
xavdid-stripe 76eadd6
rename thin_event
xavdid-stripe d3753db
Merge branch 'master' into DEVSDK-2662
xavdid-stripe 86a4594
fix example & re-export event notifications
xavdid-stripe ca9d5d7
move some imports
xavdid-stripe 9553437
Merge branch 'master' into DEVSDK-2662
xavdid-stripe f590b52
swap event fetch_related_object to stripe-context
xavdid-stripe a5ea083
Merge branch 'master' into DEVSDK-2662
xavdid-stripe ecebd56
update docstring
xavdid-stripe bdbcf13
update comment
xavdid-stripe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
event_notification_webhook_handler.py - receive and process thin events like the | ||
v1.billing.meter.error_report_triggered event. | ||
|
||
In this example, we: | ||
- create a StripeClient called client | ||
- use client.parse_event_notification() to parse the received notification webhook body | ||
- if its type is "v1.billing.meter.error_report_triggered": | ||
- call event_notification.fetch_event() to retrieve the full event object | ||
- call event_notification.fetch_related_object() to retrieve the Meter that failed | ||
- log info about the failure | ||
""" | ||
|
||
import os | ||
from stripe import StripeClient | ||
|
||
from flask import Flask, request, jsonify | ||
|
||
app = Flask(__name__) | ||
api_key = os.environ.get("STRIPE_API_KEY", "") | ||
webhook_secret = os.environ.get("WEBHOOK_SECRET", "") | ||
|
||
client = StripeClient(api_key) | ||
|
||
|
||
@app.route("/webhook", methods=["POST"]) | ||
def webhook(): | ||
webhook_body = request.data | ||
sig_header = request.headers.get("Stripe-Signature") | ||
|
||
try: | ||
event_notification = client.parse_event_notification( | ||
webhook_body, sig_header, webhook_secret | ||
) | ||
|
||
# Fetch the event data to understand the failure | ||
if ( | ||
event_notification.type | ||
== "v1.billing.meter.error_report_triggered" | ||
): | ||
meter = event_notification.fetch_related_object() | ||
event = event_notification.fetch_event() | ||
print( | ||
f"Err! Meter {meter.id} had a problem (more info: {event.data.developer_message_summary})" | ||
) | ||
# Record the failures and alert your team | ||
# Add your logic here | ||
|
||
return jsonify(success=True), 200 | ||
except Exception as e: | ||
return jsonify(error=str(e)), 400 | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(port=4242) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
# File generated from our OpenAPI spec | ||
from stripe.events._event_classes import ( | ||
ALL_EVENT_NOTIFICATIONS as ALL_EVENT_NOTIFICATIONS, | ||
) | ||
from stripe.events._v1_billing_meter_error_report_triggered_event import ( | ||
V1BillingMeterErrorReportTriggeredEvent as V1BillingMeterErrorReportTriggeredEvent, | ||
V1BillingMeterErrorReportTriggeredEventNotification as V1BillingMeterErrorReportTriggeredEventNotification, | ||
) | ||
from stripe.events._v1_billing_meter_no_meter_found_event import ( | ||
V1BillingMeterNoMeterFoundEvent as V1BillingMeterNoMeterFoundEvent, | ||
V1BillingMeterNoMeterFoundEventNotification as V1BillingMeterNoMeterFoundEventNotification, | ||
) | ||
from stripe.events._v2_core_event_destination_ping_event import ( | ||
V2CoreEventDestinationPingEvent as V2CoreEventDestinationPingEvent, | ||
V2CoreEventDestinationPingEventNotification as V2CoreEventDestinationPingEventNotification, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
# File generated from our OpenAPI spec | ||
from typing import Union | ||
from stripe.events._v1_billing_meter_error_report_triggered_event import ( | ||
V1BillingMeterErrorReportTriggeredEvent, | ||
V1BillingMeterErrorReportTriggeredEventNotification, | ||
) | ||
from stripe.events._v1_billing_meter_no_meter_found_event import ( | ||
V1BillingMeterNoMeterFoundEvent, | ||
V1BillingMeterNoMeterFoundEventNotification, | ||
) | ||
from stripe.events._v2_core_event_destination_ping_event import ( | ||
V2CoreEventDestinationPingEvent, | ||
V2CoreEventDestinationPingEventNotification, | ||
) | ||
|
||
|
||
THIN_EVENT_CLASSES = { | ||
V2_EVENT_CLASS_LOOKUP = { | ||
V1BillingMeterErrorReportTriggeredEvent.LOOKUP_TYPE: V1BillingMeterErrorReportTriggeredEvent, | ||
V1BillingMeterNoMeterFoundEvent.LOOKUP_TYPE: V1BillingMeterNoMeterFoundEvent, | ||
V2CoreEventDestinationPingEvent.LOOKUP_TYPE: V2CoreEventDestinationPingEvent, | ||
} | ||
|
||
V2_EVENT_NOTIFICATION_CLASS_LOOKUP = { | ||
V1BillingMeterErrorReportTriggeredEventNotification.LOOKUP_TYPE: V1BillingMeterErrorReportTriggeredEventNotification, | ||
V1BillingMeterNoMeterFoundEventNotification.LOOKUP_TYPE: V1BillingMeterNoMeterFoundEventNotification, | ||
V2CoreEventDestinationPingEventNotification.LOOKUP_TYPE: V2CoreEventDestinationPingEventNotification, | ||
} | ||
|
||
ALL_EVENT_NOTIFICATIONS = Union[ | ||
V1BillingMeterErrorReportTriggeredEventNotification, | ||
V1BillingMeterNoMeterFoundEventNotification, | ||
V2CoreEventDestinationPingEventNotification, | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.