less jank web requests to the mojang api
All checks were successful
deploy / deploy (push) Successful in 43s

This commit is contained in:
Lee 2024-04-06 21:40:03 +01:00
parent cda40a07d0
commit efb3281818
5 changed files with 51 additions and 31 deletions

@ -82,6 +82,12 @@
<version>0.5.11</version> <version>0.5.11</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice @ControllerAdvice
public final class ExceptionControllerAdvice { public final class ExceptionControllerAdvice {
/** /**
* Handle a raised exception. * Handle a raised exception.
* *

@ -1,18 +1,13 @@
package cc.fascinated.mojang; package cc.fascinated.mojang;
import cc.fascinated.Main;
import cc.fascinated.mojang.types.MojangApiProfile; import cc.fascinated.mojang.types.MojangApiProfile;
import cc.fascinated.mojang.types.MojangSessionServerProfile; import cc.fascinated.mojang.types.MojangSessionServerProfile;
import com.google.gson.reflect.TypeToken; import cc.fascinated.util.WebRequest;
import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.net.URI; @Service @Log4j2
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
@Service
public class MojangAPIService { public class MojangAPIService {
@Value("${mojang.session-server}") @Value("${mojang.session-server}")
@ -27,18 +22,8 @@ public class MojangAPIService {
* @param id the uuid or name of the player * @param id the uuid or name of the player
* @return the profile * @return the profile
*/ */
@SneakyThrows
public MojangSessionServerProfile getSessionServerProfile(String id) { public MojangSessionServerProfile getSessionServerProfile(String id) {
HttpRequest request = HttpRequest.newBuilder() return WebRequest.get(mojangSessionServerUrl + "/session/minecraft/profile/" + id);
.uri(new URI(mojangSessionServerUrl + "/session/minecraft/profile/" + id))
.GET()
.build();
HttpResponse<String> response = Main.getCLIENT().send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
return null;
}
return Main.getGSON().fromJson(response.body(), new TypeToken<MojangSessionServerProfile>(){}.getType());
} }
/** /**
@ -47,17 +32,7 @@ public class MojangAPIService {
* @param id the name of the player * @param id the name of the player
* @return the profile * @return the profile
*/ */
@SneakyThrows
public MojangApiProfile getApiProfile(String id) { public MojangApiProfile getApiProfile(String id) {
HttpRequest request = HttpRequest.newBuilder() return WebRequest.get(mojangApiUrl + "/users/profiles/minecraft/" + id);
.uri(new URI(mojangApiUrl + "/users/profiles/minecraft/" + id))
.GET()
.build();
HttpResponse<String> response = Main.getCLIENT().send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
return null;
}
return Main.getGSON().fromJson(response.body(), new TypeToken<MojangApiProfile>(){}.getType());
} }
} }

@ -5,7 +5,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter; import lombok.Getter;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;

@ -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 <T> the type of the response
*/
public static <T> T get(String url) {
try {
ResponseEntity<T> profile = CLIENT.get()
.uri(url)
.retrieve()
.toEntity((Class<T>) Object.class);
if (profile.getStatusCode().isError()) {
return null;
}
return profile.getBody();
} catch (HttpClientErrorException ex) {
return null;
}
}
}