Skip to content

Commit

Permalink
Implement very basic HTTP client in 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jun 24, 2024
1 parent f79ceb0 commit d3c902f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand All @@ -38,7 +40,7 @@ public Logger logger() {

@Override
public HttpClient http() {
return null;
return this.httpClient;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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<byte[]> get(String url) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("User-Agent", USER_AGENT)
.GET()
.build();
this.log(request);
CompletableFuture<HttpResponse<byte[]>> response = this.client.sendAsync(request, HttpResponse.BodyHandlers.ofByteArray());
CompletableFuture<byte[]> 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("")
);
}

}

0 comments on commit d3c902f

Please sign in to comment.