Skip to content

Commit a2abd1b

Browse files
authored
generate / tweak EventNotificationHandler code for private preview (#2215)
1 parent 658fa2a commit a2abd1b

9 files changed

Lines changed: 3676 additions & 2 deletions

src/main/java/com/stripe/StripeClient.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.stripe.net.Webhook.Signature;
99
import java.net.PasswordAuthentication;
1010
import java.net.Proxy;
11+
import lombok.Builder;
1112
import lombok.Getter;
1213

1314
/**
@@ -41,6 +42,62 @@ protected StripeResponseGetter getResponseGetter() {
4142
return responseGetter;
4243
}
4344

45+
/** Gets the current StripeContext from the client's configuration. Used in unit testing. */
46+
protected String getContext() {
47+
// TODO(major): add getOptions to the StripeResponseGetter interface? that would simplify this
48+
if (!(responseGetter instanceof LiveStripeResponseGetter)) {
49+
return null;
50+
}
51+
52+
LiveStripeResponseGetter liveGetter = (LiveStripeResponseGetter) responseGetter;
53+
StripeResponseGetterOptions options = liveGetter.getOptions();
54+
55+
return options.getStripeContext();
56+
}
57+
58+
/**
59+
* Creates a new StripeClient with the same configuration as this client but with a custom
60+
* StripeContext. This method is useful for creating thread-safe clients with different contexts,
61+
* such as when processing webhooks in parallel where each webhook has its own context.
62+
*
63+
* <p>The new client will share the same configuration (API key, timeouts, proxy settings, etc.)
64+
* and HTTP client as this client, but will have the specified context. This allows for efficient
65+
* parallel processing without reinitializing HTTP connections.
66+
*
67+
* @param context the custom stripe_context to use for the new client
68+
* @return a new StripeClient with the custom context
69+
* @throws IllegalStateException if this client doesn't use a LiveStripeResponseGetter
70+
*/
71+
public StripeClient withStripeContext(StripeContext context) {
72+
// Convert StripeContext to String
73+
String contextString = (context == null) ? null : context.toString();
74+
75+
StripeResponseGetter responseGetter = this.getResponseGetter();
76+
77+
// We can only create a new client for LiveStripeResponseGetter because it's the only class with
78+
// `getOptions()`. If we add that method to the interface in a later major, we could remove this
79+
// check.
80+
if (!(responseGetter instanceof LiveStripeResponseGetter)) {
81+
throw new IllegalStateException(
82+
"Cannot create a client with custom context for non-Live response getters");
83+
}
84+
85+
LiveStripeResponseGetter liveGetter = (LiveStripeResponseGetter) responseGetter;
86+
87+
// Create a new LiveStripeResponseGetter with updated context, reusing the HTTP client
88+
LiveStripeResponseGetter newResponseGetter =
89+
liveGetter.withNewOptions(
90+
options -> {
91+
ClientStripeResponseGetterOptions existingOptions =
92+
(ClientStripeResponseGetterOptions) options;
93+
94+
return existingOptions.toBuilder().stripeContext(contextString).build();
95+
});
96+
97+
// Create and return a new StripeClient with the new response getter
98+
return new StripeClient(newResponseGetter);
99+
}
100+
44101
/**
45102
* Returns an StripeEvent instance using the provided JSON payload. Throws a JsonSyntaxException
46103
* if the payload is not valid JSON, and a SignatureVerificationException if the signature
@@ -1116,6 +1173,8 @@ public com.stripe.service.WebhookEndpointService webhookEndpoints() {
11161173
}
11171174

11181175
// The end of the section generated from our OpenAPI spec
1176+
@SuppressWarnings("ObjectToString")
1177+
@Builder(toBuilder = true)
11191178
static class ClientStripeResponseGetterOptions extends StripeResponseGetterOptions {
11201179
// When adding setting here keep them in sync with settings in RequestOptions and
11211180
// in the RequestOptions.merge method
@@ -1521,4 +1580,9 @@ public StripeResponse rawRequest(
15211580
public StripeObject deserialize(String rawJson, ApiMode apiMode) throws StripeException {
15221581
return StripeObject.deserializeStripeObject(rawJson, this.getResponseGetter(), apiMode);
15231582
}
1583+
1584+
public StripeEventNotificationHandler notificationHandler(
1585+
String webhookSecret, StripeEventNotificationHandler.FallbackCallback fallbackCallback) {
1586+
return new StripeEventNotificationHandler(webhookSecret, this, fallbackCallback);
1587+
}
15241588
}

src/main/java/com/stripe/StripeContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public StripeContext pop() {
6464
/**
6565
* Converts the context to a string by joining segments with '/'.
6666
*
67-
* @return string representation of the context segments joined by '/', `null` if there are no
68-
* segments (useful for clearing context)
67+
* @return string representation of the context segments joined by '/'. If there are no segments,
68+
* returns an empty string (useful for clearing context).
6969
*/
7070
@Override
7171
public String toString() {

0 commit comments

Comments
 (0)