From efb3281818a2888f30a068f001fef79834dd6037 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 6 Apr 2024 21:40:03 +0100 Subject: [PATCH] less jank web requests to the mojang api --- pom.xml | 6 +++ .../controller/ExceptionControllerAdvice.java | 1 + .../fascinated/mojang/MojangAPIService.java | 35 +++-------------- .../java/cc/fascinated/player/impl/Skin.java | 1 - .../java/cc/fascinated/util/WebRequest.java | 39 +++++++++++++++++++ 5 files changed, 51 insertions(+), 31 deletions(-) create mode 100644 src/main/java/cc/fascinated/util/WebRequest.java diff --git a/pom.xml b/pom.xml index 276a805..477ae73 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,12 @@ 0.5.11 compile + + org.apache.httpcomponents.client5 + httpclient5 + 5.3.1 + compile + \ No newline at end of file diff --git a/src/main/java/cc/fascinated/api/controller/ExceptionControllerAdvice.java b/src/main/java/cc/fascinated/api/controller/ExceptionControllerAdvice.java index c601e10..9c68098 100644 --- a/src/main/java/cc/fascinated/api/controller/ExceptionControllerAdvice.java +++ b/src/main/java/cc/fascinated/api/controller/ExceptionControllerAdvice.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; @ControllerAdvice public final class ExceptionControllerAdvice { + /** * Handle a raised exception. * diff --git a/src/main/java/cc/fascinated/mojang/MojangAPIService.java b/src/main/java/cc/fascinated/mojang/MojangAPIService.java index a1eb60c..b3aaa37 100644 --- a/src/main/java/cc/fascinated/mojang/MojangAPIService.java +++ b/src/main/java/cc/fascinated/mojang/MojangAPIService.java @@ -1,18 +1,13 @@ package cc.fascinated.mojang; -import cc.fascinated.Main; import cc.fascinated.mojang.types.MojangApiProfile; import cc.fascinated.mojang.types.MojangSessionServerProfile; -import com.google.gson.reflect.TypeToken; -import lombok.SneakyThrows; +import cc.fascinated.util.WebRequest; +import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import java.net.URI; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; - -@Service +@Service @Log4j2 public class MojangAPIService { @Value("${mojang.session-server}") @@ -27,18 +22,8 @@ public class MojangAPIService { * @param id the uuid or name of the player * @return the profile */ - @SneakyThrows public MojangSessionServerProfile getSessionServerProfile(String id) { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI(mojangSessionServerUrl + "/session/minecraft/profile/" + id)) - .GET() - .build(); - - HttpResponse response = Main.getCLIENT().send(request, HttpResponse.BodyHandlers.ofString()); - if (response.statusCode() != 200) { - return null; - } - return Main.getGSON().fromJson(response.body(), new TypeToken(){}.getType()); + return WebRequest.get(mojangSessionServerUrl + "/session/minecraft/profile/" + id); } /** @@ -47,17 +32,7 @@ public class MojangAPIService { * @param id the name of the player * @return the profile */ - @SneakyThrows public MojangApiProfile getApiProfile(String id) { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI(mojangApiUrl + "/users/profiles/minecraft/" + id)) - .GET() - .build(); - - HttpResponse response = Main.getCLIENT().send(request, HttpResponse.BodyHandlers.ofString()); - if (response.statusCode() != 200) { - return null; - } - return Main.getGSON().fromJson(response.body(), new TypeToken(){}.getType()); + return WebRequest.get(mojangApiUrl + "/users/profiles/minecraft/" + id); } } diff --git a/src/main/java/cc/fascinated/player/impl/Skin.java b/src/main/java/cc/fascinated/player/impl/Skin.java index b7e9fa4..eb227bf 100644 --- a/src/main/java/cc/fascinated/player/impl/Skin.java +++ b/src/main/java/cc/fascinated/player/impl/Skin.java @@ -5,7 +5,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Getter; import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; -import org.apache.tomcat.util.http.fileupload.IOUtils; import java.io.InputStream; import java.net.URI; diff --git a/src/main/java/cc/fascinated/util/WebRequest.java b/src/main/java/cc/fascinated/util/WebRequest.java new file mode 100644 index 0000000..05d7b50 --- /dev/null +++ b/src/main/java/cc/fascinated/util/WebRequest.java @@ -0,0 +1,39 @@ +package cc.fascinated.util; + +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestClient; + +public class WebRequest { + + /** + * The web client. + */ + private static final RestClient CLIENT = RestClient.builder() + .requestFactory(new HttpComponentsClientHttpRequestFactory()) + .build(); + + /** + * Gets a response from the given URL. + * + * @param url the url + * @return the response + * @param the type of the response + */ + public static T get(String url) { + try { + ResponseEntity profile = CLIENT.get() + .uri(url) + .retrieve() + .toEntity((Class) Object.class); + + if (profile.getStatusCode().isError()) { + return null; + } + return profile.getBody(); + } catch (HttpClientErrorException ex) { + return null; + } + } +}