package br.com.alura.screenmatch.model;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record DadosEpisodio(@JsonAlias("Title") String titulo,
@JsonAlias("Episode") Integer numero,
@JsonAlias("imdbRating") String avaliacao,
@JsonAlias("Released") String dataLancamento) {
}
package br.com.alura.screenmatch.model;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record DadosSerie(@JsonAlias("Title") String titulo,
@JsonAlias("totalSeasons") Integer totalTemporadas,
@JsonAlias("imdbRating") String avaliacao) {
}
package br.com.alura.screenmatch.model;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true)
public record DadosTemporada(@JsonAlias("Season") Integer numero,
@JsonAlias("Episodes") List<DadosEpisodio> episodios) {
}
package br.com.alura.screenmatch.principal;
import...
public class Principal {
private Scanner scanner = new Scanner(System.in);
private ConsumoApi consumoApi = new ConsumoApi();
private ConverteDados conversor = new ConverteDados();
private final String ENDERECO = "https://www.omdbapi.com/?t=";
private final String API_KEY = "&apikey=f64fbab";
public void exibeMenu() {
System.out.println("Digite o nome da série para busca: ");
var nomeSerie = scanner.nextLine();
var json = consumoApi.obterDados(ENDERECO + nomeSerie.replace(" ", "+") + API_KEY);
DadosSerie dados = conversor.obterDados(json, DadosSerie.class);
System.out.println(dados);
List<DadosTemporada> temporadas = new ArrayList<>();
for (int i = 1; i <= dados.totalTemporadas(); i++) {
json = consumoApi.obterDados(ENDERECO + nomeSerie.replace(" ", "+") + "&season=" + i + API_KEY);
DadosTemporada dadosTemporada = conversor.obterDados(json, DadosTemporada.class);
temporadas.add(dadosTemporada);
}
temporadas.forEach(System.out::println);
temporadas.forEach(t -> t.episodios().forEach(e -> System.out.println(e.titulo())));
}
}
package br.com.alura.screenmatch.service;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class ConsumoApi {
public String obterDados(String endereco) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endereco))
.build();
HttpResponse<String> response = null;
try {
response = client
.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
String json = response.body();
return json;
}
}
package br.com.alura.screenmatch.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ConverteDados implements IConverteDados {
private ObjectMapper mapper = new ObjectMapper();
@Override
public <T> T obterDados(String json, Class<T> classe) {
try {
return mapper.readValue(json, classe);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
package br.com.alura.screenmatch.service;
public interface IConverteDados {
<T> T obterDados(String json, Class<T> classe);
}
package br.com.alura.screenmatch;
import br.com.alura.screenmatch.principal.Principal;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ScreenmatchApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ScreenmatchApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Principal principal = new Principal();
principal.exibeMenu();
}
}