aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/Danker/handlers/APIHandler.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/me/Danker/handlers/APIHandler.java')
-rw-r--r--src/main/java/me/Danker/handlers/APIHandler.java41
1 files changed, 36 insertions, 5 deletions
diff --git a/src/main/java/me/Danker/handlers/APIHandler.java b/src/main/java/me/Danker/handlers/APIHandler.java
index c9460fe..3c3d9b2 100644
--- a/src/main/java/me/Danker/handlers/APIHandler.java
+++ b/src/main/java/me/Danker/handlers/APIHandler.java
@@ -8,6 +8,12 @@ import me.Danker.DankersSkyblockMod;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ChatComponentText;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.IOException;
@@ -15,19 +21,21 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class APIHandler {
- public static JsonObject getResponse(String urlString) {
+ public static JsonObject getResponse(String urlString, boolean hasError) {
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
+ conn.setRequestProperty("User-Agent", "Dsm/1.0");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
- BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+ BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String input;
StringBuilder response = new StringBuilder();
@@ -40,7 +48,7 @@ public class APIHandler {
return gson.fromJson(response.toString(), JsonObject.class);
} else {
- if (urlString.startsWith("https://api.hypixel.net/")) {
+ if (hasError) {
InputStream errorStream = conn.getErrorStream();
try (Scanner scanner = new Scanner(errorStream)) {
scanner.useDelimiter("\\Z");
@@ -62,6 +70,29 @@ public class APIHandler {
return new JsonObject();
}
+
+ public static JsonObject getResponsePOST(String urlString, JsonObject body, boolean hasError) throws IOException {
+ EntityPlayer player = Minecraft.getMinecraft().thePlayer;
+
+ CloseableHttpClient httpClient = HttpClientBuilder.create().build();
+
+ try {
+ HttpPost req = new HttpPost(urlString);
+ StringEntity params = new StringEntity(body.toString());
+ req.addHeader("content-type", "application/json");
+ req.setEntity(params);
+ HttpResponse response = httpClient.execute(req);
+
+ return new Gson().fromJson(EntityUtils.toString(response.getEntity(), "UTF-8"), JsonObject.class);
+ } catch (Exception ex) {
+ player.addChatMessage(new ChatComponentText(DankersSkyblockMod.ERROR_COLOUR + "An error has occured. See logs for more details."));
+ ex.printStackTrace();
+ } finally {
+ httpClient.close();
+ }
+
+ return new JsonObject();
+ }
// Only used for UUID => Username
public static JsonArray getArrayResponse(String urlString) {
@@ -97,7 +128,7 @@ public class APIHandler {
}
public static String getUUID(String username) {
- JsonObject uuidResponse = getResponse("https://api.mojang.com/users/profiles/minecraft/" + username);
+ JsonObject uuidResponse = getResponse("https://api.mojang.com/users/profiles/minecraft/" + username, false);
return uuidResponse.get("id").getAsString();
}
@@ -107,7 +138,7 @@ public class APIHandler {
// Get profiles
System.out.println("Fetching profiles...");
- JsonObject profilesResponse = getResponse("https://api.hypixel.net/skyblock/profiles?uuid=" + UUID + "&key=" + key);
+ JsonObject profilesResponse = getResponse("https://api.hypixel.net/skyblock/profiles?uuid=" + UUID + "&key=" + key, true);
if (!profilesResponse.get("success").getAsBoolean()) {
String reason = profilesResponse.get("cause").getAsString();
player.addChatMessage(new ChatComponentText(DankersSkyblockMod.ERROR_COLOUR + "Failed with reason: " + reason));