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

add proxy feature #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion src/main/java/com/patreon/PatreonAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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 <b>one</b> of these objects unless you are using the API
Expand All @@ -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);
Expand Down Expand Up @@ -198,7 +212,7 @@ public List<Pledge> 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);
}

/**
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/com/patreon/PatreonOAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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> E toObject(String str, Class<E> clazz) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/patreon/resources/RequestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down