Skip to content

Commit b533b3d

Browse files
authored
Merge pull request #29 from Sandro642/feature/async
Update version to 0.2.6.4-STABLE, add ExampleUsages class, and implem…
2 parents 43bafa1 + fd79a6e commit b533b3d

5 files changed

Lines changed: 143 additions & 50 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = 'fr.sandro642.github'
8-
version = '0.2.6.3-STABLE'
8+
version = '0.2.6.4-STABLE'
99

1010
tasks.register('printVersion') {
1111
doLast {

readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ And if you thought APIs were complicated, think again! With ConnectLib, it's lik
1717
---
1818

1919
```java
20-
Stable Version: 0.2.6.3-STABLE
20+
Stable Version: 0.2.6.4-STABLE
2121
```
2222

2323
---
@@ -47,6 +47,7 @@ Changelog:
4747
- [0.2.0-STABLE]: Wow, arrival of 0.2.0 in such a short time? There were things to do on this project ;)
4848
- [0.2.2-STABLE]: Added log creation.
4949
- [0.2.6.1-STABLE]: Patch dû à la compatibilité avec la création de log et le Hook Minecraft.
50+
- [0.2.6.4-STABLE]: Added asynchronous job execution, allowing you to run tasks in the background without blocking your main application thread.
5051
```
5152

5253
---
@@ -104,7 +105,7 @@ repositories {
104105

105106
dependencies {
106107

107-
implementation("fr.sandro642.github:ConnectLib:0.2.6.3-STABLE")
108+
implementation("fr.sandro642.github:ConnectLib:0.2.6.4-STABLE")
108109

109110
}
110111

@@ -140,7 +141,7 @@ public class Example {
140141
}
141142
```
142143

143-
More examples HERE: [ExampleUsage.java](src/main/java/fr/sandro642/github/example/ExampleUsage.java) Not available at the moment due to new features.
144+
More examples HERE: [ExampleUsages.java](src/main/java/fr/sandro642/github/example/ExampleUsages.java)
144145

145146
---
146147

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package fr.sandro642.github.example;
2+
3+
import fr.sandro642.github.ConnectLib;
4+
import fr.sandro642.github.api.ApiFactory;
5+
import fr.sandro642.github.enums.MethodType;
6+
import fr.sandro642.github.enums.ResourceType;
7+
import fr.sandro642.github.enums.VersionType;
8+
import fr.sandro642.github.jobs.JobGetInfos;
9+
import fr.sandro642.github.utils.ConvertEnum;
10+
11+
import java.util.Map;
12+
import java.util.concurrent.CompletableFuture;
13+
14+
/**
15+
* ExampleUsages is a placeholder class that can be used to demonstrate how to use the ConnectLib library.
16+
* It can contain example methods or code snippets that show how to interact with the API, handle responses,
17+
* and utilize the features provided by the ConnectLib library.
18+
*
19+
* @author Sandro642
20+
* @version 1.0
21+
*/
22+
23+
public class ExampleUsages {
24+
25+
public enum ExampleRoutes implements ConvertEnum.RouteImport {
26+
EXAMPLE_ROUTE("/api/example/route");
27+
28+
final String route;
29+
30+
ExampleRoutes(String route) {
31+
this.route = route;
32+
}
33+
34+
@Override
35+
public String route() {
36+
return route;
37+
}
38+
}
39+
40+
public void initializeLib() {
41+
42+
// Optionally, you can specify routes if needed
43+
ConnectLib.initialize(ResourceType.MAIN_RESOURCES, ExampleRoutes.class);
44+
// You can also initialize without specifying routes
45+
ConnectLib.initialize(ResourceType.MAIN_RESOURCES);
46+
}
47+
48+
// Add methods here to demonstrate how to use the ConnectLib library
49+
// For example, you can create methods to make API calls, handle responses, etc.
50+
51+
// Example method to demonstrate usage
52+
public void exampleMethodSync() {
53+
// This method can be used to demonstrate how to interact with the API
54+
// For example, making a GET request to the EXAMPLE_ROUTE
55+
ApiFactory response = ConnectLib.JobGetInfos()
56+
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE)
57+
.getResponse()
58+
.block();
59+
60+
System.out.println(response.display());
61+
System.out.println("Response Code: " + response.getData("code"));
62+
System.out.println("Response Message: " + response.getData("message"));
63+
System.out.println("Response Data: " + response.getSpecData("data", "exampleKey"));
64+
}
65+
66+
// Example method to demonstrate asynchronous usage
67+
public void exampleMethodAsync() {
68+
try {
69+
// This method can be used to demonstrate how to interact with the API asynchronously
70+
71+
// Create a CompletableFuture to handle the asynchronous response
72+
CompletableFuture<ApiFactory> futureResponse = new CompletableFuture<>();
73+
74+
ConnectLib.JobGetInfos()
75+
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE)
76+
.getResponse()
77+
.subscribe(
78+
futureResponse::complete,
79+
futureResponse::completeExceptionally
80+
);
81+
82+
// Handle the response when it completes
83+
ApiFactory response = futureResponse.get(10, TimeUnit.SECONDS);
84+
85+
System.out.println(response.display());
86+
System.out.println("Response Code: " + response.getData("code"));
87+
System.out.println("Response Message: " + response.getData("message"));
88+
System.out.println("Response Data: " + response.getSpecData("data", "exampleKey"));
89+
} catch (java.util.concurrent.TimeoutException e) {
90+
System.err.println("The operation timed out: " + e.getMessage());
91+
} catch (Exception e) {
92+
e.printStackTrace();
93+
}
94+
}
95+
96+
// Example to use all methods in JobGetInfos
97+
public void exampleJobGetInfos() {
98+
Map<String, ?> body = Map.of();
99+
Map<String,?> params = Map.of();
100+
101+
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE, body, params);
102+
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.POST, ExampleRoutes.EXAMPLE_ROUTE, body);
103+
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.PUT, ExampleRoutes.EXAMPLE_ROUTE, null, params);
104+
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.PATCH, ExampleRoutes.EXAMPLE_ROUTE);
105+
ConnectLib.JobGetInfos().getRoutes(MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE, body, params);
106+
ConnectLib.JobGetInfos().getRoutes(MethodType.POST, ExampleRoutes.EXAMPLE_ROUTE, body);
107+
ConnectLib.JobGetInfos().getRoutes(MethodType.DELETE, ExampleRoutes.EXAMPLE_ROUTE);
108+
}
109+
110+
}

src/main/java/fr/sandro642/github/jobs/JobGetInfos.java

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import fr.sandro642.github.api.ApiFactory;
66
import fr.sandro642.github.enums.MethodType;
77
import fr.sandro642.github.enums.VersionType;
8+
import reactor.core.publisher.Mono;
89

910
import java.util.Map;
1011

@@ -108,40 +109,6 @@ public JobGetInfos getRoutes(MethodType methodType, String routeName) {
108109
return getRoutes(null, methodType, routeName, null, null);
109110
}
110111

111-
/**
112-
* Get routes from the YAML file and builds the full URL with a request body.
113-
* @param methodType Type of HTTP method (GET, POST)
114-
* @param routeName Name of the route in the YAML file
115-
* @param body Body of the request for POST (can be null for GET)
116-
* @return JobGetInfos for chaining
117-
*/
118-
public JobGetInfos getRoutesWithBody(MethodType methodType, String routeName, Map<String, ?> body) {
119-
return getRoutes(null, methodType, routeName, body, null);
120-
}
121-
122-
/**
123-
* Get routes from the YAML file and builds the full URL with additional parameters.
124-
* @param methodType Type of HTTP method (GET, POST)
125-
* @param routeName Name of the route in the YAML file
126-
* @param params Additional parameters for the request
127-
* @return JobGetInfos for chaining
128-
*/
129-
public JobGetInfos getRoutesWithParams(MethodType methodType, String routeName, Map<String, ?> params) {
130-
return getRoutes(null, methodType, routeName, null, params);
131-
}
132-
133-
/**
134-
* Get routes from the YAML file and builds the full URL with a request body and additional parameters.
135-
* @param methodType Type of HTTP method (GET, POST)
136-
* @param routeName Name of the route in the YAML file
137-
* @param body Body of the request for POST (can be null for GET)
138-
* @param params Additional parameters for the request
139-
* @return JobGetInfos for chaining
140-
*/
141-
public JobGetInfos getRoutesBoth(MethodType methodType, String routeName, Map<String, ?> body, Map<String, ?> params) {
142-
return getRoutes(null, methodType, routeName, body, params);
143-
}
144-
145112
/**
146113
* Récupère les routes depuis le fichier YAML et construit l'URL complète
147114
* @param versionType Version de l'API (V1_BRANCH, V2_BRANCH)
@@ -220,7 +187,7 @@ public <R> JobGetInfos getRoutes(VersionType versionType, MethodType methodType,
220187
* makes the API call, and returns the response as an ApiFactory object.
221188
* @return ApiFactory containing the response from the API, or null if an error occurs.
222189
*/
223-
public ApiFactory getResponse() {
190+
public Mono<ApiFactory> getResponse() {
224191
try {
225192

226193
String route = (String) ConnectLib.StoreAndRetrieve().store.get("currentRoute");
@@ -231,23 +198,23 @@ public ApiFactory getResponse() {
231198
throw new RuntimeException("Route or method not set. Please call getRoutes() first.");
232199
}
233200

234-
ApiFactory response;
201+
Mono<ApiFactory> response;
235202

236203
switch(method) {
237204
case GET:
238-
response = apiClient.callAPIGet(route).block();
205+
response = apiClient.callAPIGet(route);
239206
break;
240207
case POST:
241-
response = apiClient.callAPIPost(route, body).block();
208+
response = apiClient.callAPIPost(route, body);
242209
break;
243210
case PUT:
244-
response = apiClient.callAPIPut(route, body).block();
211+
response = apiClient.callAPIPut(route, body);
245212
break;
246213
case PATCH:
247-
response = apiClient.callAPIPatch(route, body).block();
214+
response = apiClient.callAPIPatch(route, body);
248215
break;
249216
case DELETE:
250-
response = apiClient.callAPIDelete(route).block();
217+
response = apiClient.callAPIDelete(route);
251218
break;
252219
default:
253220
ConnectLib.Logger().ERROR("Unsupported method type: " + method);

src/test/java/fr/sandro642/github/test/MainTest.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import fr.sandro642.github.utils.ConvertEnum;
1010
import org.junit.jupiter.api.Test;
1111

12+
import java.util.concurrent.CompletableFuture;
13+
import java.util.concurrent.TimeUnit;
14+
1215
/**
1316
* MainTest is a test class for the ConnectLib library.
1417
* @author Sandro642
@@ -45,12 +48,21 @@ public static void main(String[] args) {
4548
ConnectLib.initialize(ResourceType.TEST_RESOURCES, TestRoutes.class);
4649

4750
try {
51+
CompletableFuture<ApiFactory> futureResponse = new CompletableFuture<>();
4852

49-
ApiFactory response = ConnectLib.JobGetInfos()
53+
ConnectLib.JobGetInfos()
5054
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, TestRoutes.VERSION)
51-
.getResponse();
55+
.getResponse()
56+
.subscribe(
57+
futureResponse::complete,
58+
futureResponse::completeExceptionally
59+
);
60+
61+
ApiFactory response = futureResponse.get(5, TimeUnit.SECONDS);
62+
63+
System.out.println("Response: " + response.display());
64+
5265

53-
System.out.println(response.getSpecData("data", "version"));
5466
} catch (Exception e) {
5567
return;
5668
}
@@ -62,8 +74,11 @@ public void testUseFullRoute() {
6274

6375
try {
6476
ApiFactory response = ConnectLib.JobGetInfos()
65-
.getRoutes(VersionType.V1_BRANCH, MethodType.POST, TestRoutes.INFO, null, null)
66-
.getResponse();
77+
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, TestRoutes.VERSION)
78+
.getResponse()
79+
.block();
80+
81+
System.out.println("Response: " + response.display());
6782

6883
} catch (Exception e) {
6984
e.printStackTrace();

0 commit comments

Comments
 (0)