Quando o programa encontra um usuário do GitHub, ele devolve os valores "nome" e "id" como "null"
Saída:
Digite o usuário do GitHub para consulta: f3l1pe-augusto
JSON: {"login":"f3l1pe-augusto","id":137817308,"node_id":"U_kgDOCDbs3A","avatar_url":"https://avatars.githubusercontent.com/u/137817308?v=4","gravatar_id":"","url":"https://api.github.com/users/f3l1pe-augusto","html_url":"https://github.com/f3l1pe-augusto","followers_url":"https://api.github.com/users/f3l1pe-augusto/followers","following_url":"https://api.github.com/users/f3l1pe-augusto/following{/other_user}","gists_url":"https://api.github.com/users/f3l1pe-augusto/gists{/gist_id}","starred_url":"https://api.github.com/users/f3l1pe-augusto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/f3l1pe-augusto/subscriptions","organizations_url":"https://api.github.com/users/f3l1pe-augusto/orgs","repos_url":"https://api.github.com/users/f3l1pe-augusto/repos","events_url":"https://api.github.com/users/f3l1pe-augusto/events{/privacy}","received_events_url":"https://api.github.com/users/f3l1pe-augusto/received_events","type":"User","site_admin":false,"name":"Felipe","company":null,"blog":"","location":null,"email":null,"hireable":null,"bio":"Estudante de Sistemas de Informação","twitter_username":null,"public_repos":7,"public_gists":0,"followers":1,"following":2,"created_at":"2023-06-26T16:56:12Z","updated_at":"2023-10-12T18:21:02Z"}
USUÁRIO: Nome: null ID: null
Programa finalizado!
Main:
package br.com.fakecompany.main;
import br.com.fakecompany.exception.UserGitHubNotFoundException;
import br.com.fakecompany.models.Usuario;
import br.com.fakecompany.models.UsuarioGitHub;
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 Main {
public static void main(String[] args) throws IOException, InterruptedException {
Scanner scanner = new Scanner(System.in);
System.out.println("Digite o usuário do GitHub para consulta: ");
String username = scanner.nextLine();
String address = "https://api.github.com/users/" + username;
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(address))
.header("Accept", "application/vnd.github+json")
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 404) {
throw new UserGitHubNotFoundException("Usuário do GitHub não encontrado!");
}
String json = response.body();
System.out.println("JSON: " + json);
Gson gson = new GsonBuilder().
setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
UsuarioGitHub usuarioGitHub = gson.fromJson(json, UsuarioGitHub.class);
System.out.println("\nUSUÁRIO GITHUB: " + usuarioGitHub);
Usuario usuario = new Usuario(usuarioGitHub);
System.out.println("\nUSUÁRIO: " + usuario);
} catch (IOException | InterruptedException e) {
System.out.println("\nOps... Houve um erro durante a consulta à API do GitHub.");
e.printStackTrace();
} catch (UserGitHubNotFoundException e) {
System.out.println(e.getMessage());
} finally {
System.out.println("\nPrograma finalizado!");
}
}
}
Usuário:
package br.com.fakecompany.models;
public class Usuario {
private String name;
private String id;
public Usuario (UsuarioGitHub usuarioGitHub) {
this.name = usuarioGitHub.login();
this.id = usuarioGitHub.id();
}
@Override
public String toString() {
return "Nome: " + name + "\nID: " + id;
}
}
UsuárioGithub:
package br.com.fakecompany.models;
public class Usuario {
private String name;
private String id;
public Usuario (UsuarioGitHub usuarioGitHub) {
this.name = usuarioGitHub.login();
this.id = usuarioGitHub.id();
}
@Override
public String toString() {
return "Nome: " + name + "\nID: " + id;
}
}
UserGitHubNotFoundException:
package br.com.fakecompany.exception;
public class UserGitHubNotFoundException extends RuntimeException {
private String message;
public UserGitHubNotFoundException(String message) {
this.message = message;
}
@Override
public String getMessage() {
return this.message;
}
}