Skip to content

Commit

Permalink
fetching data asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
yvasyliev committed Jan 6, 2024
1 parent 9562585 commit 548af39
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
29 changes: 28 additions & 1 deletion .github/workflows/release-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,34 @@ jobs:
echo "version=$version" >> "$GITHUB_OUTPUT"
perform-tests:
uses: ./.github/workflows/perform-tests.yml
# uses: ./.github/workflows/perform-tests.yml
runs-on: ubuntu-latest
environment: Test
concurrency: perform-tests
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'corretto'
cache: maven
- name: Build with Maven
env:
REDDIT_SUBREDDIT: ${{ secrets.REDDIT_SUBREDDIT }}
REDDIT_CLIENT_ID: ${{ secrets.REDDIT_CLIENT_ID }}
REDDIT_CLIENT_SECRET: ${{ secrets.REDDIT_CLIENT_SECRET }}
REDDIT_PASSWORD: ${{ secrets.REDDIT_PASSWORD }}
REDDIT_USERNAME: ${{ secrets.REDDIT_USERNAME }}
SPRING_JPA_HIBERNATE_DDL_AUTO: ${{ vars.SPRING_JPA_HIBERNATE_DDL_AUTO }}
TELEGRAM_ADMIN_ID: ${{ secrets.TELEGRAM_ADMIN_ID }}
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_BOT_USERNAME: ${{ secrets.TELEGRAM_BOT_USERNAME }}
TELEGRAM_CHANNEL_ID: ${{ secrets.TELEGRAM_CHANNEL_ID }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
TELEGRAM_SCHEDULE_POSTING_ENABLED: ${{ vars.TELEGRAM_SCHEDULE_POSTING_ENABLED }}
run: mvn test

get-release-version:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Spring Boot-based Java application to forward Reddit posts to Telegram channel.

# Requirements

- `Java 17`
- `Java 21`
- `Maven`
- [Reddit](https://www.reddit.com/login/) profile
- [Telegram](https://web.telegram.org/a/) profile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.stereotype.Service;
import org.springframework.util.function.ThrowingSupplier;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -23,6 +24,9 @@
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.StreamSupport;

@Service
Expand Down Expand Up @@ -53,7 +57,7 @@ public class SubredditNewSupplier implements ThrowingSupplier<List<Post>> {
@Override
@NonNull
public List<Post> getWithException() throws Exception {
var subredditPosts = fetchNewPosts();
var subredditPosts = fetchNewPosts().get(timeout, TimeUnit.MINUTES);
var children = subredditPosts.withArray("/data/children").elements();
var lastCreated = propertyService.findLastCreated().orElse(0);
return StreamSupport
Expand All @@ -79,7 +83,7 @@ public List<Post> getWithException() throws Exception {
.toList();
}

private JsonNode fetchNewPosts() throws Exception {
private CompletableFuture<JsonNode> fetchNewPosts() throws Exception {
var redditAccessToken = redditAccessTokenSupplier.getWithException();
var authorization = "Bearer %s".formatted(redditAccessToken.token());
var api = "https://oauth.reddit.com/r/%s/new?raw_json=1".formatted(subreddit);
Expand All @@ -89,7 +93,14 @@ private JsonNode fetchNewPosts() throws Exception {
.GET()
.timeout(Duration.ofMinutes(timeout))
.build();
var jsonBody = httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body();
return objectMapper.readTree(jsonBody);
return httpClient
.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream())
.thenApply(httpResponse -> {
try (var body = httpResponse.body()) {
return objectMapper.readTree(body);
} catch (IOException e) {
throw new CompletionException(e);
}
});
}
}

0 comments on commit 548af39

Please sign in to comment.