0.0.5 | Async Checking with CompletableFuture
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = 'de.winniepat'
|
group = 'de.winniepat'
|
||||||
version = '0.0.4'
|
version = '0.0.5'
|
||||||
|
|
||||||
|
|
||||||
java {
|
java {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import java.net.http.HttpClient;
|
|||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A client for checking plugin licenses against a license server.
|
* 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<LicenseResult> 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.
|
* Represents the result of a license check.
|
||||||
* @param isValid boolean if the license is valid or not
|
* @param isValid boolean if the license is valid or not
|
||||||
|
|||||||
Reference in New Issue
Block a user