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;
class ErroConsultaGitHubException extends RuntimeException {
private String message;
public ErroConsultaGitHubException(String message) {
this.message = message;
}
@Override
public String getMessage() {
return "Erro ao consultar o GitHub: " + message;
}
}
public class ConsultaGitHub {
public static void main(String[] args) {
Scanner leitura = new Scanner(System.in);
System.out.println("Digite o nome de usuário do GitHub para consultar informações: ");
String username = leitura.nextLine();
String endereco = "https://api.github.com/users/" + username;
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endereco))
.header("Accept", "application/vnd.github.v3+json")
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 404) {
throw new ErroConsultaGitHubException("Usuário não encontrado no GitHub.");
}
String resposta = response.body();
System.out.println(resposta);
} catch (IOException | InterruptedException e) {
System.out.println("Houve um erro durante a consulta à API do GitHub.");
e.printStackTrace();
} catch (ErroConsultaGitHubException e) {
System.out.println(e.getMessage());
}
}
}