From 8d4c44686a0670eeaea2c17bd0b4cb1323cdc0f8 Mon Sep 17 00:00:00 2001 From: Sandro Date: Tue, 14 Oct 2025 13:26:58 +0300 Subject: [PATCH 1/3] Refactor ApiClient and ApiFactory: make ConnectLib final and improve Javadoc comments --- .../java/fr/sandro642/github/ConnectLib.java | 19 +- .../fr/sandro642/github/api/ApiClient.java | 28 +- .../fr/sandro642/github/api/ApiFactory.java | 18 +- .../fr/sandro642/github/enums/LangType.java | 2 + .../github/enums/lang/CategoriesType.java | 2 + .../github/example/ExampleUsages.java | 5 +- .../fr/sandro642/github/hook/HookManager.java | 14 +- .../fr/sandro642/github/hook/LangSupport.java | 7 +- .../fr/sandro642/github/hook/MCSupport.java | 7 +- .../fr/sandro642/github/jobs/JobGetInfos.java | 77 ++-- .../fr/sandro642/github/jobs/RouteImport.java | 1 + .../fr/sandro642/github/jobs/URLProvider.java | 1 + .../github/jobs/VersionProvider.java | 1 + .../fr/sandro642/github/misc/EnumLoader.java | 1 + .../fr/sandro642/github/misc/LangManager.java | 12 +- .../java/fr/sandro642/github/misc/Logger.java | 8 +- .../java/fr/sandro642/github/misc/Logs.java | 23 +- .../github/misc/StoreAndRetrieve.java | 6 +- .../fr/sandro642/github/misc/YamlUtils.java | 331 +++++++++--------- .../github/update/RetrieveLastVersion.java | 3 +- .../fr/sandro642/github/test/MainTest.java | 17 +- 21 files changed, 324 insertions(+), 259 deletions(-) diff --git a/src/main/java/fr/sandro642/github/ConnectLib.java b/src/main/java/fr/sandro642/github/ConnectLib.java index 534575e..6dea204 100644 --- a/src/main/java/fr/sandro642/github/ConnectLib.java +++ b/src/main/java/fr/sandro642/github/ConnectLib.java @@ -1,13 +1,13 @@ package fr.sandro642.github; import fr.sandro642.github.enums.LangType; +import fr.sandro642.github.enums.ResourceType; import fr.sandro642.github.enums.lang.CategoriesType; import fr.sandro642.github.hook.HookManager; import fr.sandro642.github.hook.LangSupport; import fr.sandro642.github.hook.MCSupport; -import fr.sandro642.github.misc.*; import fr.sandro642.github.jobs.JobGetInfos; -import fr.sandro642.github.enums.ResourceType; +import fr.sandro642.github.misc.*; import fr.sandro642.github.update.RetrieveLastVersion; import java.util.HashMap; @@ -16,6 +16,7 @@ /** * ConnectLib is the main class of the ConnectorAPI library for Standard API. * It provides methods to initialize the library with resource types, routes, and schemas, + * * @author Sandro642 * @version 1.0 * @see ConnectLib#JobGetInfos() @@ -33,15 +34,16 @@ public class ConnectLib { private static Logger logger = new Logger(); private static StoreAndRetrieve storeAndRetrieve = new StoreAndRetrieve(); private static YamlUtils yamlUtils = new YamlUtils(); - private static final Map routes = new HashMap<>(); + private static final Map routes = new HashMap<>(); private static Logs logs = new Logs(); private static LangManager langManager; private static RetrieveLastVersion rlv; /** * Init the ConnectLib with the specified resource type and routes. + * * @param resourceType the type of resource to initialize - * @param routes the routes to be used in the ConnectLib + * @param routes the routes to be used in the ConnectLib */ public void Init(ResourceType resourceType, LangType langType, Class>... routes) { try { @@ -87,6 +89,7 @@ public void Init(ResourceType resourceType, LangType langType, Class callAPIGet(String routeName) { connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.get", Map.of("routename", routeName))); - record ResponseData(int statusCode, String body) {} + record ResponseData(int statusCode, String body) { + } return webClient.get() .uri(routeName) @@ -92,14 +94,16 @@ record ResponseData(int statusCode, String body) {} /** * Method to call the API with a POST request. + * * @param routeName Name of the route to call. - * @param body Body of the request (can be null for a request without body). + * @param body Body of the request (can be null for a request without body). * @return a Mono that emits the ApiFactory response containing the parsed JSON data. */ public Mono callAPIPost(String routeName, Map body) { connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.post", Map.of("routename", routeName))); - record ResponseData(int statusCode, String body) {} + record ResponseData(int statusCode, String body) { + } return webClient.post() .uri(routeName) @@ -123,14 +127,16 @@ record ResponseData(int statusCode, String body) {} /** * Method to call the API with a PUT request. + * * @param routeName Name of the route to call. - * @param body Body of the request (can be null for a request without body). + * @param body Body of the request (can be null for a request without body). * @return a Mono that emits the ApiFactory response containing the parsed JSON data. */ public Mono callAPIPut(String routeName, Map body) { connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.put", Map.of("routename", routeName))); - record ResponseData(int statusCode, String body) {} + record ResponseData(int statusCode, String body) { + } return webClient.put() .uri(routeName) @@ -154,14 +160,16 @@ record ResponseData(int statusCode, String body) {} /** * Method to call the API with a PATCH request. + * * @param routeName Name of the route to call. - * @param body Body of the request (can be null for a request without body). + * @param body Body of the request (can be null for a request without body). * @return a Mono that emits the ApiFactory response containing the parsed JSON data. */ public Mono callAPIPatch(String routeName, Map body) { connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.patch", Map.of("routename", routeName))); - record ResponseData(int statusCode, String body) {} + record ResponseData(int statusCode, String body) { + } return webClient.patch() .uri(routeName) @@ -185,13 +193,15 @@ record ResponseData(int statusCode, String body) {} /** * Method to call the API with a DELETE request. + * * @param routeName Name of the route to call. * @return a Mono that emits the ApiFactory response containing the parsed JSON data. */ public Mono callAPIDelete(String routeName) { connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.delete", Map.of("routename", routeName))); - record ResponseData(int statusCode, String body) {} + record ResponseData(int statusCode, String body) { + } return webClient.delete() .uri(routeName) diff --git a/src/main/java/fr/sandro642/github/api/ApiFactory.java b/src/main/java/fr/sandro642/github/api/ApiFactory.java index b74836f..3b0d67f 100644 --- a/src/main/java/fr/sandro642/github/api/ApiFactory.java +++ b/src/main/java/fr/sandro642/github/api/ApiFactory.java @@ -21,7 +21,7 @@ public class ApiFactory { /** * connectLib is an instance of ConnectLib that provides access to the library's configuration and utilities. */ - private ConnectLib connectLib = new ConnectLib(); + private final ConnectLib connectLib = new ConnectLib(); /** * rawData is a Map that holds the parsed JSON data. @@ -49,7 +49,8 @@ protected void parseFromRawJson(String rawJson) { this.rawJson = rawJson; try { ObjectMapper mapper = new ObjectMapper(); - this.rawData = mapper.readValue(rawJson, new TypeReference>() {}); + this.rawData = mapper.readValue(rawJson, new TypeReference>() { + }); } catch (Exception e) { connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.APIFACTORY_CLASS, "parsefromrawjson.error", Map.of("json", rawJson, "exception", e.getMessage()))); } @@ -99,8 +100,7 @@ public Object getSpecData(O type, K value) { } Object nested = rawData.get(type.toString().toLowerCase()); - if (nested instanceof Map) { - Map nestedMap = (Map) nested; + if (nested instanceof Map nestedMap) { return nestedMap.get(value.toString().toLowerCase()); } } catch (Exception e) { @@ -136,11 +136,11 @@ public int getStatusCode() { */ public Object display() { try { - if (rawData == null) { - connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.APIFACTORY_CLASS, "general.mustbe")); - return null; - } - return rawData; + if (rawData == null) { + connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.APIFACTORY_CLASS, "general.mustbe")); + return null; + } + return rawData; } catch (Exception e) { connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.APIFACTORY_CLASS, "display.error", "exception", e.getMessage())); } diff --git a/src/main/java/fr/sandro642/github/enums/LangType.java b/src/main/java/fr/sandro642/github/enums/LangType.java index d35b513..af7f949 100644 --- a/src/main/java/fr/sandro642/github/enums/LangType.java +++ b/src/main/java/fr/sandro642/github/enums/LangType.java @@ -23,6 +23,7 @@ public enum LangType { /** * Constructor to initialize the language code. + * * @param lang */ LangType(String lang) { @@ -31,6 +32,7 @@ public enum LangType { /** * Get the language code. + * * @return the language code string */ public String getLang() { diff --git a/src/main/java/fr/sandro642/github/enums/lang/CategoriesType.java b/src/main/java/fr/sandro642/github/enums/lang/CategoriesType.java index 72c1318..92766d5 100644 --- a/src/main/java/fr/sandro642/github/enums/lang/CategoriesType.java +++ b/src/main/java/fr/sandro642/github/enums/lang/CategoriesType.java @@ -30,6 +30,7 @@ public enum CategoriesType { /** * Constructor to initialize the category. + * * @param category */ CategoriesType(String category) { @@ -38,6 +39,7 @@ public enum CategoriesType { /** * Get the category. + * * @return the category string */ public String getCategory() { diff --git a/src/main/java/fr/sandro642/github/example/ExampleUsages.java b/src/main/java/fr/sandro642/github/example/ExampleUsages.java index 2c1859f..6ccc582 100644 --- a/src/main/java/fr/sandro642/github/example/ExampleUsages.java +++ b/src/main/java/fr/sandro642/github/example/ExampleUsages.java @@ -7,7 +7,6 @@ import fr.sandro642.github.enums.ResourceType; import fr.sandro642.github.enums.VersionType; import fr.sandro642.github.jobs.RouteImport; -import fr.sandro642.github.misc.EnumLoader; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -24,7 +23,7 @@ public class ExampleUsages { - private ConnectLib connectLib = new ConnectLib(); + private final ConnectLib connectLib = new ConnectLib(); public enum ExampleRoutes implements RouteImport { EXAMPLE_ROUTE("/api/example/route"); @@ -93,7 +92,7 @@ public void exampleMethodAsync() { // Example to use all methods in JobGetInfos public void exampleJobGetInfos() { Map body = Map.of(); - Map params = Map.of(); + Map params = Map.of(); Map query = Map.of(); connectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE, body, params, query); diff --git a/src/main/java/fr/sandro642/github/hook/HookManager.java b/src/main/java/fr/sandro642/github/hook/HookManager.java index d2372da..af9c424 100644 --- a/src/main/java/fr/sandro642/github/hook/HookManager.java +++ b/src/main/java/fr/sandro642/github/hook/HookManager.java @@ -3,15 +3,15 @@ import fr.sandro642.github.ConnectLib; import fr.sandro642.github.enums.ResourceType; import fr.sandro642.github.enums.lang.CategoriesType; -import fr.sandro642.github.misc.Logger; /** * HookManager is a class that manages hooks for different resource types. * It allows for the initialization and management of hooks based on the specified resource type. - * @see HookManager#BASE_PATH() - * @see HookManager#FILE_LOCATION_KEY() + * * @author Sandro642 * @version 1.0 + * @see HookManager#BASE_PATH() + * @see HookManager#FILE_LOCATION_KEY() */ public class HookManager { @@ -26,7 +26,7 @@ public class HookManager { * connectLib is an instance of ConnectLib that provides access to the library's configuration and utilities. * It is used throughout the HookManager class to log messages and access other functionalities. */ - private ConnectLib connectLib = new ConnectLib(); + private final ConnectLib connectLib = new ConnectLib(); /** * Initializes the HookManager with the specified resource type. @@ -43,8 +43,8 @@ public class HookManager { * @return the initialized resource type */ public ResourceType initHook(ResourceType resourceType) { - this.resourceType = resourceType; - return this.resourceType; + HookManager.resourceType = resourceType; + return HookManager.resourceType; } /** @@ -55,7 +55,7 @@ public ResourceType initHook(ResourceType resourceType) { public void FILE_LOCATION_KEY() { switch (resourceType) { case MC_RESOURCES: - connectLib.StoreAndRetrieve().store.put(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY, connectLib.MCSupport().getPluginPath()); + connectLib.StoreAndRetrieve().store.put(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY, connectLib.MCSupport().getPluginPath()); break; case MAIN_RESOURCES, TEST_RESOURCES: connectLib.StoreAndRetrieve().store.put(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY, resourceType.getPath()); diff --git a/src/main/java/fr/sandro642/github/hook/LangSupport.java b/src/main/java/fr/sandro642/github/hook/LangSupport.java index 6595182..5ae1269 100644 --- a/src/main/java/fr/sandro642/github/hook/LangSupport.java +++ b/src/main/java/fr/sandro642/github/hook/LangSupport.java @@ -2,7 +2,6 @@ import fr.sandro642.github.ConnectLib; import fr.sandro642.github.enums.LangType; -import fr.sandro642.github.enums.MethodType; import fr.sandro642.github.enums.lang.CategoriesType; /** @@ -24,16 +23,18 @@ public class LangSupport { * connectLib is an instance of ConnectLib that provides access to the library's configuration and utilities. * It is used throughout the LangSupport class to log messages and access other functionalities. */ - private ConnectLib connectLib = new ConnectLib(); + private final ConnectLib connectLib = new ConnectLib(); /** * Private singleton langType. + * * @return langTypeSingleton */ private LangType langTypeSingleton; /** * Sets the LangType variable for this instance. + * * @param langType The language type to set. Must not be null. * @return The LangType that was set. */ @@ -48,6 +49,7 @@ public LangType setLangTypeVariable(LangType langType) { /** * Method to get the path of the language file based on the LangType. + * * @return The path of the language file. * @throws IllegalArgumentException If the LangType is null or unsupported. */ @@ -63,6 +65,7 @@ public String getPathFile() { * Public Getter of LangSupport instance. * This method returns the singleton instance of LangSupport. * If the instance is null, it creates a new instance. + * * @return instance */ public static LangSupport getInstance() { diff --git a/src/main/java/fr/sandro642/github/hook/MCSupport.java b/src/main/java/fr/sandro642/github/hook/MCSupport.java index 9f63b20..940d201 100644 --- a/src/main/java/fr/sandro642/github/hook/MCSupport.java +++ b/src/main/java/fr/sandro642/github/hook/MCSupport.java @@ -7,6 +7,7 @@ /** * MCSupport is a utility class for handling Minecraft plugin support in the connectLib library. * It provides methods to check if the project is a Minecraft project, set and get the plugin instance, + * * @author Sandro642 * @version 1.0 */ @@ -21,7 +22,7 @@ public class MCSupport { * connectLib is an instance of ConnectLib that provides access to the library's configuration and utilities. * It is used throughout the MCSupport class to log messages and access other functionalities. */ - private ConnectLib connectLib = new ConnectLib(); + private final ConnectLib connectLib = new ConnectLib(); /** * Instance of the Minecraft plugin. @@ -48,8 +49,8 @@ public Plugin setPluginVariable(Plugin plugin) { if (plugin == null) { connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.MCSUPPORT_CLASS, "setplugvar.illegalarg")); } - this.pluginSingleton = plugin; - return this.pluginSingleton; + this.pluginSingleton = plugin; + return this.pluginSingleton; } /** diff --git a/src/main/java/fr/sandro642/github/jobs/JobGetInfos.java b/src/main/java/fr/sandro642/github/jobs/JobGetInfos.java index eafbc52..a52329b 100644 --- a/src/main/java/fr/sandro642/github/jobs/JobGetInfos.java +++ b/src/main/java/fr/sandro642/github/jobs/JobGetInfos.java @@ -15,6 +15,7 @@ /** * JobGetInfos is a utility class for managing API requests in the connectLib library. * It provides methods to construct API routes based on the configuration defined in a YAML file. + * * @author Sandro642 * @version 1.0 * @since 1.0 @@ -31,7 +32,7 @@ public class JobGetInfos { /** * connectLib instance to access its methods and properties. */ - private ConnectLib connectLib = new ConnectLib(); + private final ConnectLib connectLib = new ConnectLib(); /** * URLProvider instance to provide custom URL branches. @@ -50,6 +51,7 @@ public JobGetInfos() { /** * Converts the route name to lowercase. * This method is used to ensure that the route names match the keys in the YAML configuration. + * * @param routeName The enum representing the route name. * @return The lowercase string representation of the route name. */ @@ -59,10 +61,11 @@ private String getRouteName(Enum routeName) { /** * Get routes from the YAML file and builds the full URL. + * * @param versionType Version of the API (V1_BRANCH, V2_BRANCH) - * @param methodType Type of HTTP method (GET, POST) - * @param routeName Name of the route in the YAML file - * @param body Body of the request for POST (can be null for GET) + * @param methodType Type of HTTP method (GET, POST) + * @param routeName Name of the route in the YAML file + * @param body Body of the request for POST (can be null for GET) * @return JobGetInfos for chaining */ public JobGetInfos getRoutes(VersionProvider versionType, MethodType methodType, Enum routeName, Map body) { @@ -71,9 +74,10 @@ public JobGetInfos getRoutes(VersionProvider versionType, MethodType methodType, /** * Get routes from the YAML file and builds the full URL with parameters. + * * @param versionType Version of the API (V1_BRANCH, V2_BRANCH) - * @param methodType Type of HTTP method (GET, POST) - * @param routeName Name of the route in the YAML file + * @param methodType Type of HTTP method (GET, POST) + * @param routeName Name of the route in the YAML file * @return JobGetInfos for chaining */ public JobGetInfos getRoutes(VersionProvider versionType, MethodType methodType, Enum routeName) { @@ -82,8 +86,9 @@ public JobGetInfos getRoutes(VersionProvider versionType, MethodType methodType, /** * Get routes from the YAML file and builds the full URL with a request body. + * * @param methodType Type of HTTP method (GET, POST) - * @param routeName Name of the route in the YAML file + * @param routeName Name of the route in the YAML file * @return JobGetInfos for chaining */ public JobGetInfos getRoutes(MethodType methodType, Enum routeName) { @@ -92,9 +97,10 @@ public JobGetInfos getRoutes(MethodType methodType, Enum routeName) { /** * Get routes from the YAML file and builds the full URL with a request body and parameters. + * * @param methodType Type of HTTP method (GET, POST) - * @param routeName Name of the route in the YAML file - * @param body Body of the request for POST (can be null for GET) + * @param routeName Name of the route in the YAML file + * @param body Body of the request for POST (can be null for GET) * @return JobGetInfos for chaining */ public JobGetInfos getRoutes(MethodType methodType, Enum routeName, Map body) { @@ -103,9 +109,10 @@ public JobGetInfos getRoutes(MethodType methodType, Enum routeName, Map routeName, Map body, Map params) { @@ -114,8 +121,9 @@ public JobGetInfos getRoutes(MethodType methodType, Enum routeName, Map body) { @@ -147,11 +157,12 @@ public JobGetInfos getRoutes(VersionProvider versionType, MethodType methodType, /** * Get routes from the YAML file and builds the full URL with a request body and parameters. + * * @param methodType Type of HTTP method (GET, POST) - * @param routeName Name of the route in the YAML file - * @param body Body of the request for POST (can be null for GET) - * @param params Additional parameters for the request - * @param query Additional query parameters for the request + * @param routeName Name of the route in the YAML file + * @param body Body of the request for POST (can be null for GET) + * @param params Additional parameters for the request + * @param query Additional query parameters for the request * @return JobGetInfos for chaining */ public JobGetInfos getRoutes(MethodType methodType, String routeName, Map body, Map params, Map query) { @@ -160,11 +171,12 @@ public JobGetInfos getRoutes(MethodType methodType, String routeName, Map routeName, Map body, Map params, Map query) { @@ -173,11 +185,12 @@ public JobGetInfos getRoutes(MethodType methodType, Enum routeName, Map JobGetInfos getRoutes(VersionProvider versionType, MethodType methodType, R routeName, Map body, Map params, Map query) { @@ -227,7 +240,7 @@ public JobGetInfos getRoutes(VersionProvider versionType, MethodType methodT if (queryString.length() > 1) { // Clean the route by removing $parameter$ placeholders and extra & String cleanRoute = fullRoute.replaceAll("\\$[^$]+\\$", "").replaceAll("&+", ""); - fullRoute = cleanRoute + queryString.toString(); + fullRoute = cleanRoute + queryString; } } @@ -252,6 +265,7 @@ public JobGetInfos getRoutes(VersionProvider versionType, MethodType methodT /** * Set a custom URL branch for the API calls. * If not set, the default URL from the configuration will be used. + * * @param urlBranch The URLProvider instance providing the custom URL branch. * @return JobGetInfos for chaining */ @@ -265,6 +279,7 @@ public JobGetInfos urlBranch(URLProvider urlBranch) { * Get the response from the API based on the current route and method. * This method retrieves the stored route, method, and body from the store, * makes the API call, and returns the response as an ApiFactory object. + * * @return ApiFactory containing the response from the API, or null if an error occurs. */ public CompletableFuture getResponse() { @@ -304,7 +319,7 @@ public CompletableFuture getResponse() { responseFuture.completeExceptionally(error); }; - switch(method) { + switch (method) { case GET: apiClient.callAPIGet(route).subscribe( response -> onSuccess.accept(response), diff --git a/src/main/java/fr/sandro642/github/jobs/RouteImport.java b/src/main/java/fr/sandro642/github/jobs/RouteImport.java index 0116058..c49fd9d 100644 --- a/src/main/java/fr/sandro642/github/jobs/RouteImport.java +++ b/src/main/java/fr/sandro642/github/jobs/RouteImport.java @@ -9,6 +9,7 @@ public interface RouteImport { /** * Gets the route associated with the enum constant. + * * @return the route as a String */ String getRoute(); diff --git a/src/main/java/fr/sandro642/github/jobs/URLProvider.java b/src/main/java/fr/sandro642/github/jobs/URLProvider.java index 3df0f0c..30bd87e 100644 --- a/src/main/java/fr/sandro642/github/jobs/URLProvider.java +++ b/src/main/java/fr/sandro642/github/jobs/URLProvider.java @@ -9,6 +9,7 @@ public interface URLProvider { /** * Gets the URL of the class implementing this interface. + * * @return the URL as a String */ String getURL(); diff --git a/src/main/java/fr/sandro642/github/jobs/VersionProvider.java b/src/main/java/fr/sandro642/github/jobs/VersionProvider.java index f42893d..95fa685 100644 --- a/src/main/java/fr/sandro642/github/jobs/VersionProvider.java +++ b/src/main/java/fr/sandro642/github/jobs/VersionProvider.java @@ -9,6 +9,7 @@ public interface VersionProvider { /** * Gets the version of the class implementing this interface. + * * @return the version as a String */ String getVersion(); diff --git a/src/main/java/fr/sandro642/github/misc/EnumLoader.java b/src/main/java/fr/sandro642/github/misc/EnumLoader.java index 7d2ff79..e291a04 100644 --- a/src/main/java/fr/sandro642/github/misc/EnumLoader.java +++ b/src/main/java/fr/sandro642/github/misc/EnumLoader.java @@ -8,6 +8,7 @@ /** * ConvertEnum is a utility class for converting enums that implement the RouteImport interface into a map of routes. * This class provides a method to convert an enum class into a map where the keys are the enum constants and the values are their corresponding routes. + * * @author Sandro642 * @version 1.0 * @since 1.0 diff --git a/src/main/java/fr/sandro642/github/misc/LangManager.java b/src/main/java/fr/sandro642/github/misc/LangManager.java index 47c0517..0ff36cf 100644 --- a/src/main/java/fr/sandro642/github/misc/LangManager.java +++ b/src/main/java/fr/sandro642/github/misc/LangManager.java @@ -16,10 +16,10 @@ /** * LangManager handles loading and retrieving language messages * from .lang files with placeholder support. - * + *

* This version uses only enum-based categories (CategoriesType). - * - * WARNING : THE ONLY FILE YOU CAN'T ADD IN LANG FILE, ENGLISH DEFAULT LANG. + *

+ * WARNING : THE ONLY FILE YOU CAN'T ADD IN LANG FILE, ENGLISH DEFAULT LANG. * * @author Sandro642 * @version 2.0 @@ -30,7 +30,7 @@ public class LangManager { * connectLib is an instance of ConnectLib that provides access to the library's configuration and utilities. * It is used throughout the LangManager class to log messages and access other functionalities. */ - private ConnectLib connectLib = new ConnectLib(); + private final ConnectLib connectLib = new ConnectLib(); private final Map> messages; private static final Pattern CATEGORY_PATTERN = Pattern.compile("\\[(.+)]"); @@ -117,9 +117,9 @@ private CategoriesType parseCategory(String categoryName) { /** * Récupère un message localisé avec remplacement de variables. * - * @param category La catégorie (enum) + * @param category La catégorie (enum) * @param messagePath Le chemin du message - * @param arguments Les arguments sous forme de paires clé-valeur + * @param arguments Les arguments sous forme de paires clé-valeur */ public String getMessage(CategoriesType category, String messagePath, String... arguments) { if (arguments == null || arguments.length == 0) { diff --git a/src/main/java/fr/sandro642/github/misc/Logger.java b/src/main/java/fr/sandro642/github/misc/Logger.java index 51f7768..f5a595d 100644 --- a/src/main/java/fr/sandro642/github/misc/Logger.java +++ b/src/main/java/fr/sandro642/github/misc/Logger.java @@ -4,7 +4,7 @@ * Logger is a utility class for logging messages in the ConnectLib library. * It provides methods to log messages with different severity levels: * INFO, WARN, ERROR, and CRITICAL. - * + *

* Use setShowLogs(true/false) to control console output. * Logs are always saved to file regardless of the setting. * @@ -14,7 +14,7 @@ public class Logger { - private Logs logs = new Logs(); + private final Logs logs = new Logs(); private static boolean showLogs = false; // Default: logs are hidden /** @@ -31,6 +31,7 @@ public void showLogs() { * Method to display an informational message in the console. * This method prints the message in green color if showLogs is enabled, * and always logs it to file. + * * @param msg The message to log */ public void INFO(String msg) { @@ -47,6 +48,7 @@ public void INFO(String msg) { * Method to display a warning message in the console. * This method prints the message in yellow color if showLogs is enabled, * and always logs it to file. + * * @param msg The message to log */ public void WARN(String msg) { @@ -63,6 +65,7 @@ public void WARN(String msg) { * Method to display an error message in the console. * This method prints the message in red color if showLogs is enabled, * and always logs it to file. + * * @param msg The message to log */ public void ERROR(String msg) { @@ -79,6 +82,7 @@ public void ERROR(String msg) { * Method to display a critical message in the console. * This method prints the message in magenta color if showLogs is enabled, * and always logs it to file. + * * @param msg The message to log */ public void CRITICAL(String msg) { diff --git a/src/main/java/fr/sandro642/github/misc/Logs.java b/src/main/java/fr/sandro642/github/misc/Logs.java index b911596..a55b8eb 100644 --- a/src/main/java/fr/sandro642/github/misc/Logs.java +++ b/src/main/java/fr/sandro642/github/misc/Logs.java @@ -1,5 +1,7 @@ package fr.sandro642.github.misc; +import fr.sandro642.github.ConnectLib; + import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -8,8 +10,6 @@ import java.util.Date; import java.util.List; -import fr.sandro642.github.ConnectLib; - /** * Logs is a utility class for managing logging in the ConnectLib library. * It provides methods to create logs with timestamps and log types, and to write them to a file. @@ -29,7 +29,7 @@ public class Logs { * connectLib is an instance of ConnectLib that provides access to the library's configuration and utilities. * It is used throughout the Logs class to log messages and access other functionalities. */ - private ConnectLib connectLib = new ConnectLib(); + private final ConnectLib connectLib = new ConnectLib(); /** * Constructor of Logs @@ -41,19 +41,20 @@ public class Logs { * Make list for save logs in memory before writing to file. * This buffer is used to store log messages temporarily before they are written to a file. */ - private static List logBuffer = new ArrayList<>(); + private static final List logBuffer = new ArrayList<>(); /** * Static block to initialize the Logs instance. * This block is executed when the class is loaded, ensuring that the instance is created only once. */ public void setPathFile() { - this.pathFile = connectLib.HookManager().BASE_PATH(); + pathFile = connectLib.HookManager().BASE_PATH(); } /** * * Static block to initialize the Logs instance. * This block is executed when the class is loaded, ensuring that the instance is created only once. + * * @param logType The type of resource for which the logs are being created. * @param

* @param logText The text to be logged. @@ -64,16 +65,15 @@ public

void MakeALog(String logText, P logType) { SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String timestamp = timestampFormat.format(date); - StringBuilder logContent = new StringBuilder(); - logContent.append("[").append(timestamp).append("] "); - logContent.append("[").append(logType.toString().toUpperCase()).append("] "); - logContent.append(logText).append("\n"); + String logContent = "[" + timestamp + "] " + + "[" + logType.toString().toUpperCase() + "] " + + logText + "\n"; - logBuffer.add(logContent.toString()); + logBuffer.add(logContent); if (connectLib.YamlUtils().isLogEnabled()) { - if (logType.toString().toUpperCase().equals("CRITICAL") || logType.toString().toUpperCase().equals("ERROR")) { + if (logType.toString().equalsIgnoreCase("CRITICAL") || logType.toString().equalsIgnoreCase("ERROR")) { File directory = new File(pathFile, "logs"); if (!directory.exists()) { directory.mkdirs(); @@ -102,6 +102,7 @@ public

void MakeALog(String logText, P logType) { /** * Get the instance of Logs. + * * @return instance of Logs */ public static Logs getLogs() { diff --git a/src/main/java/fr/sandro642/github/misc/StoreAndRetrieve.java b/src/main/java/fr/sandro642/github/misc/StoreAndRetrieve.java index d78a94f..a2d7053 100644 --- a/src/main/java/fr/sandro642/github/misc/StoreAndRetrieve.java +++ b/src/main/java/fr/sandro642/github/misc/StoreAndRetrieve.java @@ -24,7 +24,8 @@ public class StoreAndRetrieve { /** * Add a key-value pair to the store. - * @param key is the key under which the value will be stored. + * + * @param key is the key under which the value will be stored. * @param value is the value to be stored under the specified key. */ public void put(String key, Object value) { @@ -33,6 +34,7 @@ public void put(String key, Object value) { /** * Retrieve a value associated with the specified key. + * * @param key is the key for which the value will be retrieved. * @return the value associated with the specified key, or null if the key does not exist. */ @@ -42,6 +44,7 @@ public Object get(String key) { /** * Remove a key-value pair from the store. + * * @param key is the key of the entry to be removed. */ public void remove(String key) { @@ -50,6 +53,7 @@ public void remove(String key) { /** * Check if the store contains a specific key. + * * @param key is the key to check for existence in the store. * @return true if the key exists, false otherwise. */ diff --git a/src/main/java/fr/sandro642/github/misc/YamlUtils.java b/src/main/java/fr/sandro642/github/misc/YamlUtils.java index 31b1200..fe67f33 100644 --- a/src/main/java/fr/sandro642/github/misc/YamlUtils.java +++ b/src/main/java/fr/sandro642/github/misc/YamlUtils.java @@ -21,174 +21,175 @@ public class YamlUtils { - private ConnectLib connectLib = new ConnectLib(); - - /** - * Gets the base URL from the YAML configuration file. - * * This method reads the `infos.yml` file located in the directory specified by the - * @return the base URL as a String, or null if an error occurs. - */ - public String getURL() { - - String yamlFilePath = connectLib.StoreAndRetrieve().store.get(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY) - + "/infos.yml"; - - try (InputStream inputStream = Files.newInputStream(Paths.get(yamlFilePath))) { - Yaml yaml = new Yaml(); - Map yamlData = yaml.load(inputStream); - return (String) yamlData.get("urlPath"); - } catch (Exception ex) { + private final ConnectLib connectLib = new ConnectLib(); + + /** + * Gets the base URL from the YAML configuration file. + * * This method reads the `infos.yml` file located in the directory specified by the + * + * @return the base URL as a String, or null if an error occurs. + */ + public String getURL() { + + String yamlFilePath = connectLib.StoreAndRetrieve().store.get(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY) + + "/infos.yml"; + + try (InputStream inputStream = Files.newInputStream(Paths.get(yamlFilePath))) { + Yaml yaml = new Yaml(); + Map yamlData = yaml.load(inputStream); + return (String) yamlData.get("urlPath"); + } catch (Exception ex) { connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.YAMLUTILS_CLASS, "geturl.error", Map.of("file", "infos.yml", "exception", ex.getMessage()))); - return null; - } - } - - /** - * Checks if logging is enabled in the YAML configuration file. - * This method reads the `infos.yml` file and retrieves the `enableLogs` setting. - * - * @return true if logging is enabled, false if disabled, or null if an error occurs. - */ - public Boolean isLogEnabled() { - String yamlFilePath = connectLib.StoreAndRetrieve().store.get(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY) - + "/infos.yml"; - - try (InputStream inputStream = Files.newInputStream(Paths.get(yamlFilePath))) { - Yaml yaml = new Yaml(); - Map yamlData = yaml.load(inputStream); - return (Boolean) yamlData.get("enableLogs"); - } catch (Exception ex) { - return null; - } - } - - /** - * Retrieves the routes defined in the YAML configuration file. - * This method reads the `infos.yml` file and returns a map of routes. - * - * @return a map of route names to their corresponding paths, or null if an error occurs. - */ - public Map getRoutes() { - - String yamlFilePath = connectLib.StoreAndRetrieve().store.get(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY) - + "/infos.yml"; - - try (InputStream inputStream = Files.newInputStream(Paths.get(yamlFilePath))) { - Yaml yaml = new Yaml(); - Map yamlData = yaml.load(inputStream); - - return (Map) yamlData.get("routes"); - } catch (Exception ex) { - return null; - } - } - -/** - * Generates a template `infos.yml` file if it does not already exist. - * If the file exists, it updates the routes section with the provided routes. - * - * @param routes a map of route names to their corresponding paths - */ - public void generateTemplateIfNotExists(Map, String> routes) { - String basePath = connectLib.HookManager().BASE_PATH(); - - File directory = new File(basePath); - - if (!directory.exists()) { - directory.mkdirs(); - } - - File file = new File(basePath, "infos.yml"); - - if (file.exists()) { - try { - List lines = new ArrayList<>(); - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - String line; - while ((line = reader.readLine()) != null) { - lines.add(line); - } - } - - int routesStartIndex = -1; - int routesEndIndex = -1; - - for (int i = 0; i < lines.size(); i++) { - String line = lines.get(i).trim(); - if (line.equals("routes:")) { - routesStartIndex = i; - } else if (routesStartIndex != -1 && line.matches("^[a-zA-Z_][a-zA-Z0-9_]*:.*")) { - - if (!line.startsWith("#") && !line.matches("^\\s*[a-zA-Z_][a-zA-Z0-9_]*:\\s*\"/.*")) { - routesEndIndex = i; - break; - } - } - } - - // If routesEndIndex is not found, it means we reached the end of the file - if (routesStartIndex != -1 && routesEndIndex == -1) { - routesEndIndex = lines.size(); - } - - if (routesStartIndex != -1) { - - List newLines = new ArrayList<>(); - newLines.addAll(lines.subList(0, routesStartIndex + 1)); - - newLines.add(" #info: \"/info/version\""); - newLines.add(" #ping: \"/ping\""); - newLines.add(" #status: \"/status\""); - newLines.add(""); - - - for (Map.Entry, String> entry : routes.entrySet()) { - newLines.add(" " + entry.getKey().name().toLowerCase() + ": \"" + entry.getValue() + "\""); - } - - if (routesEndIndex < lines.size()) { - newLines.add(""); - newLines.addAll(lines.subList(routesEndIndex, lines.size())); - } - - try (FileWriter writer = new FileWriter(file)) { - for (String line : newLines) { - writer.write(line + "\n"); - } - } - } - - } catch (IOException e) { + return null; + } + } + + /** + * Checks if logging is enabled in the YAML configuration file. + * This method reads the `infos.yml` file and retrieves the `enableLogs` setting. + * + * @return true if logging is enabled, false if disabled, or null if an error occurs. + */ + public Boolean isLogEnabled() { + String yamlFilePath = connectLib.StoreAndRetrieve().store.get(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY) + + "/infos.yml"; + + try (InputStream inputStream = Files.newInputStream(Paths.get(yamlFilePath))) { + Yaml yaml = new Yaml(); + Map yamlData = yaml.load(inputStream); + return (Boolean) yamlData.get("enableLogs"); + } catch (Exception ex) { + return null; + } + } + + /** + * Retrieves the routes defined in the YAML configuration file. + * This method reads the `infos.yml` file and returns a map of routes. + * + * @return a map of route names to their corresponding paths, or null if an error occurs. + */ + public Map getRoutes() { + + String yamlFilePath = connectLib.StoreAndRetrieve().store.get(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY) + + "/infos.yml"; + + try (InputStream inputStream = Files.newInputStream(Paths.get(yamlFilePath))) { + Yaml yaml = new Yaml(); + Map yamlData = yaml.load(inputStream); + + return (Map) yamlData.get("routes"); + } catch (Exception ex) { + return null; + } + } + + /** + * Generates a template `infos.yml` file if it does not already exist. + * If the file exists, it updates the routes section with the provided routes. + * + * @param routes a map of route names to their corresponding paths + */ + public void generateTemplateIfNotExists(Map, String> routes) { + String basePath = connectLib.HookManager().BASE_PATH(); + + File directory = new File(basePath); + + if (!directory.exists()) { + directory.mkdirs(); + } + + File file = new File(basePath, "infos.yml"); + + if (file.exists()) { + try { + List lines = new ArrayList<>(); + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + while ((line = reader.readLine()) != null) { + lines.add(line); + } + } + + int routesStartIndex = -1; + int routesEndIndex = -1; + + for (int i = 0; i < lines.size(); i++) { + String line = lines.get(i).trim(); + if (line.equals("routes:")) { + routesStartIndex = i; + } else if (routesStartIndex != -1 && line.matches("^[a-zA-Z_][a-zA-Z0-9_]*:.*")) { + + if (!line.startsWith("#") && !line.matches("^\\s*[a-zA-Z_][a-zA-Z0-9_]*:\\s*\"/.*")) { + routesEndIndex = i; + break; + } + } + } + + // If routesEndIndex is not found, it means we reached the end of the file + if (routesStartIndex != -1 && routesEndIndex == -1) { + routesEndIndex = lines.size(); + } + + if (routesStartIndex != -1) { + + List newLines = new ArrayList<>(); + newLines.addAll(lines.subList(0, routesStartIndex + 1)); + + newLines.add(" #info: \"/info/version\""); + newLines.add(" #ping: \"/ping\""); + newLines.add(" #status: \"/status\""); + newLines.add(""); + + + for (Map.Entry, String> entry : routes.entrySet()) { + newLines.add(" " + entry.getKey().name().toLowerCase() + ": \"" + entry.getValue() + "\""); + } + + if (routesEndIndex < lines.size()) { + newLines.add(""); + newLines.addAll(lines.subList(routesEndIndex, lines.size())); + } + + try (FileWriter writer = new FileWriter(file)) { + for (String line : newLines) { + writer.write(line + "\n"); + } + } + } + + } catch (IOException e) { connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.YAMLUTILS_CLASS, "gentempifnotexist.error", Map.of("file", "infos.yml", "exception", e.getMessage()))); - } - } else { - - StringBuilder template = new StringBuilder( - "# properties ConnectLib By Sandro642\n\n" + - "urlPath: \"http://localhost:8080/api\"\n\n" + - "routes:\n" + - " #info: \"/info/version\"\n" + - " #ping: \"/ping\"\n" + - " #status: \"/status\"\n\n"); - - for (Map.Entry, String> entry : routes.entrySet()) { - template.append(" ") - .append(entry.getKey().name().toLowerCase()) - .append(": \"") - .append(entry.getValue()) - .append("\"\n"); - } - - template.append("\n\n# Logs\n") - .append("enableLogs: true\n"); - - try (FileWriter writer = new FileWriter(file)) { - writer.write(template.toString()); + } + } else { + + StringBuilder template = new StringBuilder( + "# properties ConnectLib By Sandro642\n\n" + + "urlPath: \"http://localhost:8080/api\"\n\n" + + "routes:\n" + + " #info: \"/info/version\"\n" + + " #ping: \"/ping\"\n" + + " #status: \"/status\"\n\n"); + + for (Map.Entry, String> entry : routes.entrySet()) { + template.append(" ") + .append(entry.getKey().name().toLowerCase()) + .append(": \"") + .append(entry.getValue()) + .append("\"\n"); + } + + template.append("\n\n# Logs\n") + .append("enableLogs: true\n"); + + try (FileWriter writer = new FileWriter(file)) { + writer.write(template.toString()); connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.YAMLUTILS_CLASS, "gentempifnotexist.needed", Map.of("file", "infos.yml"))); - } catch (IOException e) { + } catch (IOException e) { connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.YAMLUTILS_CLASS, "gentempifnotexist.errorcreated", Map.of("file", "infos.yml", "exception", e.getMessage()))); - } - } - } + } + } + } } diff --git a/src/main/java/fr/sandro642/github/update/RetrieveLastVersion.java b/src/main/java/fr/sandro642/github/update/RetrieveLastVersion.java index 217239c..dca688f 100644 --- a/src/main/java/fr/sandro642/github/update/RetrieveLastVersion.java +++ b/src/main/java/fr/sandro642/github/update/RetrieveLastVersion.java @@ -20,12 +20,13 @@ public class RetrieveLastVersion { * The latest version of the software. * Default is set to the current version defined in the Version class. */ - private String latestVersion = Version.VERSION; + private final String latestVersion = Version.VERSION; /** * Fetches the latest version from the GitHub API. * It sends a GET request to the tags endpoint of the ConnectLib repository * and retrieves the name of the first tag, which represents the latest version. + * * @return */ public String fetchVersion() { diff --git a/src/test/java/fr/sandro642/github/test/MainTest.java b/src/test/java/fr/sandro642/github/test/MainTest.java index 956ebce..76eefbd 100644 --- a/src/test/java/fr/sandro642/github/test/MainTest.java +++ b/src/test/java/fr/sandro642/github/test/MainTest.java @@ -9,7 +9,6 @@ import fr.sandro642.github.jobs.RouteImport; import fr.sandro642.github.jobs.URLProvider; import fr.sandro642.github.jobs.VersionProvider; -import fr.sandro642.github.misc.EnumLoader; import org.junit.jupiter.api.Test; import java.util.Map; @@ -25,7 +24,7 @@ public class MainTest { - private static ConnectLib connectLib = new ConnectLib(); + private static final ConnectLib connectLib = new ConnectLib(); public enum TestRoutes implements RouteImport { HELLO("/hello"), @@ -44,13 +43,16 @@ public String getRoute() { } } - public enum TestUrl implements URLProvider { + /** + * Example of URL branches, you can add multiple branches if you have multiple environments (dev, prod, etc.) + */ + public enum ExampleUrlBranch implements URLProvider { LOCALHOST("http://localhost:8080") ; private final String url; - TestUrl(String url) { + ExampleUrlBranch(String url) { this.url = url; } @@ -97,7 +99,6 @@ public static void main(String[] args) { } catch (Exception e) { - return; } } @@ -135,6 +136,12 @@ public void testLangType() { try { CompletableFuture factoryCompletableFuture = connectLib.JobGetInfos() .getRoutes(MethodType.GET, TestRoutes.HELLO) + + /** + * You can change the URL of the API here if you want to use another URL than the one in the config file. + */ + .urlBranch(ExampleUrlBranch.LOCALHOST) + .getResponse(); ApiFactory response = factoryCompletableFuture.get(5, TimeUnit.SECONDS); From 48d5838cd531222ff3f7f5b38bdb1a30b3099d62 Mon Sep 17 00:00:00 2001 From: Sandro Date: Tue, 14 Oct 2025 13:30:37 +0300 Subject: [PATCH 2/3] Update version to 0.3.8.1-DEV_BUILD and remove stable version references from README --- README.md | 30 ------------------------------ build.gradle | 2 +- 2 files changed, 1 insertion(+), 31 deletions(-) diff --git a/README.md b/README.md index 46ab2a2..bba1209 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,6 @@ And if you thought APIs were complicated, think again! With ConnectLib, it's lik --- -```java -Stable Version: 0.3.8-STABLE -``` - ---- - ```java[build.gradle](build.gradle) Support Lib : Java 23 @@ -103,24 +97,6 @@ Imagine an API that doesn't just connect services, but becomes the conductor of ## 🧑‍💻 Usage Example -Library Import -```java -repositories { - - maven { - url = uri("https://sandro642.github.io/connectlib/jar") - } - -} - -dependencies { - - implementation("fr.sandro642.github:ConnectLib:0.3.8-STABLE") - -} - -``` - More examples HERE: [ExampleUsages.java](src/main/java/fr/sandro642/github/example/ExampleUsages.java) --- @@ -141,12 +117,6 @@ More examples HERE: [ExampleUsages.java](src/main/java/fr/sandro642/github/examp --- -## 🧠 Evolution Ideas - -- Adding connectors for new services -- Web management interface -- Plugin system - --- ## 📞 Contact diff --git a/build.gradle b/build.gradle index 11c9dd2..bf631d2 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'fr.sandro642.github' -version = '0.3.8-STABLE' +version = '0.3.8.1-DEV_BUILD' // Générer une classe de version automatiquement task generateVersionClass { From 5dd3b45941f30472cccc0b9023266ce335282bec Mon Sep 17 00:00:00 2001 From: Sandro Soria Date: Tue, 14 Oct 2025 12:32:42 +0200 Subject: [PATCH 3/3] Add DEV/BRANCH-0.3.8.1 to workflow triggers --- .github/workflows/work-jar.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/work-jar.yml b/.github/workflows/work-jar.yml index f6222b4..52e80ee 100644 --- a/.github/workflows/work-jar.yml +++ b/.github/workflows/work-jar.yml @@ -3,6 +3,7 @@ on: push: branches: - main + - DEV/BRANCH-0.3.8.1 permissions: contents: write pages: write