Olá Rede, desejo compartilhar essa minha resolução do desafio para registro de progresso e tanto para ajudar aqueles que tiveram soluções diferentes e queira implementar ou melhorar o próprio código.
Fico aberto a sugestões de melhorias e feedback.
Github API
- Classe executável
import com.google.gson.Gson;
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.Arrays;
import java.util.Scanner;
public class GithubAPI {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Digite o nome de usuário do Github para consultar informações:");
String user = input.nextLine();
String endereco = "https://api.github.com/users/" + user;
System.out.println(endereco);
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endereco))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
// 404: página não encontrada
if (response.statusCode() == 404) {
throw new ErrorConsultaGithubException("Error: usuário não encontrado.");
}
String json = response.body();
System.out.println(json);
Gson gson = new Gson();
GithubStatusInfo statusInfo = gson.fromJson(json, GithubStatusInfo.class);
GithubStatus account = new GithubStatus(statusInfo);
System.out.println(account);
} catch (IOException | InterruptedException | IllegalArgumentException e) {
System.out.println("Error: durante a consulta à API do GitHub");
System.out.println(Arrays.toString(e.getStackTrace()));
} catch (ErrorConsultaGithubException e) {
System.out.println(e.getMessage());
} finally {
System.out.println("Operação finalizada!");
}
}
}
GithubStatus
- SuperClasse
public class GithubStatus {
private String name;
private String login;
private String bio;
private int followers;
private int following;
private int repos;
public GithubStatus(String login) {
this.login = login;
}
public GithubStatus(GithubStatusInfo statusInfo) {
this.name = statusInfo.name();
this.login = statusInfo.login();
this.bio = statusInfo.bio();
this.followers = Integer.valueOf(statusInfo.followers());
this.following = Integer.valueOf(statusInfo.following());
this.repos = Integer.valueOf(statusInfo.repos());
}
public String getName() {
return name;
}
public String getLogin() {
return login;
}
public String getBio() {
return bio;
}
public int getFollowers() {
return followers;
}
public int getFollowing() {
return following;
}
public int getRepos() {
return repos;
}
@Override
public String toString() {
return "Name: " + name + " (" + login + ")\nBio: " + bio + "\nFollowers: " + followers + "\nFollowing: " + following;
}
}
GithubStatusInfo
- Classe Record
public record GithubStatusInfo(String name, String login, String bio, int followers, int following, int repos) {
}
Exemplo de Exibição do console:
Digite o nome de usuário do Github para consultar informações:
D4nN3t0
https://api.github.com/users/D4nN3t0
// json omitido
Name: Daniel Avelino (D4nN3t0)
Bio: Noções de programação web e IA, inglês Intermediário, desenvolvedor em formação.
Followers: 1
Following: 4
Operação finalizada!
Vamos nos conectar!
Link do Github: https://github.com/D4nN3t0