Skip to content

Commit

Permalink
Merge pull request #50 from alexanderjordanbaker/AllowUnknownEnumValues
Browse files Browse the repository at this point in the history
Allow accessing new Enum values without requiring a library upgrade
  • Loading branch information
alexanderjordanbaker authored Oct 27, 2023
2 parents d8aae17 + cb93c45 commit b157703
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 103 deletions.
13 changes: 11 additions & 2 deletions src/main/java/com/apple/itunes/storekit/client/APIException.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class APIException extends Exception {
private final int httpStatusCode;
private final APIError apiError;
private final Long apiError;

public APIException(int httpStatusCode) {
this.httpStatusCode = httpStatusCode;
Expand All @@ -18,14 +18,23 @@ public APIException(int httpStatusCode) {

public APIException(int httpStatusCode, APIError apiError) {
this.httpStatusCode = httpStatusCode;
this.apiError = apiError;
this.apiError = apiError != null ? apiError.errorCode() : null;
}

public APIException(int httpStatusCode, Long rawApiError) {
this.httpStatusCode = httpStatusCode;
this.apiError = rawApiError;
}

public int getHttpStatusCode() {
return httpStatusCode;
}

public APIError getApiError() {
return apiError != null ? APIError.fetchErrorResponseFromErrorCode(apiError) : null;
}

public Long getRawApiError() {
return apiError;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ public class AppStoreServerAPIClient {
* @param environment The environment to target
*/
public AppStoreServerAPIClient(String signingKey, String keyId, String issuerId, String bundleId, Environment environment) {
BearerTokenAuthenticator bearerTokenAuthenticator = new BearerTokenAuthenticator(signingKey, keyId, issuerId, bundleId);
this.bearerTokenAuthenticator = bearerTokenAuthenticator;
this.bearerTokenAuthenticator = new BearerTokenAuthenticator(signingKey, keyId, issuerId, bundleId);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
this.httpClient = builder.build();
this.urlBase = HttpUrl.parse(environment.equals(Environment.SANDBOX) ? SANDBOX_URL : PRODUCTION_URL);
this.gson = new Gson();
}

private Request buildRequest(String path, String method, Map<String, List<String>> queryParameters, Object body) {
private Response makeRequest(String path, String method, Map<String, List<String>> queryParameters, Object body) throws IOException {
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.addHeader("User-Agent", USER_AGENT);
requestBuilder.addHeader("Authorization", "Bearer " + bearerTokenAuthenticator.generateToken());
Expand All @@ -86,13 +85,16 @@ private Request buildRequest(String path, String method, Map<String, List<String
} else {
requestBuilder.method(method, null);
}
return requestBuilder.build();
return getResponse(requestBuilder.build());
}

private <T> T makeHttpCall(String path, String method, Map<String, List<String>> queryParameters, Object body, Class<T> clazz) throws IOException, APIException {
Request request = buildRequest(path, method, queryParameters, body);
protected Response getResponse(Request request) throws IOException {
Call call = httpClient.newCall(request);
try (Response r = call.execute()) {
return call.execute();
}

protected <T> T makeHttpCall(String path, String method, Map<String, List<String>> queryParameters, Object body, Class<T> clazz) throws IOException, APIException {
try (Response r = makeRequest(path, method, queryParameters, body)) {
if (r.code() >= 200 && r.code() < 300) {
if (clazz.equals(Void.class)) {
return null;
Expand All @@ -109,10 +111,7 @@ private <T> T makeHttpCall(String path, String method, Map<String, List<String>>
ResponseBody responseBody = r.body();
if (responseBody != null) {
ErrorPayload errorPayload = gson.fromJson(responseBody.charStream(), ErrorPayload.class);
APIError apiError = APIError.fetchErrorResponseFromErrorCode(errorPayload.getErrorCode());
if (apiError != null) {
throw new APIException(r.code(), apiError);
}
throw new APIException(r.code(), errorPayload.getErrorCode());
}
} catch (APIException e) {
throw e;
Expand All @@ -137,7 +136,7 @@ private <T> T makeHttpCall(String path, String method, Map<String, List<String>>
* @see <a href="https://developer.apple.com/documentation/appstoreserverapi/extend_subscription_renewal_dates_for_all_active_subscribers">Extend Subscription Renewal Dates for All Active Subscribers</a>
*/
public MassExtendRenewalDateResponse extendRenewalDateForAllActiveSubscribers(MassExtendRenewalDateRequest massExtendRenewalDateRequest) throws APIException, IOException {
return makeHttpCall("/inApps/v1/subscriptions/extend/mass/", "POST", Map.of(), massExtendRenewalDateRequest, MassExtendRenewalDateResponse.class);
return makeHttpCall("/inApps/v1/subscriptions/extend/mass", "POST", Map.of(), massExtendRenewalDateRequest, MassExtendRenewalDateResponse.class);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/apple/itunes/storekit/model/AppTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class AppTransaction implements DecodedSignedData {
private static final String SERIALIZED_NAME_PREORDER_DATE = "preorderDate";

@SerializedName(SERIALIZED_NAME_RECEIPT_TYPE)
private Environment receiptType;
private String receiptType;
@SerializedName(SERIALIZED_NAME_APP_APPLE_ID)
private Long appAppleId;
@SerializedName(SERIALIZED_NAME_BUNDLE_ID)
Expand Down Expand Up @@ -58,15 +58,26 @@ public class AppTransaction implements DecodedSignedData {
@see <a href="https://developer.apple.com/documentation/storekit/apptransaction/3963901-environment">environment</a>
*/
public Environment getReceiptType() {
return this.receiptType != null ? Environment.fromValue(this.receiptType) : null;
}

/**
* @see #getReceiptType()
*/
public String getRawReceiptType() {
return this.receiptType;
}

public void setReceiptType(Environment receiptType) {
this.receiptType = receiptType;
this.receiptType = receiptType != null ? receiptType.getValue() : null;
}

public void setRawReceiptType(String rawReceiptType) {
this.receiptType = rawReceiptType;
}

public AppTransaction receiptType(Environment receiptType) {
this.receiptType = receiptType;
this.receiptType = receiptType != null ? receiptType.getValue() : null;
return this;
}

Expand Down
Loading

0 comments on commit b157703

Please sign in to comment.