From 50f49b9834294ce13b825640245eaa66f65a5830 Mon Sep 17 00:00:00 2001 From: Sandro Date: Sun, 20 Jul 2025 15:46:18 +0200 Subject: [PATCH 1/2] Update version to 0.2.6.4-STABLE, add ExampleUsages class, and implement asynchronous job execution --- build.gradle | 2 +- readme.md | 7 +- .../github/example/ExampleUsages.java | 108 ++++++++++++++++++ .../fr/sandro642/github/jobs/JobGetInfos.java | 49 ++------ .../fr/sandro642/github/test/MainTest.java | 25 +++- 5 files changed, 141 insertions(+), 50 deletions(-) create mode 100644 src/main/java/fr/sandro642/github/example/ExampleUsages.java diff --git a/build.gradle b/build.gradle index 1925b59..55b64cb 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'fr.sandro642.github' -version = '0.2.6.3-STABLE' +version = '0.2.6.4-STABLE' tasks.register('printVersion') { doLast { diff --git a/readme.md b/readme.md index 6744872..445fd41 100644 --- a/readme.md +++ b/readme.md @@ -17,7 +17,7 @@ And if you thought APIs were complicated, think again! With ConnectLib, it's lik --- ```java -Stable Version: 0.2.6.3-STABLE +Stable Version: 0.2.6.4-STABLE ``` --- @@ -47,6 +47,7 @@ Changelog: - [0.2.0-STABLE]: Wow, arrival of 0.2.0 in such a short time? There were things to do on this project ;) - [0.2.2-STABLE]: Added log creation. - [0.2.6.1-STABLE]: Patch dû à la compatibilité avec la création de log et le Hook Minecraft. + - [0.2.6.4-STABLE]: Added asynchronous job execution, allowing you to run tasks in the background without blocking your main application thread. ``` --- @@ -104,7 +105,7 @@ repositories { dependencies { - implementation("fr.sandro642.github:ConnectLib:0.2.6.3-STABLE") + implementation("fr.sandro642.github:ConnectLib:0.2.6.4-STABLE") } @@ -140,7 +141,7 @@ public class Example { } ``` -More examples HERE: [ExampleUsage.java](src/main/java/fr/sandro642/github/example/ExampleUsage.java) Not available at the moment due to new features. +More examples HERE: [ExampleUsages.java](src/main/java/fr/sandro642/github/example/ExampleUsages.java) --- diff --git a/src/main/java/fr/sandro642/github/example/ExampleUsages.java b/src/main/java/fr/sandro642/github/example/ExampleUsages.java new file mode 100644 index 0000000..6782548 --- /dev/null +++ b/src/main/java/fr/sandro642/github/example/ExampleUsages.java @@ -0,0 +1,108 @@ +package fr.sandro642.github.example; + +import fr.sandro642.github.ConnectLib; +import fr.sandro642.github.api.ApiFactory; +import fr.sandro642.github.enums.MethodType; +import fr.sandro642.github.enums.ResourceType; +import fr.sandro642.github.enums.VersionType; +import fr.sandro642.github.jobs.JobGetInfos; +import fr.sandro642.github.utils.ConvertEnum; + +import java.util.Map; +import java.util.concurrent.CompletableFuture; + +/** + * ExampleUsages is a placeholder class that can be used to demonstrate how to use the ConnectLib library. + * It can contain example methods or code snippets that show how to interact with the API, handle responses, + * and utilize the features provided by the ConnectLib library. + * + * @author Sandro642 + * @version 1.0 + */ + +public class ExampleUsages { + + public enum ExampleRoutes implements ConvertEnum.RouteImport { + EXAMPLE_ROUTE("/api/example/route"); + + final String route; + + ExampleRoutes(String route) { + this.route = route; + } + + @Override + public String route() { + return route; + } + } + + public void initializeLib() { + + // Optionally, you can specify routes if needed + ConnectLib.initialize(ResourceType.MAIN_RESOURCES, ExampleRoutes.class); + // You can also initialize without specifying routes + ConnectLib.initialize(ResourceType.MAIN_RESOURCES); + } + + // Add methods here to demonstrate how to use the ConnectLib library + // For example, you can create methods to make API calls, handle responses, etc. + + // Example method to demonstrate usage + public void exampleMethodSync() { + // This method can be used to demonstrate how to interact with the API + // For example, making a GET request to the EXAMPLE_ROUTE + ApiFactory response = ConnectLib.JobGetInfos() + .getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE) + .getResponse() + .block(); + + System.out.println(response.display()); + System.out.println("Response Code: " + response.getData("code")); + System.out.println("Response Message: " + response.getData("message")); + System.out.println("Response Data: " + response.getSpecData("data", "exampleKey")); + } + + // Example method to demonstrate asynchronous usage + public void exampleMethodAsync() { + try { + // This method can be used to demonstrate how to interact with the API asynchronously + + // Create a CompletableFuture to handle the asynchronous response + CompletableFuture futureResponse = new CompletableFuture<>(); + + ConnectLib.JobGetInfos() + .getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE) + .getResponse() + .subscribe( + futureResponse::complete, + futureResponse::completeExceptionally + ); + + // Handle the response when it completes + ApiFactory response = futureResponse.get(); + + System.out.println(response.display()); + System.out.println("Response Code: " + response.getData("code")); + System.out.println("Response Message: " + response.getData("message")); + System.out.println("Response Data: " + response.getSpecData("data", "exampleKey")); + } catch (Exception e) { + e.printStackTrace(); + } + } + + // Example to use all methods in JobGetInfos + public void exampleJobGetInfos() { + Map body = Map.of(); + Map params = Map.of(); + + ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE, body, params); + ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.POST, ExampleRoutes.EXAMPLE_ROUTE, body); + ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.PUT, ExampleRoutes.EXAMPLE_ROUTE, null, params); + ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.PATCH, ExampleRoutes.EXAMPLE_ROUTE); + ConnectLib.JobGetInfos().getRoutes(MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE, body, params); + ConnectLib.JobGetInfos().getRoutes(MethodType.POST, ExampleRoutes.EXAMPLE_ROUTE, body); + ConnectLib.JobGetInfos().getRoutes(MethodType.DELETE, ExampleRoutes.EXAMPLE_ROUTE); + } + +} diff --git a/src/main/java/fr/sandro642/github/jobs/JobGetInfos.java b/src/main/java/fr/sandro642/github/jobs/JobGetInfos.java index 1f04d4f..1cb890f 100644 --- a/src/main/java/fr/sandro642/github/jobs/JobGetInfos.java +++ b/src/main/java/fr/sandro642/github/jobs/JobGetInfos.java @@ -5,6 +5,7 @@ import fr.sandro642.github.api.ApiFactory; import fr.sandro642.github.enums.MethodType; import fr.sandro642.github.enums.VersionType; +import reactor.core.publisher.Mono; import java.util.Map; @@ -108,40 +109,6 @@ public JobGetInfos getRoutes(MethodType methodType, String routeName) { return getRoutes(null, methodType, routeName, null, null); } - /** - * 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 body Body of the request for POST (can be null for GET) - * @return JobGetInfos for chaining - */ - public JobGetInfos getRoutesWithBody(MethodType methodType, String routeName, Map body) { - return getRoutes(null, methodType, routeName, body, null); - } - - /** - * Get routes from the YAML file and builds the full URL with additional parameters. - * @param methodType Type of HTTP method (GET, POST) - * @param routeName Name of the route in the YAML file - * @param params Additional parameters for the request - * @return JobGetInfos for chaining - */ - public JobGetInfos getRoutesWithParams(MethodType methodType, String routeName, Map params) { - return getRoutes(null, methodType, routeName, null, params); - } - - /** - * Get routes from the YAML file and builds the full URL with a request body and additional 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 - * @return JobGetInfos for chaining - */ - public JobGetInfos getRoutesBoth(MethodType methodType, String routeName, Map body, Map params) { - return getRoutes(null, methodType, routeName, body, params); - } - /** * Récupère les routes depuis le fichier YAML et construit l'URL complète * @param versionType Version de l'API (V1_BRANCH, V2_BRANCH) @@ -220,7 +187,7 @@ public JobGetInfos getRoutes(VersionType versionType, MethodType methodType, * 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 ApiFactory getResponse() { + public Mono getResponse() { try { String route = (String) ConnectLib.StoreAndRetrieve().store.get("currentRoute"); @@ -231,23 +198,23 @@ public ApiFactory getResponse() { throw new RuntimeException("Route or method not set. Please call getRoutes() first."); } - ApiFactory response; + Mono response; switch(method) { case GET: - response = apiClient.callAPIGet(route).block(); + response = apiClient.callAPIGet(route); break; case POST: - response = apiClient.callAPIPost(route, body).block(); + response = apiClient.callAPIPost(route, body); break; case PUT: - response = apiClient.callAPIPut(route, body).block(); + response = apiClient.callAPIPut(route, body); break; case PATCH: - response = apiClient.callAPIPatch(route, body).block(); + response = apiClient.callAPIPatch(route, body); break; case DELETE: - response = apiClient.callAPIDelete(route).block(); + response = apiClient.callAPIDelete(route); break; default: ConnectLib.Logger().ERROR("Unsupported method type: " + method); diff --git a/src/test/java/fr/sandro642/github/test/MainTest.java b/src/test/java/fr/sandro642/github/test/MainTest.java index ee93d77..ae6edac 100644 --- a/src/test/java/fr/sandro642/github/test/MainTest.java +++ b/src/test/java/fr/sandro642/github/test/MainTest.java @@ -9,6 +9,9 @@ import fr.sandro642.github.utils.ConvertEnum; import org.junit.jupiter.api.Test; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + /** * MainTest is a test class for the ConnectLib library. * @author Sandro642 @@ -45,12 +48,21 @@ public static void main(String[] args) { ConnectLib.initialize(ResourceType.TEST_RESOURCES, TestRoutes.class); try { + CompletableFuture futureResponse = new CompletableFuture<>(); - ApiFactory response = ConnectLib.JobGetInfos() + ConnectLib.JobGetInfos() .getRoutes(VersionType.V1_BRANCH, MethodType.GET, TestRoutes.VERSION) - .getResponse(); + .getResponse() + .subscribe( + futureResponse::complete, + futureResponse::completeExceptionally + ); + + ApiFactory response = futureResponse.get(5, TimeUnit.SECONDS); + + System.out.println("Response: " + response.display()); + - System.out.println(response.getSpecData("data", "version")); } catch (Exception e) { return; } @@ -62,8 +74,11 @@ public void testUseFullRoute() { try { ApiFactory response = ConnectLib.JobGetInfos() - .getRoutes(VersionType.V1_BRANCH, MethodType.POST, TestRoutes.INFO, null, null) - .getResponse(); + .getRoutes(VersionType.V1_BRANCH, MethodType.GET, TestRoutes.VERSION) + .getResponse() + .block(); + + System.out.println("Response: " + response.display()); } catch (Exception e) { e.printStackTrace(); From fd79a6e17d8a7db079b010123a020caa2ee9e672 Mon Sep 17 00:00:00 2001 From: Sandro Soria Date: Sun, 20 Jul 2025 15:48:24 +0200 Subject: [PATCH 2/2] Update src/main/java/fr/sandro642/github/example/ExampleUsages.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/main/java/fr/sandro642/github/example/ExampleUsages.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/fr/sandro642/github/example/ExampleUsages.java b/src/main/java/fr/sandro642/github/example/ExampleUsages.java index 6782548..16e4098 100644 --- a/src/main/java/fr/sandro642/github/example/ExampleUsages.java +++ b/src/main/java/fr/sandro642/github/example/ExampleUsages.java @@ -80,12 +80,14 @@ public void exampleMethodAsync() { ); // Handle the response when it completes - ApiFactory response = futureResponse.get(); + ApiFactory response = futureResponse.get(10, TimeUnit.SECONDS); System.out.println(response.display()); System.out.println("Response Code: " + response.getData("code")); System.out.println("Response Message: " + response.getData("message")); System.out.println("Response Data: " + response.getSpecData("data", "exampleKey")); + } catch (java.util.concurrent.TimeoutException e) { + System.err.println("The operation timed out: " + e.getMessage()); } catch (Exception e) { e.printStackTrace(); }