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

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