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

View File

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

View File

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

View File

@ -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<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());
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<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());
return WebRequest.get(mojangApiUrl + "/users/profiles/minecraft/" + id);
}
}

View File

@ -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;

View File

@ -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;
}
}
}