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

Change misleading identifier "name" of getPost to "id" in Reddit4J.java and add Method for retrieving Raw Json Response #20

Merged
merged 6 commits into from
Jul 26, 2024
Merged
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
46 changes: 42 additions & 4 deletions src/main/java/masecla/reddit4j/client/Reddit4J.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ public Connection useEndpoint(String endpointPath) {
return connection;
}

public Connection useUnauthorizedEndpoint(String endpointPath) {
Connection connection = Jsoup.connect(OAUTH_URL + endpointPath);
connection.ignoreContentType(true).userAgent(userAgent);
connection.maxBodySize(0);
return connection;
}

public void ensureConnection() throws IOException, InterruptedException, AuthenticationException {
// There is no token
if (token == null) {
Expand Down Expand Up @@ -461,12 +468,12 @@ public SubredditPostListingEndpointRequest getSubredditPosts(String subreddit, S
return new SubredditPostListingEndpointRequest("/r/" + subreddit + "/" + sorting.getValue(), this);
}

public Optional<RedditPost> getPost(String name) throws IOException, InterruptedException {
if (!name.startsWith("t3_")) {
name = "t3_" + name;
public Optional<RedditPost> getPost(String id) throws IOException, InterruptedException {
if (!id.startsWith("t3_")) {
id = "t3_" + id;
}

Connection connection = useEndpoint("/api/info").data("id", name);
Connection connection = useEndpoint("/api/info").data("id", id);
Response response = this.httpClient.execute(connection);

TypeToken<?> ttData3 = TypeToken.getParameterized(RedditData.class, RedditPost.class);
Expand All @@ -492,6 +499,37 @@ public SubredditPostListingEndpointRequest getUserSubmitted(String username) {
return new SubredditPostListingEndpointRequest("/user/" + username + "/submitted", this);
}

public String getRawJson(String endpointPath, Method method, boolean authorized) throws IOException, InterruptedException {
Connection connection;
if (authorized) {
connection = useEndpoint(endpointPath).method(method);
}
else {
connection = useUnauthorizedEndpoint(endpointPath).method(method);
}
Response response = this.httpClient.execute(connection);
return response.body();
}

public Optional<RedditPost> getRandom(boolean authorized, int limit) throws IOException, InterruptedException {
Connection connection;
if (authorized) {
assert limit > 0;
connection = useEndpoint("/random.json?limit=" + limit);
}
else {
connection = useUnauthorizedEndpoint("/random.json");
}
Response response = this.httpClient.execute(connection);

TypeToken<?> ttData3 = TypeToken.getParameterized(RedditData.class, RedditPost.class);
TypeToken<?> ttData2 = TypeToken.getParameterized(RedditListing.class, ttData3.getType());
TypeToken<?> ttData = TypeToken.getParameterized(RedditData.class, ttData2.getType());
RedditData<RedditListing<RedditData<RedditPost>>> fromJson = new Gson().fromJson(JsonParser.parseString(response.body()).getAsJsonArray().get(0), ttData.getType());

return fromJson.getData().getChildren().stream().findFirst().map(RedditData::getData);
}

@Deprecated
public static Reddit4J unlimited() {
Reddit4J result = new Reddit4J();
Expand Down
Loading