aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils/Http.java
diff options
context:
space:
mode:
authorAaron <51387595+AzureAaron@users.noreply.github.com>2023-10-31 01:08:28 -0400
committerGitHub <noreply@github.com>2023-10-31 01:08:28 -0400
commit5bb91104d3275283d7479f0b35c1b18be470d632 (patch)
treefd19087bdc6ffdcecfbb492763efa65fb52379cc /src/main/java/de/hysky/skyblocker/utils/Http.java
parent1389107c648a5b1d8395d6629cf8328a3ca67324 (diff)
downloadSkyblocker-5bb91104d3275283d7479f0b35c1b18be470d632.tar.gz
Skyblocker-5bb91104d3275283d7479f0b35c1b18be470d632.tar.bz2
Skyblocker-5bb91104d3275283d7479f0b35c1b18be470d632.zip
Player Secrets Tracker (#394)
* Player Secrets Tracker * Refactor SecretsTracker --------- Co-authored-by: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils/Http.java')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/Http.java22
1 files changed, 17 insertions, 5 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/Http.java b/src/main/java/de/hysky/skyblocker/utils/Http.java
index eabb02e4..573c3458 100644
--- a/src/main/java/de/hysky/skyblocker/utils/Http.java
+++ b/src/main/java/de/hysky/skyblocker/utils/Http.java
@@ -49,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 new ApiResponse(body, response.statusCode(), getCacheStatus(response.headers()));
+ return new ApiResponse(body, response.statusCode(), getCacheStatus(headers), getAge(headers));
}
public static HttpHeaders sendHeadRequest(String url) throws IOException, InterruptedException {
@@ -66,8 +68,8 @@ public class Http {
return response.headers();
}
- public static String sendName2UuidRequest(String name) throws IOException, InterruptedException {
- return sendGetRequest(NAME_2_UUID + name);
+ public static ApiResponse sendName2UuidRequest(String name) throws IOException, InterruptedException {
+ return sendCacheableGetRequest(NAME_2_UUID + name);
}
/**
@@ -115,12 +117,16 @@ public class Http {
*
* @see <a href="https://developers.cloudflare.com/cache/concepts/default-cache-behavior/#cloudflare-cache-responses">Cloudflare Cache Docs</a>
*/
- public static String getCacheStatus(HttpHeaders headers) {
+ 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) {
+ public record ApiResponse(String content, int statusCode, String cacheStatus, int age) implements AutoCloseable {
public boolean ok() {
return statusCode == 200;
@@ -129,5 +135,11 @@ public class Http {
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
+ }
}
}