From b08ae0c93fe5bc9ce80caf80784b69f84be44cb9 Mon Sep 17 00:00:00 2001 From: Nataliia Shtykh Date: Wed, 14 Feb 2024 17:59:02 +0200 Subject: [PATCH] corrected getCharacters method --- .../controller/CharactersController.java | 2 +- .../service/DataInitialization.java | 1 + .../service/RickAndMortyClient.java | 33 +++++++++++-------- .../{ => character}/CharacterService.java | 2 +- .../{ => character}/CharacterServiceImpl.java | 2 +- 5 files changed, 24 insertions(+), 16 deletions(-) rename src/main/java/mate/academy/rickandmorty/service/{ => character}/CharacterService.java (86%) rename src/main/java/mate/academy/rickandmorty/service/{ => character}/CharacterServiceImpl.java (96%) diff --git a/src/main/java/mate/academy/rickandmorty/controller/CharactersController.java b/src/main/java/mate/academy/rickandmorty/controller/CharactersController.java index 88b7d86d..1d66bda5 100644 --- a/src/main/java/mate/academy/rickandmorty/controller/CharactersController.java +++ b/src/main/java/mate/academy/rickandmorty/controller/CharactersController.java @@ -5,7 +5,7 @@ import java.util.List; import lombok.RequiredArgsConstructor; import mate.academy.rickandmorty.dto.internal.CharacterInternalDto; -import mate.academy.rickandmorty.service.CharacterService; +import mate.academy.rickandmorty.service.character.CharacterService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; diff --git a/src/main/java/mate/academy/rickandmorty/service/DataInitialization.java b/src/main/java/mate/academy/rickandmorty/service/DataInitialization.java index 37b5dede..296f9754 100644 --- a/src/main/java/mate/academy/rickandmorty/service/DataInitialization.java +++ b/src/main/java/mate/academy/rickandmorty/service/DataInitialization.java @@ -5,6 +5,7 @@ import lombok.RequiredArgsConstructor; import mate.academy.rickandmorty.mapper.CharacterMapper; import mate.academy.rickandmorty.model.Character; +import mate.academy.rickandmorty.service.character.CharacterService; import org.springframework.stereotype.Component; @Component diff --git a/src/main/java/mate/academy/rickandmorty/service/RickAndMortyClient.java b/src/main/java/mate/academy/rickandmorty/service/RickAndMortyClient.java index 5f30dbe1..0b117757 100644 --- a/src/main/java/mate/academy/rickandmorty/service/RickAndMortyClient.java +++ b/src/main/java/mate/academy/rickandmorty/service/RickAndMortyClient.java @@ -6,6 +6,7 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.util.ArrayList; import java.util.List; import lombok.RequiredArgsConstructor; import mate.academy.rickandmorty.dto.external.CharacterExternalDto; @@ -15,25 +16,31 @@ @Component @RequiredArgsConstructor public class RickAndMortyClient { - private static final String url = "https://rickandmortyapi.com/api/character"; + private static final String basicUrl = "https://rickandmortyapi.com/api/character?page=%d"; private final ObjectMapper objectMapper; public List getCharacters() { + List charactersFromApi = new ArrayList<>(); HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest httpRequest = HttpRequest.newBuilder() - .GET() - .uri(URI.create(url)) - .build(); + for (int i = 1; i <= 42; i++) { + String url = String.format(basicUrl, i); + HttpRequest httpRequest = HttpRequest.newBuilder() + .GET() + .uri(URI.create(url)) + .build(); - try { - HttpResponse response = - httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); - CharacterResponseDataDto dataDto = - objectMapper.readValue(response.body(), CharacterResponseDataDto.class); - return dataDto.getResults(); - } catch (IOException | InterruptedException e) { - throw new RuntimeException(e); + try { + HttpResponse response = + httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); + CharacterResponseDataDto dataDto = + objectMapper.readValue(response.body(), CharacterResponseDataDto.class); + System.out.println(dataDto.getResults()); + charactersFromApi.addAll(dataDto.getResults()); + } catch (IOException | InterruptedException e) { + throw new RuntimeException(e); + } } + return charactersFromApi; } } diff --git a/src/main/java/mate/academy/rickandmorty/service/CharacterService.java b/src/main/java/mate/academy/rickandmorty/service/character/CharacterService.java similarity index 86% rename from src/main/java/mate/academy/rickandmorty/service/CharacterService.java rename to src/main/java/mate/academy/rickandmorty/service/character/CharacterService.java index ccf52e78..cf95a33a 100644 --- a/src/main/java/mate/academy/rickandmorty/service/CharacterService.java +++ b/src/main/java/mate/academy/rickandmorty/service/character/CharacterService.java @@ -1,4 +1,4 @@ -package mate.academy.rickandmorty.service; +package mate.academy.rickandmorty.service.character; import java.util.List; import mate.academy.rickandmorty.dto.internal.CharacterInternalDto; diff --git a/src/main/java/mate/academy/rickandmorty/service/CharacterServiceImpl.java b/src/main/java/mate/academy/rickandmorty/service/character/CharacterServiceImpl.java similarity index 96% rename from src/main/java/mate/academy/rickandmorty/service/CharacterServiceImpl.java rename to src/main/java/mate/academy/rickandmorty/service/character/CharacterServiceImpl.java index 662a0d74..2761c905 100644 --- a/src/main/java/mate/academy/rickandmorty/service/CharacterServiceImpl.java +++ b/src/main/java/mate/academy/rickandmorty/service/character/CharacterServiceImpl.java @@ -1,4 +1,4 @@ -package mate.academy.rickandmorty.service; +package mate.academy.rickandmorty.service.character; import java.util.List; import java.util.Random;