|
8 | 8 | import com.stripe.net.Webhook.Signature; |
9 | 9 | import java.net.PasswordAuthentication; |
10 | 10 | import java.net.Proxy; |
| 11 | +import lombok.Builder; |
11 | 12 | import lombok.Getter; |
12 | 13 |
|
13 | 14 | /** |
@@ -41,6 +42,62 @@ protected StripeResponseGetter getResponseGetter() { |
41 | 42 | return responseGetter; |
42 | 43 | } |
43 | 44 |
|
| 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 | + |
44 | 101 | /** |
45 | 102 | * Returns an StripeEvent instance using the provided JSON payload. Throws a JsonSyntaxException |
46 | 103 | * if the payload is not valid JSON, and a SignatureVerificationException if the signature |
@@ -1044,6 +1101,8 @@ public com.stripe.service.WebhookEndpointService webhookEndpoints() { |
1044 | 1101 | } |
1045 | 1102 |
|
1046 | 1103 | // The end of the section generated from our OpenAPI spec |
| 1104 | + @SuppressWarnings("ObjectToString") |
| 1105 | + @Builder(toBuilder = true) |
1047 | 1106 | static class ClientStripeResponseGetterOptions extends StripeResponseGetterOptions { |
1048 | 1107 | // When adding setting here keep them in sync with settings in RequestOptions and |
1049 | 1108 | // in the RequestOptions.merge method |
@@ -1423,4 +1482,9 @@ public StripeResponse rawRequest( |
1423 | 1482 | public StripeObject deserialize(String rawJson, ApiMode apiMode) throws StripeException { |
1424 | 1483 | return StripeObject.deserializeStripeObject(rawJson, this.getResponseGetter(), apiMode); |
1425 | 1484 | } |
| 1485 | + |
| 1486 | + public StripeEventNotificationHandler notificationHandler( |
| 1487 | + String webhookSecret, StripeEventNotificationHandler.FallbackCallback fallbackCallback) { |
| 1488 | + return new StripeEventNotificationHandler(webhookSecret, this, fallbackCallback); |
| 1489 | + } |
1426 | 1490 | } |
0 commit comments