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

v3.0.1 #35

Merged
merged 2 commits into from
Dec 20, 2023
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ for more details).
| Variable | Required | Default value | Description | Example |
|:--------------------------------------------:|:--------:|:-------------:|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-------------------------------------------------|
| `delayed.blocking.executor.delay.in.seconds` | `false` | `20` | Delay in seconds used to postpone sending messages in order to avoid "too many requests" errors in Telegram. | `20` |
| `http.request.timeout.in.minutes` | `false` | `2` | Timeout in minutes applied to Reddit API call. | `5` |
| `reddit.authors.blocked.by.default` | `false` | `false` | `true` - check Reddit autor against Block list. `false` - ignore Block list and ask admin to validate every post. | `true` |
| `reddit.client.id` | `true` | - | Reddit `client_id`. | `pW134F0XNuueG4W78x9uGA` |
| `reddit.client.secret` | `true` | - | Reddit client `secret`. | `fsdT6VkTgf1WMfSW6Pd5t4DRvfVueB` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.net.URLEncoder;
import java.net.http.HttpRequest;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Base64;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -29,6 +30,9 @@ public class RedditAccessTokenRequestFactoryBean implements FactoryBean<HttpRequ
@Value("java:${project.artifactId}:${project.version} (by /u/${reddit.username})")
private String userAgent;

@Value("#{T(java.time.Duration).ofMinutes(${http.request.timeout.in.minutes:2})}")
private Duration timeout;

@Override
public HttpRequest getObject() {
var credentials = "%s:%s".formatted(redditClientId, redditClientSecret);
Expand All @@ -43,6 +47,7 @@ public HttpRequest getObject() {
.header("Content-Type", "application/x-www-form-urlencoded")
.header("User-Agent", userAgent)
.POST(HttpRequest.BodyPublishers.ofString(encode(payload)))
.timeout(timeout)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.yvasyliev.model.dto.RedditAccessToken;
import org.apache.http.client.HttpResponseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.function.ThrowingSupplier;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
Expand All @@ -27,7 +29,11 @@ public class RedditAccessTokenSupplier implements ThrowingSupplier<RedditAccessT
@Override
public RedditAccessToken getWithException() throws IOException, InterruptedException {
if (redditAccessToken == null || redditAccessToken.isExpired()) {
var jsonBody = httpClient.send(redditAccessTokenRequest, HttpResponse.BodyHandlers.ofString()).body();
var response = httpClient.send(redditAccessTokenRequest, HttpResponse.BodyHandlers.ofString());
var jsonBody = response.body();
if (response.statusCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
throw new HttpResponseException(response.statusCode(), jsonBody);
}
redditAccessToken = objectMapper.readValue(jsonBody, RedditAccessToken.class);
}
return redditAccessToken;
Expand Down
Loading