aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils/Http.java
diff options
context:
space:
mode:
authormsg-programs <msgdoesstuff@gmail.com>2023-10-31 21:03:42 +0100
committermsg-programs <msgdoesstuff@gmail.com>2023-10-31 21:03:42 +0100
commitd560c4611e603fa9e72ff6842bc14518d7bdbd63 (patch)
tree36eb1e24443bc9d9fbba51e14f12e5aacfac935c /src/main/java/de/hysky/skyblocker/utils/Http.java
parent1df4ef827d8a2e2fcc3767c1f5bf961f16b7fa19 (diff)
parent5bb91104d3275283d7479f0b35c1b18be470d632 (diff)
downloadSkyblocker-d560c4611e603fa9e72ff6842bc14518d7bdbd63.tar.gz
Skyblocker-d560c4611e603fa9e72ff6842bc14518d7bdbd63.tar.bz2
Skyblocker-d560c4611e603fa9e72ff6842bc14518d7bdbd63.zip
Merge branch 'master' of https://github.com/SkyblockerMod/Skyblocker into cleanup-2
# Conflicts: # src/main/java/de/hysky/skyblocker/config/SkyblockerConfig.java # src/main/java/de/hysky/skyblocker/utils/Http.java # src/main/java/de/hysky/skyblocker/utils/render/title/TitleContainerConfigScreen.java Pull changes from upstream master
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils/Http.java')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/Http.java68
1 files changed, 62 insertions, 6 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/Http.java b/src/main/java/de/hysky/skyblocker/utils/Http.java
index e0b9ecf8..4c8a3ada 100644
--- a/src/main/java/de/hysky/skyblocker/utils/Http.java
+++ b/src/main/java/de/hysky/skyblocker/utils/Http.java
@@ -5,6 +5,7 @@ import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.http.HttpClient;
+import java.net.http.HttpClient.Redirect;
import java.net.http.HttpClient.Version;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
@@ -15,6 +16,8 @@ import java.time.Duration;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
+import org.jetbrains.annotations.NotNull;
+
import de.hysky.skyblocker.SkyblockerMod;
import net.minecraft.SharedConstants;
@@ -22,12 +25,19 @@ import net.minecraft.SharedConstants;
* @implNote All http requests are sent using HTTP 2
*/
public class Http {
+ private static final String NAME_2_UUID = "https://api.minecraftservices.com/minecraft/profile/lookup/name/";
+ private static final String HYPIXEL_PROXY = "https://hysky.de/api/hypixel/";
private static final String USER_AGENT = "Skyblocker/" + SkyblockerMod.VERSION + " (" + SharedConstants.getGameVersion().getName() + ")";
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
+ .followRedirects(Redirect.NORMAL)
.build();
public static String sendGetRequest(String url) throws IOException, InterruptedException {
+ return sendCacheableGetRequest(url).content();
+ }
+
+ private static ApiResponse sendCacheableGetRequest(String url) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.GET()
.header("Accept", "application/json")
@@ -39,9 +49,11 @@ public class Http {
HttpResponse<InputStream> response = HTTP_CLIENT.send(request, BodyHandlers.ofInputStream());
InputStream decodedInputStream = getDecodedInputStream(response);
+
String body = new String(decodedInputStream.readAllBytes());
+ HttpHeaders headers = response.headers();
- return body;
+ return new ApiResponse(body, response.statusCode(), getCacheStatus(headers), getAge(headers));
}
public static HttpHeaders sendHeadRequest(String url) throws IOException, InterruptedException {
@@ -52,13 +64,26 @@ public class Http {
.uri(URI.create(url))
.build();
- HttpResponse<Void> response = HTTP_CLIENT.send(request, BodyHandlers.discarding());
+ HttpResponse<Void> response = HTTP_CLIENT.send(request, BodyHandlers.discarding());
return response.headers();
}
- private static InputStream getDecodedInputStream(HttpResponse<InputStream> response) {
- String encoding = getContentEncoding(response);
+ public static ApiResponse sendName2UuidRequest(String name) throws IOException, InterruptedException {
+ return sendCacheableGetRequest(NAME_2_UUID + name);
+ }
+ /**
+ * @param endpoint the endpoint - do not include any leading or trailing slashes
+ * @param query the query string - use empty string if n/a
+ * @return the requested data with zero pre-processing applied
+ */
+ public static ApiResponse sendHypixelRequest(String endpoint, @NotNull String query) throws IOException, InterruptedException {
+ return sendCacheableGetRequest(HYPIXEL_PROXY + endpoint + query);
+ }
+
+ private static InputStream getDecodedInputStream(HttpResponse<InputStream> response) {
+ String encoding = getContentEncoding(response.headers());
+
try {
switch (encoding) {
case "":
@@ -75,8 +100,8 @@ public class Http {
}
}
- private static String getContentEncoding(HttpResponse<InputStream> response) {
- return response.headers().firstValue("Content-Encoding").orElse("");
+ private static String getContentEncoding(HttpHeaders headers) {
+ return headers.firstValue("Content-Encoding").orElse("");
}
public static String getEtag(HttpHeaders headers) {
@@ -86,4 +111,35 @@ public class Http {
public static String getLastModified(HttpHeaders headers) {
return headers.firstValue("Last-Modified").orElse("");
}
+
+ /**
+ * Returns the cache status of the resource
+ *
+ * @see <a href="https://developers.cloudflare.com/cache/concepts/default-cache-behavior/#cloudflare-cache-responses">Cloudflare Cache Docs</a>
+ */
+ private static String getCacheStatus(HttpHeaders headers) {
+ return headers.firstValue("CF-Cache-Status").orElse("UNKNOWN");
+ }
+
+ private static int getAge(HttpHeaders headers) {
+ return Integer.parseInt(headers.firstValue("Age").orElse("-1"));
+ }
+
+ //TODO If ever needed, we could just replace cache status with the response headers and go from there
+ public record ApiResponse(String content, int statusCode, String cacheStatus, int age) implements AutoCloseable {
+
+ public boolean ok() {
+ return statusCode == 200;
+ }
+
+ public boolean cached() {
+ return cacheStatus.equals("HIT");
+ }
+
+ @Override
+ public void close() {
+ //Allows for nice syntax when dealing with api requests in try catch blocks
+ //Maybe one day we'll have some resources to free
+ }
+ }
}