package br.com.alura.screenmatch.principal;
import br.com.alura.screenmatch.modelos.Books;
import br.com.alura.screenmatch.modelos.Livro;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;
public class SeachBook {
public static void main(String[] args) throws IOException, InterruptedException {
Scanner scanner = new Scanner(System.in);
System.out.println("Digite o Livro que estar Procurando: ");
var buscar = scanner.nextLine().trim();
System.out.println("Adicione a chave de validação");
var chave = scanner.nextLine().trim();
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://www.googleapis.com/books/v1/volumes?q=" + buscar +
"&key=" + chave))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
//System.out.println(response.body());
String json = response.body();
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy
.UPPER_CAMEL_CASE).create();
Livro livro = gson.fromJson(json, Livro.class);
System.out.println("Convertido");
Books meulivro = new Books(livro);
System.out.println(meulivro);
}
}
package br.com.alura.screenmatch.modelos;
import com.google.gson.annotations.SerializedName;
public class Books {
@SerializedName("title")
String titulo;
@SerializedName("authors")
String autor;
@SerializedName("publisher")
String editora;
@SerializedName("description")
String descricao;
public Books(String title, String authors, String publisher, String description) {
this.titulo = title;
this.autor = authors;
this.editora = publisher;
this.descricao = description;
}
public Books(Livro book) {
this.titulo = book.title();
this.autor = book.authors();
this.editora = book.publisher();
this.descricao = book.description();
}
public String getTitulo() {
return titulo;
}
public String getAutor() {
return autor;
}
public String getEditora() {
return editora;
}
public String getDescricao() {
return descricao;
}
@Override
public String toString() {
return "Books{" +
"titulo='" + titulo + '\'' +
", autor='" + autor + '\'' +
", editora='" + editora + '\'' +
", descricao='" + descricao + '\'' +
'}';
}
}
package br.com.alura.screenmatch.modelos;
public record Livro(String title, String authors, String publisher, String description) {
}