Skip to content

Commit

Permalink
Fix errors that the mentor suggested: added method getCharacterReques…
Browse files Browse the repository at this point in the history
…t(int) in CharacterServiceImpl to avoid code duplication
  • Loading branch information
WasjaZeikan committed Oct 21, 2023
1 parent 6cab2db commit 1d77575
Showing 1 changed file with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ public List<CharacterDto> getCharactersContainName(String name) {
}

private void initCounts(HttpClient httpClient) throws IOException, InterruptedException {
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create(String.format(URL,1)))
.build();
HttpRequest httpRequest = getCharacterRequest(1);
HttpResponse<String> httpResponse = httpClient.send(httpRequest,
HttpResponse.BodyHandlers.ofString());
CharacterMetaInformationResponseDto responseDto = objectMapper
Expand All @@ -78,12 +75,16 @@ private void initCounts(HttpClient httpClient) throws IOException, InterruptedEx

private CharacterEndpointResponseDto getCharactersFromApi(HttpClient httpClient, int page)
throws IOException, InterruptedException {
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create(String.format(URL, page)))
.build();
HttpRequest httpRequest = getCharacterRequest(page);
HttpResponse<String> httpResponse = httpClient.send(httpRequest,
HttpResponse.BodyHandlers.ofString());
return objectMapper.readValue(httpResponse.body(), CharacterEndpointResponseDto.class);
}

private HttpRequest getCharacterRequest(int page) {
return HttpRequest.newBuilder()
.GET()
.uri(URI.create(String.format(URL, page)))
.build();
}
}

0 comments on commit 1d77575

Please sign in to comment.