• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-java / #16493

03 Oct 2024 07:15PM UTC coverage: 12.942% (+0.08%) from 12.864%
#16493

push

github

web-flow
Merge Stripe-java v27.0.0 to beta branch (#1888)

409 of 1651 new or added lines in 88 files covered. (24.77%)

31 existing lines in 7 files now uncovered.

18773 of 145050 relevant lines covered (12.94%)

0.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.11
/src/main/java/com/stripe/net/RequestOptions.java
1
package com.stripe.net;
2

3
import com.stripe.Stripe;
4
import java.net.PasswordAuthentication;
5
import java.net.Proxy;
6
import java.util.Map;
7
import lombok.EqualsAndHashCode;
8

9
@EqualsAndHashCode(callSuper = false)
10
public class RequestOptions {
11
  // When adding setting here keep them in sync with settings in StripeClientOptions and
12
  // in the RequestOptions.merge method
13
  private final Authenticator authenticator;
14
  private final String clientId;
15
  private final String stripeContext;
16
  private final String idempotencyKey;
17
  private final String stripeAccount;
18

19
  private final String baseUrl;
20
  /** Uses the globally set version by defaeult, unless an override is provided. */
21
  private final String stripeVersion = Stripe.stripeVersion;
1✔
22

23
  /**
24
   * Stripe version override when made on behalf of others. This can be used when the returned
25
   * response will not be deserialized into the current classes pinned to {@link Stripe#VERSION}.
26
   */
27
  private final String stripeVersionOverride;
28

29
  private final Integer connectTimeout;
30
  private final Integer readTimeout;
31
  private final Integer maxNetworkRetries;
32
  private final Proxy connectionProxy;
33
  private final PasswordAuthentication proxyCredential;
34

35
  public static RequestOptions getDefault() {
36
    return new RequestOptions(
1✔
37
        null, null, null, null, null, null, null, null, null, null, null, null);
38
  }
39

40
  protected RequestOptions(
41
      Authenticator authenticator,
42
      String clientId,
43
      String idempotencyKey,
44
      String stripeContext,
45
      String stripeAccount,
46
      String stripeVersionOverride,
47
      String baseUrl,
48
      Integer connectTimeout,
49
      Integer readTimeout,
50
      Integer maxNetworkRetries,
51
      Proxy connectionProxy,
52
      PasswordAuthentication proxyCredential) {
1✔
53
    this.authenticator = authenticator;
1✔
54
    this.clientId = clientId;
1✔
55
    this.idempotencyKey = idempotencyKey;
1✔
56
    this.stripeContext = stripeContext;
1✔
57
    this.stripeAccount = stripeAccount;
1✔
58
    this.stripeVersionOverride = stripeVersionOverride;
1✔
59
    this.baseUrl = baseUrl;
1✔
60
    this.connectTimeout = connectTimeout;
1✔
61
    this.readTimeout = readTimeout;
1✔
62
    this.maxNetworkRetries = maxNetworkRetries;
1✔
63
    this.connectionProxy = connectionProxy;
1✔
64
    this.proxyCredential = proxyCredential;
1✔
65
  }
1✔
66

67
  public Authenticator getAuthenticator() {
68
    return this.authenticator;
1✔
69
  }
70

71
  public String getApiKey() {
72
    if (authenticator instanceof BearerTokenAuthenticator) {
1✔
73
      return ((BearerTokenAuthenticator) authenticator).getApiKey();
1✔
74
    }
75

76
    return null;
1✔
77
  }
78

79
  public String getClientId() {
80
    return clientId;
1✔
81
  }
82

83
  public String getStripeContext() {
84
    return stripeContext;
1✔
85
  }
86

87
  public String getIdempotencyKey() {
88
    return idempotencyKey;
1✔
89
  }
90

91
  public String getStripeAccount() {
92
    return stripeAccount;
1✔
93
  }
94

95
  public String getStripeVersion() {
96
    return stripeVersion;
1✔
97
  }
98

99
  public static String unsafeGetStripeVersionOverride(RequestOptions options) {
100
    return options.stripeVersionOverride;
1✔
101
  }
102

103
  public Integer getReadTimeout() {
104
    return readTimeout;
1✔
105
  }
106

107
  public Integer getConnectTimeout() {
108
    return connectTimeout;
1✔
109
  }
110

111
  public Integer getMaxNetworkRetries() {
112
    return maxNetworkRetries;
1✔
113
  }
114

115
  public Proxy getConnectionProxy() {
116
    return connectionProxy;
1✔
117
  }
118

119
  public PasswordAuthentication getProxyCredential() {
120
    return proxyCredential;
1✔
121
  }
122

123
  public String getBaseUrl() {
124
    return baseUrl;
1✔
125
  }
126

127
  public static RequestOptionsBuilder builder() {
128
    return new RequestOptionsBuilder();
1✔
129
  }
130

131
  /**
132
   * Convert request options to builder, retaining invariant values for the integration.
133
   *
134
   * @deprecated prefer {@link toBuilderFullCopy()} which fully copies the request options instead
135
   *     of a subset of its options.
136
   * @return option builder.
137
   */
138
  @Deprecated
139
  public RequestOptionsBuilder toBuilder() {
140
    return new RequestOptionsBuilder()
1✔
141
        .setAuthenticator(this.authenticator)
1✔
142
        .setStripeAccount(this.stripeAccount);
1✔
143
  }
144

145
  /**
146
   * Convert request options to builder, copying all options.
147
   *
148
   * @return option builder.
149
   */
150
  public RequestOptionsBuilder toBuilderFullCopy() {
151
    return RequestOptionsBuilder.unsafeSetStripeVersionOverride(
1✔
152
        new RequestOptionsBuilder()
153
            .setAuthenticator(this.authenticator)
1✔
154
            .setBaseUrl(this.baseUrl)
1✔
155
            .setClientId(this.clientId)
1✔
156
            .setIdempotencyKey(this.idempotencyKey)
1✔
157
            .setStripeAccount(this.stripeAccount)
1✔
158
            .setConnectTimeout(this.connectTimeout)
1✔
159
            .setReadTimeout(this.readTimeout)
1✔
160
            .setMaxNetworkRetries(this.maxNetworkRetries)
1✔
161
            .setConnectionProxy(this.connectionProxy)
1✔
162
            .setProxyCredential(this.proxyCredential),
1✔
163
        stripeVersionOverride);
164
  }
165

166
  public static class RequestOptionsBuilder {
167
    protected Authenticator authenticator;
168
    protected String clientId;
169
    protected String idempotencyKey;
170
    protected String stripeContext;
171
    protected String stripeAccount;
172
    protected String stripeVersionOverride;
173
    protected Integer connectTimeout;
174
    protected Integer readTimeout;
175
    protected Integer maxNetworkRetries;
176
    protected Proxy connectionProxy;
177
    protected PasswordAuthentication proxyCredential;
178
    protected String baseUrl;
179

180
    /**
181
     * Constructs a request options builder with the global parameters (API key and client ID) as
182
     * default values.
183
     */
184
    public RequestOptionsBuilder() {}
1✔
185

186
    public Authenticator getAuthenticator() {
NEW
187
      return this.authenticator;
×
188
    }
189

190
    public RequestOptionsBuilder setAuthenticator(Authenticator authenticator) {
191
      this.authenticator = authenticator;
1✔
192
      return this;
1✔
193
    }
194

195
    public String getApiKey() {
NEW
196
      if (authenticator instanceof BearerTokenAuthenticator) {
×
NEW
197
        return ((BearerTokenAuthenticator) authenticator).getApiKey();
×
198
      }
199

NEW
200
      return null;
×
201
    }
202

203
    public RequestOptionsBuilder setApiKey(String apiKey) {
204
      if (apiKey == null) {
1✔
205
        this.authenticator = null;
1✔
206
      } else {
207
        this.authenticator = new BearerTokenAuthenticator(normalizeApiKey(apiKey));
1✔
208
      }
209
      return this;
1✔
210
    }
211

212
    public RequestOptionsBuilder clearApiKey() {
NEW
213
      this.authenticator = null;
×
214
      return this;
×
215
    }
216

217
    public String getClientId() {
218
      return clientId;
×
219
    }
220

221
    public RequestOptionsBuilder setClientId(String clientId) {
222
      this.clientId = normalizeClientId(clientId);
1✔
223
      return this;
1✔
224
    }
225

226
    public RequestOptionsBuilder clearClientId() {
227
      this.clientId = null;
×
228
      return this;
×
229
    }
230

231
    public String getStripeContext() {
NEW
232
      return stripeContext;
×
233
    }
234

235
    public RequestOptionsBuilder setStripeContext(String context) {
236
      this.stripeContext = context;
1✔
237
      return this;
1✔
238
    }
239

240
    public RequestOptionsBuilder clearStripeContext() {
NEW
241
      this.stripeContext = null;
×
NEW
242
      return this;
×
243
    }
244

245
    public RequestOptionsBuilder setIdempotencyKey(String idempotencyKey) {
246
      this.idempotencyKey = idempotencyKey;
1✔
247
      return this;
1✔
248
    }
249

250
    public Integer getConnectTimeout() {
251
      return connectTimeout;
×
252
    }
253

254
    /**
255
     * Sets the timeout value that will be used for making new connections to the Stripe API (in
256
     * milliseconds).
257
     *
258
     * @param timeout timeout value in milliseconds
259
     */
260
    public RequestOptionsBuilder setConnectTimeout(Integer timeout) {
261
      this.connectTimeout = timeout;
1✔
262
      return this;
1✔
263
    }
264

265
    public Integer getReadTimeout() {
266
      return readTimeout;
×
267
    }
268

269
    /**
270
     * Sets the timeout value that will be used when reading data from an established connection to
271
     * the Stripe API (in milliseconds).
272
     *
273
     * <p>Note that this value should be set conservatively because some API requests can take time
274
     * and a short timeout increases the likelihood of causing a problem in the backend.
275
     *
276
     * @param timeout timeout value in milliseconds
277
     */
278
    public RequestOptionsBuilder setReadTimeout(Integer timeout) {
279
      this.readTimeout = timeout;
1✔
280
      return this;
1✔
281
    }
282

283
    public Integer getMaxNetworkRetries() {
284
      return maxNetworkRetries;
×
285
    }
286

287
    /**
288
     * Sets the maximum number of times the request will be retried in the event of a failure.
289
     *
290
     * @param maxNetworkRetries the number of times to retry the request
291
     */
292
    public RequestOptionsBuilder setMaxNetworkRetries(Integer maxNetworkRetries) {
293
      this.maxNetworkRetries = maxNetworkRetries;
1✔
294
      return this;
1✔
295
    }
296

297
    public Proxy getConnectionProxy() {
298
      return connectionProxy;
×
299
    }
300

301
    public RequestOptionsBuilder setConnectionProxy(Proxy connectionProxy) {
302
      this.connectionProxy = connectionProxy;
1✔
303
      return this;
1✔
304
    }
305

306
    public PasswordAuthentication getProxyCredential() {
307
      return proxyCredential;
×
308
    }
309

310
    public RequestOptionsBuilder setProxyCredential(PasswordAuthentication proxyCredential) {
311
      this.proxyCredential = proxyCredential;
1✔
312
      return this;
1✔
313
    }
314

315
    public RequestOptionsBuilder clearIdempotencyKey() {
316
      this.idempotencyKey = null;
×
317
      return this;
×
318
    }
319

320
    public String getIdempotencyKey() {
321
      return this.idempotencyKey;
×
322
    }
323

324
    public String getStripeAccount() {
325
      return this.stripeAccount;
×
326
    }
327

328
    public RequestOptionsBuilder setStripeAccount(String stripeAccount) {
329
      this.stripeAccount = stripeAccount;
1✔
330
      return this;
1✔
331
    }
332

333
    public RequestOptionsBuilder clearStripeAccount() {
334
      return setStripeAccount(null);
×
335
    }
336

337
    /**
338
     * This is for internal use only. See {@link com.stripe.model.EphemeralKey#create(Map,
339
     * RequestOptions)}. Setting this yourself will result in a version mismatch between your
340
     * request and this library's types, which can result in missing data and deserialization
341
     * errors.
342
     *
343
     * <p>Static, so that it doesn't appear in IDE auto-completion alongside the other setters.
344
     *
345
     * @return option builder
346
     */
347
    public static RequestOptionsBuilder unsafeSetStripeVersionOverride(
348
        RequestOptionsBuilder builder, String stripeVersionOverride) {
349
      builder.stripeVersionOverride = normalizeStripeVersion(stripeVersionOverride);
1✔
350
      return builder;
1✔
351
    }
352

353
    public RequestOptionsBuilder setBaseUrl(final String baseUrl) {
354
      this.baseUrl = baseUrl;
1✔
355
      return this;
1✔
356
    }
357

358
    /** Constructs a {@link RequestOptions} with the specified values. */
359
    public RequestOptions build() {
360
      return new RequestOptions(
1✔
361
          this.authenticator,
362
          normalizeClientId(this.clientId),
1✔
363
          normalizeIdempotencyKey(this.idempotencyKey),
1✔
364
          stripeContext,
365
          normalizeStripeAccount(this.stripeAccount),
1✔
366
          normalizeStripeVersion(this.stripeVersionOverride),
1✔
367
          normalizeBaseUrl(this.baseUrl),
1✔
368
          connectTimeout,
369
          readTimeout,
370
          maxNetworkRetries,
371
          connectionProxy,
372
          proxyCredential);
373
    }
374
  }
375

376
  protected static String normalizeApiKey(String apiKey) {
377
    // null apiKeys are considered "valid"
378
    if (apiKey == null) {
1✔
UNCOV
379
      return null;
×
380
    }
381
    return apiKey.trim();
1✔
382
  }
383

384
  protected static String normalizeClientId(String clientId) {
385
    // null client_ids are considered "valid"
386
    if (clientId == null) {
1✔
387
      return null;
1✔
388
    }
389
    String normalized = clientId.trim();
1✔
390
    if (normalized.isEmpty()) {
1✔
391
      throw new InvalidRequestOptionsException("Empty client_id specified!");
×
392
    }
393
    return normalized;
1✔
394
  }
395

396
  protected static String normalizeStripeVersion(String stripeVersion) {
397
    // null stripeVersions are considered "valid" and use Stripe.stripeVersion
398
    if (stripeVersion == null) {
1✔
399
      return null;
1✔
400
    }
401
    String normalized = stripeVersion.trim();
1✔
402
    if (normalized.isEmpty()) {
1✔
403
      throw new InvalidRequestOptionsException("Empty Stripe version specified!");
×
404
    }
405
    return normalized;
1✔
406
  }
407

408
  protected static String normalizeBaseUrl(String baseUrl) {
409
    // null baseUrl is valid, and will fall back to e.g. Stripe.apiBase or Stripe.connectBase
410
    // (depending on the method)
411
    if (baseUrl == null) {
1✔
412
      return null;
1✔
413
    }
414
    String normalized = baseUrl.trim();
1✔
415
    if (normalized.isEmpty()) {
1✔
416
      throw new InvalidRequestOptionsException("Empty baseUrl specified!");
×
417
    }
418
    return normalized;
1✔
419
  }
420

421
  protected static String normalizeIdempotencyKey(String idempotencyKey) {
422
    if (idempotencyKey == null) {
1✔
423
      return null;
1✔
424
    }
425
    String normalized = idempotencyKey.trim();
1✔
426
    if (normalized.isEmpty()) {
1✔
427
      throw new InvalidRequestOptionsException("Empty Idempotency Key Specified!");
×
428
    }
429
    if (normalized.length() > 255) {
1✔
430
      throw new InvalidRequestOptionsException(
×
431
          String.format(
×
432
              "Idempotency Key length was %d, which is larger than the 255 character " + "maximum!",
433
              normalized.length()));
×
434
    }
435
    return normalized;
1✔
436
  }
437

438
  protected static String normalizeStripeContext(String stripContext) {
439
    if (stripContext == null) {
1✔
440
      return null;
1✔
441
    }
NEW
442
    String normalized = stripContext.trim();
×
NEW
443
    if (normalized.isEmpty()) {
×
NEW
444
      throw new InvalidRequestOptionsException("Empty stripe context specified!");
×
445
    }
NEW
446
    return normalized;
×
447
  }
448

449
  protected static String normalizeStripeAccount(String stripeAccount) {
450
    if (stripeAccount == null) {
1✔
451
      return null;
1✔
452
    }
453
    String normalized = stripeAccount.trim();
1✔
454
    if (normalized.isEmpty()) {
1✔
455
      throw new InvalidRequestOptionsException("Empty stripe account specified!");
×
456
    }
457
    return normalized;
1✔
458
  }
459

460
  static RequestOptions merge(StripeResponseGetterOptions clientOptions, RequestOptions options) {
461
    if (options == null) {
1✔
462
      return new RequestOptions(
1✔
463
          clientOptions.getAuthenticator(), // authenticator
1✔
464
          clientOptions.getClientId(), // clientId
1✔
465
          null, // idempotencyKey
466
          clientOptions.getStripeContext(), // stripeContext
1✔
467
          null, // stripeAccount
468
          null, // stripeVersionOverride
469
          null, // baseUrl
470
          clientOptions.getConnectTimeout(), // connectTimeout
1✔
471
          clientOptions.getReadTimeout(), // readTimeout
1✔
472
          clientOptions.getMaxNetworkRetries(), // maxNetworkRetries
1✔
473
          clientOptions.getConnectionProxy(), // connectionProxy
1✔
474
          clientOptions.getProxyCredential() // proxyCredential
1✔
475
          );
476
    }
477
    return new RequestOptions(
1✔
478
        options.getAuthenticator() != null
1✔
479
            ? options.getAuthenticator()
1✔
480
            : clientOptions.getAuthenticator(),
1✔
481
        options.getClientId() != null ? options.getClientId() : clientOptions.getClientId(),
1✔
482
        options.getIdempotencyKey(),
1✔
483
        options.getStripeContext() != null
1✔
484
            ? options.getStripeContext()
1✔
485
            : clientOptions.getStripeContext(),
1✔
486
        options.getStripeAccount(),
1✔
487
        RequestOptions.unsafeGetStripeVersionOverride(options),
1✔
488
        options.getBaseUrl(),
1✔
489
        options.getConnectTimeout() != null
1✔
490
            ? options.getConnectTimeout()
1✔
491
            : clientOptions.getConnectTimeout(),
1✔
492
        options.getReadTimeout() != null
1✔
493
            ? options.getReadTimeout()
1✔
494
            : clientOptions.getReadTimeout(),
1✔
495
        options.getMaxNetworkRetries() != null
1✔
496
            ? options.getMaxNetworkRetries()
1✔
497
            : clientOptions.getMaxNetworkRetries(),
1✔
498
        options.getConnectionProxy() != null
1✔
499
            ? options.getConnectionProxy()
1✔
500
            : clientOptions.getConnectionProxy(),
1✔
501
        options.getProxyCredential() != null
1✔
502
            ? options.getProxyCredential()
1✔
503
            : clientOptions.getProxyCredential());
1✔
504
  }
505

506
  public static class InvalidRequestOptionsException extends RuntimeException {
507
    private static final long serialVersionUID = 1L;
508

509
    public InvalidRequestOptionsException(String message) {
510
      super(message);
×
511
    }
×
512
  }
513
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc