0.0.7 | More intuitive I guess and much cleanup
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = 'de.winniepat'
|
group = 'de.winniepat'
|
||||||
version = '0.0.6'
|
version = '0.0.7'
|
||||||
|
|
||||||
|
|
||||||
java {
|
java {
|
||||||
|
|||||||
@@ -38,39 +38,12 @@ public class LicenseClient {
|
|||||||
*/
|
*/
|
||||||
public static LicenseResult check(String apiUrl, String plugin, String licenseKey, String serverId) {
|
public static LicenseResult check(String apiUrl, String plugin, String licenseKey, String serverId) {
|
||||||
try {
|
try {
|
||||||
JsonObject payload = new JsonObject();
|
HttpRequest request = createRequest(apiUrl, plugin, licenseKey, serverId);
|
||||||
payload.addProperty("plugin", plugin);
|
HttpResponse<String> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
payload.addProperty("licenseKey", licenseKey);
|
|
||||||
|
|
||||||
if (serverId != null) {
|
return fromHttpResponse(response);
|
||||||
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();
|
|
||||||
|
|
||||||
HttpResponse<String> response = HTTP_CLIENT.send(
|
|
||||||
request,
|
|
||||||
HttpResponse.BodyHandlers.ofString()
|
|
||||||
);
|
|
||||||
|
|
||||||
JsonObject json = GSON.fromJson(response.body(), JsonObject.class);
|
|
||||||
|
|
||||||
return new LicenseResult(
|
|
||||||
json.get("valid").getAsBoolean(),
|
|
||||||
json.get("status").getAsString(),
|
|
||||||
json.get("message").getAsString()
|
|
||||||
);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return new LicenseResult(
|
return errorResult(e);
|
||||||
false,
|
|
||||||
"error",
|
|
||||||
e.getMessage()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +56,40 @@ public class LicenseClient {
|
|||||||
* @return CompletabaleFuture with the LicenseResult from the backend
|
* @return CompletabaleFuture with the LicenseResult from the backend
|
||||||
*/
|
*/
|
||||||
public static CompletableFuture<LicenseResult> checkAsync(String apiUrl, String plugin, String licenseKey, String serverId) {
|
public static CompletableFuture<LicenseResult> checkAsync(String apiUrl, String plugin, String licenseKey, String serverId) {
|
||||||
|
HttpRequest request = createRequest(apiUrl, plugin, licenseKey, serverId);
|
||||||
|
|
||||||
|
return HTTP_CLIENT
|
||||||
|
.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
||||||
|
.thenApply(LicenseClient::fromHttpResponse)
|
||||||
|
.exceptionally(LicenseClient::errorResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Record representing the result of a license check, containing the validity, status, message, plugin, customer, expiration date, and check date.
|
||||||
|
* @param valid indicates whether the license is valid or not
|
||||||
|
* @param status the status of the license check, e.g. "valid", "invalid", "expired", etc.
|
||||||
|
* @param message a message providing additional information about the license check result, e.g. error messages or instructions for resolving issues
|
||||||
|
* @param plugin the plugin for which the license check was performed
|
||||||
|
* @param customer the customer associated with the license, if available
|
||||||
|
* @param expiresAt the expiration date of the license, if available
|
||||||
|
* @param checkedAt the date and time when the license check was performed
|
||||||
|
*/
|
||||||
|
public record LicenseResult(
|
||||||
|
boolean valid,
|
||||||
|
String status,
|
||||||
|
String message,
|
||||||
|
String plugin,
|
||||||
|
String customer,
|
||||||
|
String expiresAt,
|
||||||
|
String checkedAt
|
||||||
|
) { }
|
||||||
|
|
||||||
|
private static HttpRequest createRequest(
|
||||||
|
String apiUrl,
|
||||||
|
String plugin,
|
||||||
|
String licenseKey,
|
||||||
|
String serverId
|
||||||
|
) {
|
||||||
JsonObject payload = new JsonObject();
|
JsonObject payload = new JsonObject();
|
||||||
payload.addProperty("plugin", plugin);
|
payload.addProperty("plugin", plugin);
|
||||||
payload.addProperty("licenseKey", licenseKey);
|
payload.addProperty("licenseKey", licenseKey);
|
||||||
@@ -92,68 +98,50 @@ public class LicenseClient {
|
|||||||
payload.addProperty("serverId", serverId);
|
payload.addProperty("serverId", serverId);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
return HttpRequest.newBuilder()
|
||||||
.uri(URI.create(apiUrl))
|
.uri(URI.create(apiUrl))
|
||||||
.timeout(Duration.ofSeconds(10))
|
.timeout(Duration.ofSeconds(10))
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.POST(HttpRequest.BodyPublishers.ofString(GSON.toJson(payload)))
|
.POST(HttpRequest.BodyPublishers.ofString(GSON.toJson(payload)))
|
||||||
.build();
|
.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 errorResult(Throwable throwable) {
|
||||||
* Parses the JSON response from the license server API into a LicenseResult object.
|
|
||||||
* @param body the JSON response body from the license server API
|
|
||||||
* @return a LicenseResult object containing the validity, status, and message from the API response
|
|
||||||
*/
|
|
||||||
private static LicenseResult parseResponse(String body) {
|
|
||||||
JsonObject json = GSON.fromJson(body, JsonObject.class);
|
|
||||||
|
|
||||||
return new LicenseResult(
|
return new LicenseResult(
|
||||||
getBoolean(json, "valid", false),
|
false,
|
||||||
getString(json, "status", "unknown"),
|
"error",
|
||||||
getString(json, "message", "")
|
throwable.getMessage(),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents the result of a license check.
|
|
||||||
* @param isValid boolean if the license is valid or not
|
|
||||||
* @param status license status
|
|
||||||
* @param message additional message from the backend
|
|
||||||
*/
|
|
||||||
public record LicenseResult(boolean isValid, String status, String message) { }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper Method for getting Strings from the JsonObject without creating a NullPointerException
|
|
||||||
*
|
|
||||||
* @param json JsonObject to get the value from
|
|
||||||
* @param key key to get the value for
|
|
||||||
* @param def fallback value if Null
|
|
||||||
* @return the value from the JsonObject or the fallback value if the key is not present or null
|
|
||||||
*/
|
|
||||||
private static String getString(JsonObject json, String key, String def) {
|
private static String getString(JsonObject json, String key, String def) {
|
||||||
if (!json.has(key) || json.get(key).isJsonNull()) return def;
|
return json.has(key) && !json.get(key).isJsonNull()
|
||||||
return json.get(key).getAsString();
|
? json.get(key).getAsString()
|
||||||
|
: def;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static LicenseResult fromHttpResponse(HttpResponse<String> response) {
|
||||||
* Helper Method for getting booleans from the JsonObject without creating a NullPointerException
|
if (response.statusCode() != 200) {
|
||||||
* @param json JsonObject to get the value from
|
return errorResult(new RuntimeException(
|
||||||
* @param key key to get the value for
|
"HTTP " + response.statusCode() + ": " + response.body()
|
||||||
* @param def fallback value if Null
|
));
|
||||||
* @return the value from the JsonObject or the fallback value if the key is not present or null
|
}
|
||||||
*/
|
|
||||||
private static boolean getBoolean(JsonObject json, String key, boolean def) {
|
JsonObject json = JsonParser.parseString(response.body()).getAsJsonObject();
|
||||||
if (!json.has(key) || json.get(key).isJsonNull()) return def;
|
boolean valid = json.has("valid") && json.get("valid").getAsBoolean();
|
||||||
return json.get(key).getAsBoolean();
|
|
||||||
|
return new LicenseResult(
|
||||||
|
valid,
|
||||||
|
getString(json, "status", "unknown"),
|
||||||
|
getString(json, "message", ""),
|
||||||
|
getString(json, "plugin", null),
|
||||||
|
getString(json, "customer", null),
|
||||||
|
getString(json, "expiresAt", null),
|
||||||
|
getString(json, "checkedAt", null)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user