Skip to content

Commit 9bdae1d

Browse files
committed
made leetcode async
1 parent 06a0978 commit 9bdae1d

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/main/java/org/example/project/service/LeetcodeService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
import java.net.http.HttpRequest;
1717
import java.net.http.HttpResponse;
1818
import java.util.*;
19+
import java.util.concurrent.ExecutionException;
1920

2021
@Slf4j
2122
@Service
2223
public class LeetcodeService {
2324

24-
public String getProblemWithTagAndDifficulty(String difficulty, String userTag) throws URISyntaxException, IOException, InterruptedException {
25+
public String getProblemWithTagAndDifficulty(String difficulty, String userTag) throws URISyntaxException, IOException, InterruptedException, ExecutionException {
2526
List<Problem> problems = getProblemsWithTagAndDifficulty(difficulty, userTag);
2627
if (problems.isEmpty())
2728
throw new IOException();
@@ -30,19 +31,19 @@ public String getProblemWithTagAndDifficulty(String difficulty, String userTag)
3031
return "Title: " + randomProblem.title + "\nLink: https://leetcode.com/problems/" + randomProblem.titleSlug;
3132
}
3233

33-
private List<Problem> getProblemsWithTagAndDifficulty(String difficulty, String userTag) throws URISyntaxException, IOException, InterruptedException {
34+
private List<Problem> getProblemsWithTagAndDifficulty(String difficulty, String userTag) throws URISyntaxException, IOException, InterruptedException, ExecutionException {
3435
try {
3536
HttpClient client = HttpClient.newHttpClient();
3637
HttpRequest request = HttpRequest.newBuilder()
3738
.uri(new URI("http://leetcode:3000/problems?tags=" + userTag + "&limit=10000"))
3839
.GET()
3940
.build();
4041

41-
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
42+
var response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
4243

4344
ObjectMapper objectMapper = new ObjectMapper();
4445

45-
ProblemSet problemset = objectMapper.readValue(response.body(), new TypeReference<>() {});
46+
ProblemSet problemset = objectMapper.readValue(response.get().body(), new TypeReference<>() {});
4647

4748
List<Problem> problems = new ArrayList<>();
4849
for (Question question : problemset.getProblemsetQuestionList()) {

src/main/java/org/example/project/service/command/leetcode/GotTagHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.io.IOException;
1313
import java.net.URISyntaxException;
14+
import java.util.concurrent.ExecutionException;
1415

1516
@Component
1617
@RequiredArgsConstructor
@@ -37,7 +38,7 @@ public void handleCommand(long chatId, Update update) {
3738
LeetcodeCommandHandlerFactory.getDifficulty(),
3839
tag
3940
);
40-
} catch (URISyntaxException | IOException | InterruptedException e) {
41+
} catch (URISyntaxException | IOException | InterruptedException | ExecutionException e) {
4142
messageSender.sendMessageWithKeyboard(
4243
chatId,
4344
"Internal server error!",

0 commit comments

Comments
 (0)