diff --git a/fabric/src/main/java/net/smyler/terramap/TerramapFabricMod.java b/fabric/src/main/java/net/smyler/terramap/TerramapFabricMod.java index 05e2e312..a3502155 100644 --- a/fabric/src/main/java/net/smyler/terramap/TerramapFabricMod.java +++ b/fabric/src/main/java/net/smyler/terramap/TerramapFabricMod.java @@ -9,6 +9,7 @@ import net.smyler.smylib.json.TextJsonAdapter; import net.smyler.smylib.text.Text; import net.smyler.terramap.http.HttpClient; +import net.smyler.terramap.http.TerramapHttpClient; import org.apache.logging.log4j.Logger; import static org.apache.logging.log4j.LogManager.getLogger; @@ -23,11 +24,12 @@ public class TerramapFabricMod implements ModInitializer, Terramap { .registerTypeAdapter(Text.class, new TextJsonAdapter()) .setPrettyPrinting() .create(); + private final HttpClient httpClient = new TerramapHttpClient(this.logger); @Override public void onInitialize() { - Terramap.InstanceHolder.setInstance(this); this.logger.info("Initializing Terramap"); + Terramap.InstanceHolder.setInstance(this); SmyLib.initializeGameClient(new WrappedMinecraft(Minecraft.getInstance()), this.logger); } @@ -38,7 +40,7 @@ public Logger logger() { @Override public HttpClient http() { - return null; + return this.httpClient; } @Override diff --git a/fabric/src/main/java/net/smyler/terramap/http/TerramapHttpClient.java b/fabric/src/main/java/net/smyler/terramap/http/TerramapHttpClient.java new file mode 100644 index 00000000..3af5ec43 --- /dev/null +++ b/fabric/src/main/java/net/smyler/terramap/http/TerramapHttpClient.java @@ -0,0 +1,60 @@ +package net.smyler.terramap.http; + +import org.apache.logging.log4j.Logger; + +import java.net.URI; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Objects; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; + + +public class TerramapHttpClient implements HttpClient { + + private static final String USER_AGENT = "Experimental Terramap version https://github.com/SmylerMC/terramap"; + private final java.net.http.HttpClient client = java.net.http.HttpClient.newBuilder().build(); + private final Logger logger; + + public TerramapHttpClient(Logger logger) { + this.logger = logger; + logger.warn("You are using an HTTP client that does not support caching. This is not suitable for production."); + } + + @Override + public CompletableFuture get(String url) { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(url)) + .header("User-Agent", USER_AGENT) + .GET() + .build(); + this.log(request); + CompletableFuture> response = this.client.sendAsync(request, HttpResponse.BodyHandlers.ofByteArray()); + CompletableFuture bytes = response.thenApply(HttpResponse::body); + bytes.exceptionally(t -> { + if (t instanceof CancellationException) { + response.cancel(true); + } + return null; + }); + return bytes; + } + + @Override + public void setMaxConcurrentRequests(String host, int maxConcurrentRequests) { + logger.warn( + "Trying to set max concurrent requests to {}: {}. This is not yet supported", + host, + maxConcurrentRequests + ); + } + + private void log(HttpRequest request) { + this.logger.debug("{} {} {}", + request.method(), + request.uri(), + request.version().map(Objects::toString).orElse("") + ); + } + +}