-
Notifications
You must be signed in to change notification settings - Fork 595
⚠️ 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
Changes from 6 commits
d3286a5
6fb3aff
4105517
f626379
3677548
4b47e75
edca391
0211486
e197dd6
dd1b11f
146c552
26e11b4
f933121
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,15 +22,15 @@ | |
| [ApiController] | ||
| public class ThinEventWebhookHandler : ControllerBase | ||
| { | ||
| private readonly StripeClient _client; | ||
|
Check warning on line 25 in src/Examples/V2/ThinEventWebhookHandler.cs
|
||
| private readonly string _webhookSecret; | ||
|
Check warning on line 26 in src/Examples/V2/ThinEventWebhookHandler.cs
|
||
|
|
||
| public ThinEventWebhookHandler() | ||
|
Check warning on line 28 in src/Examples/V2/ThinEventWebhookHandler.cs
|
||
| { | ||
| var apiKey = Environment.GetEnvironmentVariable("STRIPE_API_KEY"); | ||
| _client = new StripeClient(apiKey); | ||
|
|
||
| _webhookSecret = Environment.GetEnvironmentVariable("WEBHOOK_SECRET"); | ||
|
Check warning on line 33 in src/Examples/V2/ThinEventWebhookHandler.cs
|
||
| } | ||
|
|
||
| [HttpPost] | ||
|
|
@@ -39,7 +39,7 @@ | |
| var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync(); | ||
| try | ||
| { | ||
| var thinEvent = _client.ParseThinEvent(json, Request.Headers["Stripe-Signature"], _webhookSecret); | ||
| var thinEvent = _client.ParseEventNotification(json, Request.Headers["Stripe-Signature"], _webhookSecret); | ||
|
|
||
| // Fetch the event data to understand the failure | ||
| var baseEvent = await _client.V2.Core.Events.GetAsync(thinEvent.Id); | ||
|
|
||
| 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>(); | ||
| } | ||
| } | ||
| } |
| 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>(); | ||
| } | ||
| } | ||
| } |
| 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>(); | ||
| } | ||
| } | ||
| } |
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| namespace Stripe.Infrastructure | ||
| { | ||
| using System; | ||
| using System.Reflection; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| /// <summary> | ||
| /// This converter deserializes Stripe thin events, which are polymorphic and discriminated by the value | ||
|
xavdid-stripe marked this conversation as resolved.
Outdated
|
||
| /// of a property named "type". | ||
| /// </summary> | ||
| internal class V2EventNotificationConverter : V2EventConverter | ||
| { | ||
| protected override Type GetConcreteType(string typeValue) | ||
| { | ||
| return StripeTypeRegistry.GetConcreteV2EventNotificationType(typeValue) ?? typeof(V2.UnknownEventNotification); | ||
| } | ||
|
|
||
| // mostly copied from the parent, but with different types | ||
| public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
| { | ||
| if (reader.TokenType == JsonToken.Null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var jsonObject = JObject.Load(reader); | ||
|
|
||
| // For V2 events, the "type" property is the discriminant that determines the specific | ||
| // class to deserialize into. | ||
| var typeValue = (string)jsonObject["type"]; | ||
|
|
||
| Type concreteType = this.GetConcreteType(typeValue); | ||
|
|
||
| using var subReader = jsonObject.CreateReader(); | ||
| var e = Activator.CreateInstance(concreteType); | ||
| serializer.Populate(subReader, e); | ||
| return e; | ||
| } | ||
|
|
||
| /// <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 objectType.GetTypeInfo() == typeof(V2.EventNotification); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.