Skip to content

Commit

Permalink
corrected getCharacters method
Browse files Browse the repository at this point in the history
  • Loading branch information
nshtykh committed Feb 14, 2024
1 parent 15f85f1 commit b08ae0c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CharacterExternalDto> getCharacters() {
List<CharacterExternalDto> 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<String> 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<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mate.academy.rickandmorty.service;
package mate.academy.rickandmorty.service.character;

import java.util.List;
import java.util.Random;
Expand Down

0 comments on commit b08ae0c

Please sign in to comment.