From d50849ad9e2114290f9b5ddd19fce370c1bbbc26 Mon Sep 17 00:00:00 2001 From: Patrick <147879351+WinniePatGG@users.noreply.github.com> Date: Mon, 1 Jun 2026 18:41:34 +0200 Subject: [PATCH] 0.0.5 | Async Checking with CompletableFuture --- build.gradle | 2 +- .../winniepat/licenselib/LicenseClient.java | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 813cf45..5b10706 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'de.winniepat' -version = '0.0.4' +version = '0.0.5' java { diff --git a/src/main/java/de/winniepat/licenselib/LicenseClient.java b/src/main/java/de/winniepat/licenselib/LicenseClient.java index cec35cc..c5d22e0 100644 --- a/src/main/java/de/winniepat/licenselib/LicenseClient.java +++ b/src/main/java/de/winniepat/licenselib/LicenseClient.java @@ -8,6 +8,7 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; +import java.util.concurrent.CompletableFuture; /** * A client for checking plugin licenses against a license server. @@ -77,6 +78,57 @@ public class LicenseClient { } } + /** + * Asynchronously checks the validity of a license key for a given plugin and server ID against the license server API. + * @param apiUrl Server API url + * @param plugin Plugin id matching the one on the backend api + * @param licenseKey License key issued by the api + * @param serverId Server id matching the one on the backend api + * @return CompletabaleFuture with the LicenseResult from the backend + */ + public static CompletableFuture checkAsync( + String apiUrl, + String plugin, + String licenseKey, + String serverId + ) { + + JsonObject payload = new JsonObject(); + payload.addProperty("plugin", plugin); + payload.addProperty("licenseKey", licenseKey); + + if (serverId != null) { + payload.addProperty("serverId", serverId); + } + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(apiUrl)) + .timeout(Duration.ofSeconds(10)) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(GSON.toJson(payload))) + .build(); + + return HTTP_CLIENT + .sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(response -> parseResponse(response.body())) + .exceptionally(ex -> new LicenseResult( + false, + "error", + ex.getMessage() + )); + } + + private static LicenseResult parseResponse(String body) { + JsonObject json = GSON.fromJson(body, JsonObject.class); + + return new LicenseResult( + json.get("valid").getAsBoolean(), + json.get("status").getAsString(), + json.get("message").getAsString() + ); + } + + /** * Represents the result of a license check. * @param isValid boolean if the license is valid or not