Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the need for an OAuth API mode #1854

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/main/java/com/stripe/net/ApiMode.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.stripe.net;

public enum ApiMode {
V1,
OAuth
V1
}
23 changes: 17 additions & 6 deletions src/main/java/com/stripe/net/LiveStripeResponseGetter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stripe.net;

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.stripe.Stripe;
import com.stripe.exception.*;
Expand Down Expand Up @@ -102,7 +103,7 @@ public <T extends StripeObjectInterface> T request(ApiRequest apiRequest, Type t
String requestId = response.requestId();

if (responseCode < 200 || responseCode >= 300) {
handleError(response, apiRequest.getApiMode());
handleError(response);
}

T resource = null;
Expand Down Expand Up @@ -151,7 +152,7 @@ public InputStream requestStream(ApiRequest apiRequest) throws StripeException {
Stripe.getApiBase(), e.getMessage()),
e);
}
handleError(response, apiRequest.getApiMode());
handleError(response);
}

return responseStream.body();
Expand Down Expand Up @@ -202,11 +203,21 @@ private static void raiseMalformedJsonError(
e);
}

private void handleError(StripeResponse response, ApiMode apiMode) throws StripeException {
if (apiMode == ApiMode.V1) {
private void handleError(StripeResponse response) throws StripeException {
JsonObject responseBody = ApiResource.GSON.fromJson(response.body(), JsonObject.class);

/*
OAuth errors are JSON objects where `error` is a string. In
contrast, in API errors, `error` is a hash with sub-keys. We use
this property to distinguish between OAuth and API errors.
*/
if (responseBody.has("error") && responseBody.get("error").isJsonPrimitive()) {
JsonPrimitive error = responseBody.getAsJsonPrimitive("error");
if (error.isString()) {
handleOAuthError(response);
}
} else {
handleApiError(response);
} else if (apiMode == ApiMode.OAuth) {
handleOAuthError(response);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/stripe/net/OAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static TokenResponse token(Map<String, Object> params, RequestOptions opt
"/oauth/token",
params,
options,
ApiMode.OAuth);
ApiMode.V1);
return OAuth.globalResponseGetter.request(request, TokenResponse.class);
}

Expand All @@ -77,7 +77,7 @@ public static DeauthorizedAccount deauthorize(Map<String, Object> params, Reques
"/oauth/deauthorize",
paramsCopy,
options,
ApiMode.OAuth);
ApiMode.V1);
return OAuth.globalResponseGetter.request(request, DeauthorizedAccount.class);
}

Expand Down
8 changes: 1 addition & 7 deletions src/test/java/com/stripe/BaseStripeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.stripe.exception.StripeException;
import com.stripe.model.StripeObjectInterface;
import com.stripe.net.*;
import com.stripe.net.ApiMode;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -347,12 +346,7 @@ public static <T extends StripeObjectInterface> void stubOAuthRequest(
Class<T> clazz, String response) throws StripeException {
Mockito.doReturn(ApiResource.GSON.fromJson(response, clazz))
.when(networkSpy)
.request(
Mockito.argThat(
(req) -> {
return req.getApiMode() == ApiMode.OAuth;
}),
Mockito.<Type>any());
.request(Mockito.any(ApiRequest.class), Mockito.<Type>any());
}

/**
Expand Down
Loading