-
Notifications
You must be signed in to change notification settings - Fork 593
⚠️ Add strongly typed EventNotifications #3168
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
13 commits
Select commit
Hold shift + click to select a range
d3286a5
WIP
xavdid 6fb3aff
wip
xavdid 4105517
basic test passes
xavdid f626379
add tests
xavdid 3677548
raise an error when fetching a relatedobject that doesn't exist
xavdid 4b47e75
Merge branch 'master' into DEVSDK-2662
xavdid-stripe edca391
update example
xavdid 0211486
Merge branch 'master' into DEVSDK-2662
xavdid e197dd6
update comments and test names
xavdid dd1b11f
update fetchRelatedObject on UnknownEventNotification
xavdid 146c552
Merge branch 'master' into DEVSDK-2662
xavdid-stripe 26e11b4
use stripeEntity instead of interface
xavdid f933121
update comment
xavdid 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
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,77 @@ | ||
| namespace Examples.V2 | ||
| { | ||
| #pragma warning disable SA1101 // Prefix local calls with this | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Stripe; | ||
| using Stripe.Events; | ||
| using Stripe.V2; | ||
|
|
||
| /// <summary> | ||
| /// Receive and process EventNotifications like the v1.billing.meter.error_report_triggered event. | ||
| /// | ||
| /// In this example, we: | ||
| /// - use ParseEventNotification to parse the received event notification webhook body | ||
| /// - call StripeClient.v2.core.events.retrieve to retrieve the full event object | ||
| /// - if it is a V1BillingMeterErrorReportTriggeredEvent event type, call fetchRelatedObject | ||
| /// to retrieve the Billing Meter object associated with the event. | ||
| /// </summary> | ||
| [Route("api/[controller]")] | ||
| [ApiController] | ||
| public class EventNotificationWebhookHandler : ControllerBase | ||
| { | ||
| private readonly StripeClient client; | ||
| private readonly string webhookSecret; | ||
|
|
||
| public EventNotificationWebhookHandler() | ||
|
Check warning on line 29 in src/Examples/V2/EventNotificationWebhookHandler.cs
|
||
| { | ||
| var apiKey = Environment.GetEnvironmentVariable("STRIPE_API_KEY"); | ||
| client = new StripeClient(apiKey); | ||
|
|
||
| webhookSecret = Environment.GetEnvironmentVariable("WEBHOOK_SECRET"); | ||
| } | ||
|
|
||
| [HttpPost] | ||
| public async Task<IActionResult> Index() | ||
| { | ||
| var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync(); | ||
| try | ||
| { | ||
| var eventNotification = client.ParseEventNotification(json, Request.Headers["Stripe-Signature"], webhookSecret); | ||
|
|
||
| // match on the type of the class to determine what event you have | ||
| if (eventNotification is V1BillingMeterErrorReportTriggeredEventNotification evt) | ||
| { | ||
| // the related object is correctly typed | ||
| var meter = evt.FetchRelatedObject(); | ||
| Console.WriteLine($"Default aggregation: {meter.DefaultAggregation}"); | ||
|
|
||
| // can also fetch the full event to get the event data | ||
| var eventObj = evt.FetchEvent(); | ||
| var eventData = eventObj.Data; | ||
| Console.WriteLine($"Err summary: {eventData.DeveloperMessageSummary}"); | ||
| } | ||
|
|
||
| // check other types the SDK knows about | ||
| // for event types that were released after this SDK was generated, check type | ||
| else if (eventNotification is UnknownEventNotification unknownEvt) | ||
| { | ||
| // fall back to checking type | ||
| if (unknownEvt.Type == "some.other.event") | ||
| { | ||
| // handle the event | ||
| } | ||
| } | ||
|
|
||
| return Ok(); | ||
| } | ||
| catch (StripeException e) | ||
| { | ||
| return BadRequest(e.Message); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
53 changes: 53 additions & 0 deletions
53
src/Stripe.net/Events/V1BillingMeterErrorReportTriggeredEventNotification.cs
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,53 @@ | ||
| // File generated from our OpenAPI spec | ||
| namespace Stripe.Events | ||
| { | ||
| using System.Threading.Tasks; | ||
| using Newtonsoft.Json; | ||
| using Stripe.V2; | ||
| #if NET6_0_OR_GREATER | ||
| using STJS = System.Text.Json.Serialization; | ||
| #endif | ||
|
|
||
| /// <summary> | ||
| /// Occurs when a Meter has invalid async usage events. | ||
| /// </summary> | ||
| public class V1BillingMeterErrorReportTriggeredEventNotification : V2.EventNotification | ||
| { | ||
| /// <summary> | ||
| /// Object containing the reference to API resource relevant to the event. | ||
| /// </summary> | ||
| [JsonProperty("related_object")] | ||
| #if NET6_0_OR_GREATER | ||
| [STJS.JsonPropertyName("related_object")] | ||
| #endif | ||
|
|
||
| public V2.EventNotificationRelatedObject RelatedObject { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Asynchronously retrieves the related object from the API. Make an API request on every | ||
| /// call. | ||
| /// </summary> | ||
| public Task<Billing.Meter> FetchRelatedObjectAsync() | ||
| { | ||
| return this.FetchRelatedObjectAsync<Billing.Meter>(this.RelatedObject); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the related object from the API. Make an API request on every call. | ||
| /// </summary> | ||
| public Billing.Meter FetchRelatedObject() | ||
| { | ||
| return this.FetchRelatedObject<Billing.Meter>(this.RelatedObject); | ||
| } | ||
|
|
||
| public V1BillingMeterErrorReportTriggeredEvent FetchEvent() | ||
| { | ||
| return this.FetchEvent<V1BillingMeterErrorReportTriggeredEvent>(); | ||
| } | ||
|
|
||
| public Task<V1BillingMeterErrorReportTriggeredEvent> FetchEventAsync() | ||
| { | ||
| return this.FetchEventAsync<V1BillingMeterErrorReportTriggeredEvent>(); | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/Stripe.net/Events/V1BillingMeterNoMeterFoundEventNotification.cs
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,22 @@ | ||
| // File generated from our OpenAPI spec | ||
| namespace Stripe.Events | ||
| { | ||
| using System.Threading.Tasks; | ||
| using Stripe.V2; | ||
|
|
||
| /// <summary> | ||
| /// Occurs when a Meter's id is missing or invalid in async usage events. | ||
| /// </summary> | ||
| public class V1BillingMeterNoMeterFoundEventNotification : V2.EventNotification | ||
| { | ||
| public V1BillingMeterNoMeterFoundEvent FetchEvent() | ||
| { | ||
| return this.FetchEvent<V1BillingMeterNoMeterFoundEvent>(); | ||
| } | ||
|
|
||
| public Task<V1BillingMeterNoMeterFoundEvent> FetchEventAsync() | ||
| { | ||
| return this.FetchEventAsync<V1BillingMeterNoMeterFoundEvent>(); | ||
| } | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
src/Stripe.net/Events/V2CoreEventDestinationPingEventNotification.cs
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,53 @@ | ||
| // File generated from our OpenAPI spec | ||
| namespace Stripe.Events | ||
| { | ||
| using System.Threading.Tasks; | ||
| using Newtonsoft.Json; | ||
| using Stripe.V2; | ||
| #if NET6_0_OR_GREATER | ||
| using STJS = System.Text.Json.Serialization; | ||
| #endif | ||
|
|
||
| /// <summary> | ||
| /// A ping event used to test the connection to an EventDestination. | ||
| /// </summary> | ||
| public class V2CoreEventDestinationPingEventNotification : V2.EventNotification | ||
| { | ||
| /// <summary> | ||
| /// Object containing the reference to API resource relevant to the event. | ||
| /// </summary> | ||
| [JsonProperty("related_object")] | ||
| #if NET6_0_OR_GREATER | ||
| [STJS.JsonPropertyName("related_object")] | ||
| #endif | ||
|
|
||
| public V2.EventNotificationRelatedObject RelatedObject { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Asynchronously retrieves the related object from the API. Make an API request on every | ||
| /// call. | ||
| /// </summary> | ||
| public Task<V2.EventDestination> FetchRelatedObjectAsync() | ||
| { | ||
| return this.FetchRelatedObjectAsync<V2.EventDestination>(this.RelatedObject); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the related object from the API. Make an API request on every call. | ||
| /// </summary> | ||
| public V2.EventDestination FetchRelatedObject() | ||
| { | ||
| return this.FetchRelatedObject<V2.EventDestination>(this.RelatedObject); | ||
| } | ||
|
|
||
| public V2CoreEventDestinationPingEvent FetchEvent() | ||
| { | ||
| return this.FetchEvent<V2CoreEventDestinationPingEvent>(); | ||
| } | ||
|
|
||
| public Task<V2CoreEventDestinationPingEvent> FetchEventAsync() | ||
| { | ||
| return this.FetchEventAsync<V2CoreEventDestinationPingEvent>(); | ||
| } | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/Stripe.net/Infrastructure/JsonConverters/STJV2EventNotificationConverter.cs
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,41 @@ | ||
| #if NET6_0_OR_GREATER | ||
| namespace Stripe.Infrastructure | ||
|
jar-stripe marked this conversation as resolved.
|
||
| { | ||
| using System; | ||
| using System.Reflection; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using static Stripe.Infrastructure.SerializablePropertyCache; | ||
|
|
||
| /// <summary> | ||
| /// Converts a <see cref="V2.Event"/> to JSON, including any fields | ||
| /// in derived classes. | ||
| /// </summary> | ||
| internal class STJV2EventNotificationConverter : STJDefaultConverter<V2.EventNotification> | ||
| { | ||
| /// <summary> | ||
| /// Reads the JSON representation of the object. | ||
| /// </summary> | ||
| /// <param name="reader">The <see cref="Utf8JsonReader"/> to read from.</param> | ||
| /// <param name="typeToConvert">Type of the object.</param> | ||
| /// <param name="options">The calling serializer's options.</param> | ||
| /// <returns>The object value.</returns> | ||
| public override V2.EventNotification Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| throw new NotSupportedException("STJV2EventConverter should only be used while serializing."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Determines whether this instance can convert the specified object type. | ||
| /// </summary> | ||
| /// <param name="objectType">Type of the object.</param> | ||
| /// <returns> | ||
| /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>. | ||
| /// </returns> | ||
| public override bool CanConvert(Type objectType) | ||
| { | ||
| return typeof(Stripe.V2.EventNotification).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo()); | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
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
Oops, something went wrong.
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.