Skip to content

Commit

Permalink
v3.0.1 (#35)
Browse files Browse the repository at this point in the history
* Added timeout to Reddit API HTTP request

* Handling HTTP error response
  • Loading branch information
yvasyliev authored Dec 20, 2023
1 parent daa845d commit f7aef31
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
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

0 comments on commit f7aef31

Please sign in to comment.