| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,11 +1,9 @@ | |||
| 1 | 1 | package net.discordjug.javabot.systems.help; | |
| 2 | 2 | ||
| 3 | - import java.io.IOException; | ||
| 4 | 3 | import java.net.URI; | |
| 5 | 4 | import java.net.http.HttpClient; | |
| 6 | 5 | import java.net.http.HttpRequest; | |
| 7 | 6 | import java.net.http.HttpRequest.BodyPublishers; | |
| 8 | - import java.net.http.HttpResponse; | ||
| 9 | 7 | import java.net.http.HttpResponse.BodyHandlers; | |
| 10 | 8 | ||
| 11 | 9 | import lombok.extern.slf4j.Slf4j; | |
@@ -21,13 +19,18 @@ public AnswerOverflowService(BotConfig config) { | |||
| 21 | 19 | apiKey = config.getSystems().getAnswerOverflowApiKey(); | |
| 22 | 20 | } | |
| 23 | 21 | ||
| 22 | + /** | ||
| 23 | + * Marks a message as the answer of a forum post with Answer Overflow. | ||
| 24 | + * @param postId The Discord ID of the forum post | ||
| 25 | + * @param messageId The Discord ID of the message that should be marked as the answer. | ||
| 26 | + */ | ||
| 24 | 27 | public void markAnswer(long postId, long messageId) { | |
| 25 | 28 | if (apiKey == null || apiKey.isBlank()) { | |
| 26 | 29 | return; | |
| 27 | 30 | } | |
| 28 | 31 | ||
| 29 | 32 | try (HttpClient client = HttpClient.newHttpClient()) { | |
| 30 | - HttpResponse<String> response = client.send( | ||
| 33 | + client.sendAsync( | ||
| 31 | 34 | HttpRequest.newBuilder(URI.create("https://www.answeroverflow.com/api/v1/messages/" + postId)) | |
| 32 | 35 | .header("x-api-key", apiKey) | |
| 33 | 36 | .header("Content-Type", "application/json") | |
@@ -37,14 +40,16 @@ public void markAnswer(long postId, long messageId) { | |||
| 37 | 40 | } | |
| 38 | 41 | """.formatted(messageId))) | |
| 39 | 42 | .build(), | |
| 40 | - BodyHandlers.ofString()); | ||
| 41 | - if (response.statusCode() != 200) { | ||
| 42 | - log.warn("Answer Overflow responded with unexpected status code {}, post ID: {}, message ID: {}, body: {}", response.statusCode(), postId, messageId, response.body()); | ||
| 43 | - } | ||
| 44 | - } catch (IOException e) { | ||
| 45 | - log.error("An exception occured trying to mark a post as an answer.", e); | ||
| 46 | - } catch (InterruptedException e) { | ||
| 47 | - Thread.currentThread().interrupt(); | ||
| 48 | - }; | ||
| 43 | + BodyHandlers.ofString()) | ||
| 44 | + .thenAccept(response -> { | ||
| 45 | + if (response.statusCode() != 200) { | ||
| 46 | + log.warn("Answer Overflow responded with unexpected status code {}, post ID: {}, message ID: {}, body: {}", response.statusCode(), postId, messageId, response.body()); | ||
| 47 | + } | ||
| 48 | + }) | ||
| 49 | + .exceptionally(e -> { | ||
| 50 | + log.error("An exception occured trying to mark a post as an answer.", e); | ||
| 51 | + return null; | ||
| 52 | + }); | ||
| 53 | + } | ||
| 49 | 54 | } | |
| 50 | 55 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments