From b49900200545c975ecb3481f66a2955a74b57e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=98=9F/=E3=82=B5=E3=83=BC=E3=83=93=E3=82=B9?= =?UTF-8?q?=E9=96=8B=E7=99=BA=E3=83=81=E3=83=BC=E3=83=A0/JP?= Date: Thu, 22 Aug 2019 21:26:35 +0900 Subject: [PATCH] add proxy feature --- src/main/java/com/patreon/PatreonAPI.java | 16 ++++++++++++- src/main/java/com/patreon/PatreonOAuth.java | 24 +++++++++++++++---- .../com/patreon/resources/RequestUtil.java | 12 +++++++++- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/patreon/PatreonAPI.java b/src/main/java/com/patreon/PatreonAPI.java index 4ca62aa..cadf4ed 100644 --- a/src/main/java/com/patreon/PatreonAPI.java +++ b/src/main/java/com/patreon/PatreonAPI.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.io.InputStream; +import java.net.Proxy; import java.net.URI; import java.net.URISyntaxException; import java.util.*; @@ -32,6 +33,7 @@ public class PatreonAPI { private final String accessToken; private final RequestUtil requestUtil; private ResourceConverter converter; + private final Proxy proxy; /** * Create a new instance of the Patreon API. You only need one of these objects unless you are using the API @@ -45,12 +47,24 @@ public PatreonAPI(String accessToken) { this(accessToken, new RequestUtil()); } + public PatreonAPI(String accessToken, Proxy proxy) { + this(accessToken, new RequestUtil(), proxy); + } + /** * For use in test. */ PatreonAPI(String accessToken, RequestUtil requestUtil) { + this(accessToken, requestUtil, null); + } + + /** + * For use in test. + */ + PatreonAPI(String accessToken, RequestUtil requestUtil, Proxy proxy) { this.accessToken = accessToken; this.requestUtil = requestUtil; + this.proxy = proxy; ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); @@ -198,7 +212,7 @@ public List fetchAllPledges(String campaignId) throws IOException { private InputStream getDataStream(String suffix) throws IOException { - return this.requestUtil.request(suffix, this.accessToken); + return this.requestUtil.request(suffix, this.accessToken, this.proxy); } /** diff --git a/src/main/java/com/patreon/PatreonOAuth.java b/src/main/java/com/patreon/PatreonOAuth.java index 984986a..3661c71 100644 --- a/src/main/java/com/patreon/PatreonOAuth.java +++ b/src/main/java/com/patreon/PatreonOAuth.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; +import java.net.Proxy; import java.net.URISyntaxException; import java.util.Calendar; import java.util.Date; @@ -22,11 +23,20 @@ public class PatreonOAuth { private final String clientID; private final String clientSecret; private final String redirectUri; + private final Proxy proxy; public PatreonOAuth(String clientID, String clientSecret, String redirectUri) { this.clientID = clientID; this.clientSecret = clientSecret; this.redirectUri = redirectUri; + this.proxy = null; + } + + public PatreonOAuth(String clientID, String clientSecret, String redirectUri, Proxy proxy) { + this.clientID = clientID; + this.clientSecret = clientSecret; + this.redirectUri = redirectUri; + this.proxy = proxy; } private static E toObject(String str, Class clazz) { @@ -54,9 +64,7 @@ public TokensResponse getTokens(String code) throws IOException { .data("client_secret", clientSecret) .data("redirect_uri", redirectUri) .ignoreContentType(true); - String response = requestInfo.post().body().text(); - - return toObject(response, TokensResponse.class); + return toObject(getResponseBodyText(requestInfo, this.proxy), TokensResponse.class); } public TokensResponse refreshTokens(String refreshToken) throws IOException { @@ -66,8 +74,14 @@ public TokensResponse refreshTokens(String refreshToken) throws IOException { .data("client_secret", clientSecret) .data("refresh_token", refreshToken) .ignoreContentType(true); - String response = requestInfo.post().body().text(); - return toObject(response, TokensResponse.class); + return toObject(getResponseBodyText(requestInfo, this.proxy), TokensResponse.class); + } + + private String getResponseBodyText(Connection requestInfo, Proxy proxy) throws IOException { + if (proxy == null) { + return requestInfo.post().body().text(); + } + return requestInfo.proxy(proxy).post().body().text(); } public static class TokensResponse { diff --git a/src/main/java/com/patreon/resources/RequestUtil.java b/src/main/java/com/patreon/resources/RequestUtil.java index cabb20e..149def5 100644 --- a/src/main/java/com/patreon/resources/RequestUtil.java +++ b/src/main/java/com/patreon/resources/RequestUtil.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; +import java.net.Proxy; import java.net.URL; import static com.patreon.PatreonAPI.BASE_URI; @@ -13,9 +14,18 @@ public class RequestUtil { public InputStream request(String pathSuffix, String accessToken) throws IOException { + return request(pathSuffix, accessToken, null); + } + + public InputStream request(String pathSuffix, String accessToken, Proxy proxy) throws IOException { String prefix = BASE_URI + "/api/oauth2/api/"; URL url = new URL(prefix.concat(pathSuffix)); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + HttpURLConnection connection; + if (proxy == null) { + connection = (HttpURLConnection) url.openConnection(); + } else { + connection = (HttpURLConnection) url.openConnection(proxy); + } connection.setRequestProperty("Authorization", "Bearer ".concat(accessToken)); connection.setRequestProperty("User-Agent", String.format(