diff options
author | Aaron <51387595+AzureAaron@users.noreply.github.com> | 2024-06-22 02:47:39 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-22 02:47:39 -0400 |
commit | d12504ef9992a72de9eb3c3a930c9f3c855c0acc (patch) | |
tree | 840ac5bc666da72b26f104b088eb95da5306065e /src/main/java/de/hysky/skyblocker/utils/Http.java | |
parent | d6b220a8e42a1fc2dbc955779e86d199851b4674 (diff) | |
parent | 7e3ed3e5ca248434b61e876df227ec4ea72b46a2 (diff) | |
download | Skyblocker-d12504ef9992a72de9eb3c3a930c9f3c855c0acc.tar.gz Skyblocker-d12504ef9992a72de9eb3c3a930c9f3c855c0acc.tar.bz2 Skyblocker-d12504ef9992a72de9eb3c3a930c9f3c855c0acc.zip |
Merge pull request #783 from SkyblockerMod/api-changes
Api changes the Aaron of the Azure color made
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils/Http.java')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/utils/Http.java | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/Http.java b/src/main/java/de/hysky/skyblocker/utils/Http.java index 871eac78..99db0316 100644 --- a/src/main/java/de/hysky/skyblocker/utils/Http.java +++ b/src/main/java/de/hysky/skyblocker/utils/Http.java @@ -17,6 +17,7 @@ import java.util.zip.GZIPInputStream; import java.util.zip.InflaterInputStream; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import de.hysky.skyblocker.SkyblockerMod; import net.minecraft.SharedConstants; @@ -33,15 +34,18 @@ public class Http { .followRedirects(Redirect.NORMAL) .build(); - private static ApiResponse sendCacheableGetRequest(String url) throws IOException, InterruptedException { - HttpRequest request = HttpRequest.newBuilder() + private static ApiResponse sendCacheableGetRequest(String url, @Nullable String token) throws IOException, InterruptedException { + HttpRequest.Builder requestBuilder = HttpRequest.newBuilder() .GET() .header("Accept", "application/json") .header("Accept-Encoding", "gzip, deflate") .header("User-Agent", USER_AGENT) .version(Version.HTTP_2) - .uri(URI.create(url)) - .build(); + .uri(URI.create(url)); + + if (token != null) requestBuilder.header("Token", token); + + HttpRequest request = requestBuilder.build(); HttpResponse<InputStream> response = HTTP_CLIENT.send(request, BodyHandlers.ofInputStream()); InputStream decodedInputStream = getDecodedInputStream(response); @@ -69,7 +73,7 @@ public class Http { } public static String sendGetRequest(String url) throws IOException, InterruptedException { - return sendCacheableGetRequest(url).content(); + return sendCacheableGetRequest(url, null).content(); } public static HttpHeaders sendHeadRequest(String url) throws IOException, InterruptedException { @@ -84,8 +88,26 @@ public class Http { return response.headers(); } + public static String sendPostRequest(String url, String requestBody, String contentType) throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .POST(BodyPublishers.ofString(requestBody)) + .header("Accept-Encoding", "gzip, deflate") + .header("Content-Type", contentType) + .header("User-Agent", USER_AGENT) + .version(Version.HTTP_2) + .uri(URI.create(url)) + .build(); + + HttpResponse<InputStream> response = HTTP_CLIENT.send(request, BodyHandlers.ofInputStream()); + InputStream decodedInputStream = getDecodedInputStream(response); + + String responseBody = new String(decodedInputStream.readAllBytes()); + + return responseBody; + } + public static ApiResponse sendName2UuidRequest(String name) throws IOException, InterruptedException { - return sendCacheableGetRequest(NAME_2_UUID + name); + return sendCacheableGetRequest(NAME_2_UUID + name, null); } /** @@ -96,7 +118,7 @@ public class Http { * @implNote the {@code v2} prefix is automatically added */ public static ApiResponse sendHypixelRequest(String endpoint, @NotNull String query) throws IOException, InterruptedException { - return sendCacheableGetRequest(HYPIXEL_PROXY + endpoint + query); + return sendCacheableGetRequest(HYPIXEL_PROXY + endpoint + query, ApiAuthentication.getToken()); } private static InputStream getDecodedInputStream(HttpResponse<InputStream> response) { |