Created
September 12, 2023 23:43
-
-
Save kelciocajueiro/23118ed225a81dcd4dbb1d8db33d1e9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.contributetocommunity.api; | |
import com.google.gson.Gson; | |
import org.apache.commons.lang3.StringUtils; | |
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 WhoIsApiConsumer { | |
public static String APIExplorerJava(String ipAddress) { | |
String uri = String.format("https://ipwho.is/%s", ipAddress); | |
String response = ""; | |
HttpClient httpClient = HttpClient.newHttpClient(); | |
HttpRequest httpRequest = HttpRequest.newBuilder(URI.create(uri)).build(); | |
try { | |
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); | |
if (httpResponse.statusCode() == 200) { | |
Gson gson = new Gson(); | |
IPInfo ipInfo = gson.fromJson(httpResponse.body(), IPInfo.class); | |
if (Boolean.parseBoolean(ipInfo.getSuccess())) { | |
response = String.format("IP %s is located in %s and belongs to %s", ipAddress, ipInfo.getCountry(), ipInfo.getConnection().getIsp()) | |
.replace("\"", ""); | |
} else { | |
response = FailureCases.from(ipInfo.getMessage()).getApiErrorMessage(ipAddress); | |
} | |
} else { | |
response = String.format("Failure: HTTP request failed with status code %d", httpResponse.statusCode()); | |
} | |
} catch (IOException | InterruptedException ex) { | |
System.out.println("Failure: " + ex.getMessage()); | |
} | |
return response; | |
} | |
private enum FailureCases { | |
INVALID("Invalid IP address") { | |
@Override | |
String getApiErrorMessage(String ipAddress) { | |
return "Failure: The provided IP " + ipAddress + " is invalid"; | |
} | |
}, | |
PRIVATE("Reserved range") { | |
@Override | |
String getApiErrorMessage(String ipAddress) { | |
return "Failure: The provided IP " + ipAddress + " is private"; | |
} | |
}; | |
private final String clientMessage; | |
FailureCases(String clientMessage) { | |
this.clientMessage = clientMessage; | |
} | |
abstract String getApiErrorMessage(String ipAddress); | |
public static FailureCases from(String value) { | |
return Arrays.stream(FailureCases.values()) | |
.filter(type -> StringUtils.equalsIgnoreCase(type.clientMessage, value)) | |
.findFirst().orElse(INVALID); | |
} | |
} | |
private class IPInfo { | |
private String ip; | |
private String country; | |
private String success; | |
private String message; | |
private Connection connection; | |
public String getIp() { | |
return ip; | |
} | |
public void setIp(String ip) { | |
this.ip = ip; | |
} | |
public String getSuccess() { | |
return success; | |
} | |
public String getCountry() { | |
return country; | |
} | |
public void setCountry(String country) { | |
this.country = country; | |
} | |
public void setSuccess(String success) { | |
this.success = success; | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
public Connection getConnection() { | |
return connection; | |
} | |
public void setConnection(Connection connection) { | |
this.connection = connection; | |
} | |
} | |
private class Connection { | |
private String isp; | |
public String getIsp() { | |
return isp; | |
} | |
public void setIsp(String isp) { | |
this.isp = isp; | |
} | |
} | |
public static void main (String[] args) { | |
Scanner s = new Scanner(System.in); | |
System.out.print(APIExplorerJava(s.nextLine())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment