-
Notifications
You must be signed in to change notification settings - Fork 390
⚠️ Add strongly typed EventNotifications #2036
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
+481
−123
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
dd5d052
generate event deliveries
xavdid-stripe b046ced
tests almost working
xavdid-stripe 820681b
Fix tests
xavdid-stripe f8d4439
rename thinEvent
xavdid-stripe 62d6a63
fix fetchEvent name
xavdid-stripe f6058c9
fix typo
xavdid-stripe c29f49c
fix naming and object
xavdid-stripe f92437b
Merge branch 'master' into DEVSDK-2662
xavdid-stripe 892cb2e
update example
xavdid-stripe 12104b1
update comment
xavdid-stripe 33de568
Merge branch 'master' into DEVSDK-2662
xavdid-stripe 3bb2f2b
Merge branch 'master' into DEVSDK-2662
xavdid-stripe fe9cbd0
update comment
xavdid-stripe 7a5dcec
fix doc
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
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
27 changes: 27 additions & 0 deletions
27
src/main/java/com/stripe/events/V1BillingMeterErrorReportTriggeredEventNotification.java
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,27 @@ | ||
| // File generated from our OpenAPI spec | ||
| package com.stripe.events; | ||
|
|
||
| import com.google.gson.annotations.SerializedName; | ||
| import com.stripe.exception.StripeException; | ||
| import com.stripe.model.billing.Meter; | ||
| import com.stripe.model.v2.Event.RelatedObject; | ||
| import com.stripe.model.v2.EventNotification; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public final class V1BillingMeterErrorReportTriggeredEventNotification extends EventNotification { | ||
| @SerializedName("related_object") | ||
|
|
||
| /** Object containing the reference to API resource relevant to the event. */ | ||
| RelatedObject relatedObject; | ||
|
|
||
| /** Retrieves the related object from the API. Make an API request on every call. */ | ||
| public Meter fetchRelatedObject() throws StripeException { | ||
| return (Meter) super.fetchRelatedObject(this.relatedObject); | ||
| } | ||
| /** Retrieve the corresponding full event from the Stripe API. */ | ||
| @Override | ||
| public V1BillingMeterErrorReportTriggeredEvent fetchEvent() throws StripeException { | ||
| return (V1BillingMeterErrorReportTriggeredEvent) super.fetchEvent(); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/stripe/events/V1BillingMeterNoMeterFoundEventNotification.java
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,13 @@ | ||
| // File generated from our OpenAPI spec | ||
| package com.stripe.events; | ||
|
|
||
| import com.stripe.exception.StripeException; | ||
| import com.stripe.model.v2.EventNotification; | ||
|
|
||
| public final class V1BillingMeterNoMeterFoundEventNotification extends EventNotification { | ||
| /** Retrieve the corresponding full event from the Stripe API. */ | ||
| @Override | ||
| public V1BillingMeterNoMeterFoundEvent fetchEvent() throws StripeException { | ||
| return (V1BillingMeterNoMeterFoundEvent) super.fetchEvent(); | ||
| } | ||
| } |
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
27 changes: 27 additions & 0 deletions
27
src/main/java/com/stripe/events/V2CoreEventDestinationPingEventNotification.java
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,27 @@ | ||
| // File generated from our OpenAPI spec | ||
| package com.stripe.events; | ||
|
|
||
| import com.google.gson.annotations.SerializedName; | ||
| import com.stripe.exception.StripeException; | ||
| import com.stripe.model.v2.Event.RelatedObject; | ||
| import com.stripe.model.v2.EventDestination; | ||
| import com.stripe.model.v2.EventNotification; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public final class V2CoreEventDestinationPingEventNotification extends EventNotification { | ||
| @SerializedName("related_object") | ||
|
|
||
| /** Object containing the reference to API resource relevant to the event. */ | ||
| RelatedObject relatedObject; | ||
|
|
||
| /** Retrieves the related object from the API. Make an API request on every call. */ | ||
| public EventDestination fetchRelatedObject() throws StripeException { | ||
| return (EventDestination) super.fetchRelatedObject(this.relatedObject); | ||
| } | ||
| /** Retrieve the corresponding full event from the Stripe API. */ | ||
| @Override | ||
| public V2CoreEventDestinationPingEvent fetchEvent() throws StripeException { | ||
| return (V2CoreEventDestinationPingEvent) super.fetchEvent(); | ||
| } | ||
| } |
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 was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/main/java/com/stripe/model/ThinEventRelatedObject.java
This file was deleted.
Oops, something went wrong.
134 changes: 134 additions & 0 deletions
134
src/main/java/com/stripe/model/v2/EventNotification.java
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,134 @@ | ||
| package com.stripe.model.v2; | ||
|
|
||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.annotations.SerializedName; | ||
| import com.stripe.StripeClient; | ||
| import com.stripe.exception.StripeException; | ||
| import com.stripe.model.StripeObject; | ||
| import com.stripe.model.v2.Event.RelatedObject; | ||
| import com.stripe.net.ApiMode; | ||
| import com.stripe.net.ApiResource; | ||
| import com.stripe.net.ApiResource.RequestMethod; | ||
| import com.stripe.net.RawRequestOptions; | ||
| import com.stripe.net.StripeResponse; | ||
| import java.time.Instant; | ||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
|
|
||
| /** | ||
| * `EventNotification` represents the common properties for json that's delivered from an Event | ||
| * Destination. A concrete child of `EventNotification` will be returned from | ||
| * `StripeClient.parseEventNotificaion()`. You will likely want to cast that object to a more | ||
| * specific child of `EventNotification`, like `PushedV1BillingMeterErrorReportTriggeredEvent` | ||
| */ | ||
| @Getter | ||
| public abstract class EventNotification { | ||
| /** | ||
| * For more details about Request, please refer to the <a href="https://docs.stripe.com/api">API | ||
| * Reference.</a> | ||
| */ | ||
| @Getter | ||
| public static class Request { | ||
| /** ID of the API request that caused the event. */ | ||
| @SerializedName("id") | ||
| String id; | ||
|
|
||
| /** The idempotency key transmitted during the request. */ | ||
| @SerializedName("idempotency_key") | ||
| String idempotencyKey; | ||
| } | ||
|
|
||
| @Getter | ||
| public static class Reason { | ||
| /** Information on the API request that instigated the event. */ | ||
| @SerializedName("request") | ||
| Request request; | ||
|
|
||
| /** | ||
| * Event reason type. | ||
| * | ||
| * <p>Equal to {@code request}. | ||
| */ | ||
| @SerializedName("type") | ||
| String type; | ||
| } | ||
|
|
||
| /** Unique identifier for the event. */ | ||
| @SerializedName("id") | ||
| public String id; | ||
|
|
||
| /** The type of the event. */ | ||
| @SerializedName("type") | ||
| public String type; | ||
|
|
||
| /** Time at which the object was created. */ | ||
| @SerializedName("created") | ||
| public Instant created; | ||
|
|
||
| /** Livemode indicates if the event is from a production(true) or test(false) account. */ | ||
| @SerializedName("livemode") | ||
| public Boolean livemode; | ||
|
|
||
| /** [Optional] Authentication context needed to fetch the event or related object. */ | ||
| @SerializedName("context") | ||
| public String context; | ||
|
|
||
| /** [Optional] Reason for the event. */ | ||
| @SerializedName("reason") | ||
| public Reason reason; | ||
|
|
||
| @Getter(AccessLevel.NONE) | ||
| protected transient StripeClient client; | ||
|
|
||
| /** | ||
| * Helper for constructing an Event Notification. Doesn't perform signature validation, so you | ||
| * should use {@link com.stripe.StripeClient#parseEventNotification} instead for initial handling. | ||
| * This is useful in unit tests and working with EventNotifications that you've already validated | ||
| * the authenticity of. | ||
| */ | ||
| public static EventNotification fromJson(String payload, StripeClient client) { | ||
xavdid-stripe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // don't love the double json parse here, but I don't think we can avoid it? | ||
| JsonObject jsonObject = ApiResource.GSON.fromJson(payload, JsonObject.class).getAsJsonObject(); | ||
|
|
||
| Class<? extends EventNotification> cls = | ||
| EventNotificationClassLookup.eventClassLookup.get(jsonObject.get("type").getAsString()); | ||
| if (cls == null) { | ||
| cls = UnknownEventNotification.class; | ||
| } | ||
|
|
||
| EventNotification e = ApiResource.GSON.fromJson(payload, cls); | ||
| e.client = client; | ||
| return e; | ||
| } | ||
|
|
||
| private RawRequestOptions getRequestOptions() { | ||
| if (context == null) { | ||
| return null; | ||
| } | ||
| return new RawRequestOptions.RawRequestOptionsBuilder().setStripeContext(context).build(); | ||
| } | ||
|
|
||
| /* retrieves the full payload for an event. Protected because individual push classes use it, but type it correctly */ | ||
| protected Event fetchEvent() throws StripeException { | ||
| StripeResponse response = | ||
| client.rawRequest( | ||
| RequestMethod.GET, String.format("/v2/core/events/%s", id), null, getRequestOptions()); | ||
|
|
||
| return (Event) client.deserialize(response.body(), ApiMode.V2); | ||
| } | ||
|
|
||
| /** Retrieves the object associated with the event. */ | ||
| protected StripeObject fetchRelatedObject(RelatedObject relatedObject) throws StripeException { | ||
| if (relatedObject == null) { | ||
| // used by UnknownEventNotification, so be a little defensive | ||
| return null; | ||
| } | ||
|
|
||
| String relativeUrl = relatedObject.getUrl(); | ||
|
|
||
| StripeResponse response = | ||
xavdid-stripe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| client.rawRequest(RequestMethod.GET, relativeUrl, null, getRequestOptions()); | ||
|
|
||
| return client.deserialize(response.body(), ApiMode.getMode(relativeUrl)); | ||
| } | ||
| } | ||
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.