Skip to content

Commit

Permalink
Define StripeClient "usage" in a single place (#1744)
Browse files Browse the repository at this point in the history
* Introduce ApiService.request( for usage-related logic

* .inputStream, too

* The setter doesn't mutate

* Codegen
  • Loading branch information
richardm-stripe authored Feb 5, 2024
1 parent 463d996 commit afaf454
Show file tree
Hide file tree
Showing 132 changed files with 470 additions and 997 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/stripe/net/ApiService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.stripe.net;

import com.stripe.exception.StripeException;
import com.stripe.model.StripeObjectInterface;
import java.io.InputStream;
import java.lang.reflect.Type;
import lombok.AccessLevel;
import lombok.Getter;

Expand All @@ -11,4 +15,14 @@ public abstract class ApiService {
protected ApiService(StripeResponseGetter responseGetter) {
this.responseGetter = responseGetter;
}

@SuppressWarnings("TypeParameterUnusedInFormals")
protected <T extends StripeObjectInterface> T request(ApiRequest request, Type typeToken)
throws StripeException {
return this.getResponseGetter().request(request.addUsage("stripe_client"), typeToken);
}

protected InputStream requestStream(ApiRequest request) throws StripeException {
return this.getResponseGetter().requestStream(request.addUsage("stripe_client"));
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/stripe/service/AccountLinkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public AccountLink create(AccountLinkCreateParams params, RequestOptions options
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, AccountLink.class);
return this.request(request, AccountLink.class);
}
}
22 changes: 7 additions & 15 deletions src/main/java/com/stripe/service/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public Account delete(String account, RequestOptions options) throws StripeExcep
ApiRequest request =
new ApiRequest(
BaseAddress.API, ApiResource.RequestMethod.DELETE, path, null, options, ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
return this.request(request, Account.class);
}
/** Retrieves the details of an account. */
public Account retrieve(String account, AccountRetrieveParams params) throws StripeException {
Expand All @@ -82,8 +81,7 @@ public Account retrieve(String account, AccountRetrieveParams params, RequestOpt
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
return this.request(request, Account.class);
}
/**
* Updates a <a href="https://stripe.com/docs/connect/accounts">connected account</a> by setting
Expand Down Expand Up @@ -165,8 +163,7 @@ public Account update(String account, AccountUpdateParams params, RequestOptions
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
return this.request(request, Account.class);
}
/** Retrieves the details of an account. */
public Account retrieveCurrent(AccountRetrieveCurrentParams params) throws StripeException {
Expand All @@ -192,8 +189,7 @@ public Account retrieveCurrent(AccountRetrieveCurrentParams params, RequestOptio
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
return this.request(request, Account.class);
}
/**
* Returns a list of accounts connected to your platform via <a
Expand Down Expand Up @@ -235,9 +231,7 @@ public StripeCollection<Account> list(AccountListParams params, RequestOptions o
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<Account>>() {}.getType());
return this.request(request, new TypeToken<StripeCollection<Account>>() {}.getType());
}
/**
* With <a href="https://stripe.com/docs/connect">Connect</a>, you can create Stripe accounts for
Expand Down Expand Up @@ -298,8 +292,7 @@ public Account create(AccountCreateParams params, RequestOptions options) throws
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
return this.request(request, Account.class);
}
/**
* With <a href="https://stripe.com/docs/connect">Connect</a>, you may flag accounts as
Expand Down Expand Up @@ -329,8 +322,7 @@ public Account reject(String account, AccountRejectParams params, RequestOptions
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
return this.request(request, Account.class);
}

public com.stripe.service.CapabilityService capabilities() {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/stripe/service/AccountSessionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public AccountSession create(AccountSessionCreateParams params, RequestOptions o
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, AccountSession.class);
return this.request(request, AccountSession.class);
}
}
13 changes: 4 additions & 9 deletions src/main/java/com/stripe/service/ApplePayDomainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public ApplePayDomain delete(String domain, RequestOptions options) throws Strip
ApiRequest request =
new ApiRequest(
BaseAddress.API, ApiResource.RequestMethod.DELETE, path, null, options, ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, ApplePayDomain.class);
return this.request(request, ApplePayDomain.class);
}
/** Retrieve an apple pay domain. */
public ApplePayDomain retrieve(String domain, ApplePayDomainRetrieveParams params)
Expand Down Expand Up @@ -61,8 +60,7 @@ public ApplePayDomain retrieve(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, ApplePayDomain.class);
return this.request(request, ApplePayDomain.class);
}
/** List apple pay domains. */
public StripeCollection<ApplePayDomain> list(ApplePayDomainListParams params)
Expand All @@ -89,9 +87,7 @@ public StripeCollection<ApplePayDomain> list(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<ApplePayDomain>>() {}.getType());
return this.request(request, new TypeToken<StripeCollection<ApplePayDomain>>() {}.getType());
}
/** Create an apple pay domain. */
public ApplePayDomain create(ApplePayDomainCreateParams params) throws StripeException {
Expand All @@ -109,7 +105,6 @@ public ApplePayDomain create(ApplePayDomainCreateParams params, RequestOptions o
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, ApplePayDomain.class);
return this.request(request, ApplePayDomain.class);
}
}
7 changes: 2 additions & 5 deletions src/main/java/com/stripe/service/ApplicationFeeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ public StripeCollection<ApplicationFee> list(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<ApplicationFee>>() {}.getType());
return this.request(request, new TypeToken<StripeCollection<ApplicationFee>>() {}.getType());
}
/**
* Retrieves the details of an application fee that your account has collected. The same
Expand Down Expand Up @@ -100,8 +98,7 @@ public ApplicationFee retrieve(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, ApplicationFee.class);
return this.request(request, ApplicationFee.class);
}

public com.stripe.service.FeeRefundService feeRefunds() {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/stripe/service/BalanceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public Balance retrieve(BalanceRetrieveParams params, RequestOptions options)
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Balance.class);
return this.request(request, Balance.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ public StripeCollection<BalanceTransaction> list(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<BalanceTransaction>>() {}.getType());
return this.request(
request, new TypeToken<StripeCollection<BalanceTransaction>>() {}.getType());
}
/**
* Retrieves the balance transaction with the given ID.
Expand Down Expand Up @@ -120,7 +119,6 @@ public BalanceTransaction retrieve(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, BalanceTransaction.class);
return this.request(request, BalanceTransaction.class);
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/stripe/service/BankAccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public BankAccount verify(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, BankAccount.class);
return this.request(request, BankAccount.class);
}
}
10 changes: 3 additions & 7 deletions src/main/java/com/stripe/service/CapabilityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ public StripeCollection<Capability> list(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<Capability>>() {}.getType());
return this.request(request, new TypeToken<StripeCollection<Capability>>() {}.getType());
}
/** Retrieves information about the specified Account Capability. */
public Capability retrieve(String account, String capability, CapabilityRetrieveParams params)
Expand Down Expand Up @@ -94,8 +92,7 @@ public Capability retrieve(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Capability.class);
return this.request(request, Capability.class);
}
/**
* Updates an existing Account Capability. Request or remove a capability by updating its {@code
Expand Down Expand Up @@ -139,7 +136,6 @@ public Capability update(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Capability.class);
return this.request(request, Capability.class);
}
}
6 changes: 2 additions & 4 deletions src/main/java/com/stripe/service/CashBalanceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public CashBalance retrieve(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, CashBalance.class);
return this.request(request, CashBalance.class);
}
/** Changes the settings on a customer’s cash balance. */
public CashBalance update(String customer, CashBalanceUpdateParams params)
Expand All @@ -73,7 +72,6 @@ public CashBalance update(String customer, CashBalanceUpdateParams params, Reque
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, CashBalance.class);
return this.request(request, CashBalance.class);
}
}
20 changes: 6 additions & 14 deletions src/main/java/com/stripe/service/ChargeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public StripeCollection<Charge> list(ChargeListParams params, RequestOptions opt
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<Charge>>() {}.getType());
return this.request(request, new TypeToken<StripeCollection<Charge>>() {}.getType());
}
/**
* This method is no longer recommended—use the <a
Expand Down Expand Up @@ -109,8 +107,7 @@ public Charge create(ChargeCreateParams params, RequestOptions options) throws S
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Charge.class);
return this.request(request, Charge.class);
}
/**
* Retrieves the details of a charge that has previously been created. Supply the unique charge ID
Expand Down Expand Up @@ -152,8 +149,7 @@ public Charge retrieve(String charge, ChargeRetrieveParams params, RequestOption
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Charge.class);
return this.request(request, Charge.class);
}
/**
* Updates the specified charge by setting the values of the parameters passed. Any parameters not
Expand Down Expand Up @@ -191,8 +187,7 @@ public Charge update(String charge, ChargeUpdateParams params, RequestOptions op
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Charge.class);
return this.request(request, Charge.class);
}
/**
* Search for charges you’ve previously created using Stripe’s <a
Expand Down Expand Up @@ -224,9 +219,7 @@ public StripeSearchResult<Charge> search(ChargeSearchParams params, RequestOptio
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeSearchResult<Charge>>() {}.getType());
return this.request(request, new TypeToken<StripeSearchResult<Charge>>() {}.getType());
}
/**
* Capture the payment of an existing, uncaptured charge that was created with the {@code capture}
Expand Down Expand Up @@ -292,7 +285,6 @@ public Charge capture(String charge, ChargeCaptureParams params, RequestOptions
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Charge.class);
return this.request(request, Charge.class);
}
}
7 changes: 2 additions & 5 deletions src/main/java/com/stripe/service/CountrySpecService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public StripeCollection<CountrySpec> list(CountrySpecListParams params, RequestO
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<CountrySpec>>() {}.getType());
return this.request(request, new TypeToken<StripeCollection<CountrySpec>>() {}.getType());
}
/** Returns a Country Spec for a given Country code. */
public CountrySpec retrieve(String country, CountrySpecRetrieveParams params)
Expand Down Expand Up @@ -75,7 +73,6 @@ public CountrySpec retrieve(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, CountrySpec.class);
return this.request(request, CountrySpec.class);
}
}
16 changes: 5 additions & 11 deletions src/main/java/com/stripe/service/CouponService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public Coupon delete(String coupon, RequestOptions options) throws StripeExcepti
ApiRequest request =
new ApiRequest(
BaseAddress.API, ApiResource.RequestMethod.DELETE, path, null, options, ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Coupon.class);
return this.request(request, Coupon.class);
}
/** Retrieves the coupon with the given ID. */
public Coupon retrieve(String coupon, CouponRetrieveParams params) throws StripeException {
Expand All @@ -70,8 +69,7 @@ public Coupon retrieve(String coupon, CouponRetrieveParams params, RequestOption
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Coupon.class);
return this.request(request, Coupon.class);
}
/**
* Updates the metadata of a coupon. Other coupon details (currency, duration, amount_off) are, by
Expand Down Expand Up @@ -109,8 +107,7 @@ public Coupon update(String coupon, CouponUpdateParams params, RequestOptions op
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Coupon.class);
return this.request(request, Coupon.class);
}
/** Returns a list of your coupons. */
public StripeCollection<Coupon> list(CouponListParams params) throws StripeException {
Expand All @@ -136,9 +133,7 @@ public StripeCollection<Coupon> list(CouponListParams params, RequestOptions opt
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<Coupon>>() {}.getType());
return this.request(request, new TypeToken<StripeCollection<Coupon>>() {}.getType());
}
/**
* You can create coupons easily via the <a href="https://dashboard.stripe.com/coupons">coupon
Expand Down Expand Up @@ -203,7 +198,6 @@ public Coupon create(CouponCreateParams params, RequestOptions options) throws S
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.addUsage("stripe_client");
return getResponseGetter().request(request, Coupon.class);
return this.request(request, Coupon.class);
}
}
Loading

0 comments on commit afaf454

Please sign in to comment.