Skip to content

Commit d633654

Browse files
committed
Implement asynchronous response handling in ApiClient and update version to 0.2.7.2-STABLE
1 parent 484e149 commit d633654

6 files changed

Lines changed: 88 additions & 57 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.7.1-STABLE'
8+
version = '0.2.7.2-STABLE'
99

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

readme.md

Lines changed: 3 additions & 4 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.7.1-STABLE
20+
Stable Version: 0.2.7.2-STABLE
2121
```
2222

2323
---
@@ -48,6 +48,7 @@ Changelog:
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.
5050
- [0.2.6.4-STABLE]: Added asynchronous job execution, allowing you to run tasks in the background without blocking your main application thread.
51+
- [0.2.7.2-STABLE]: Remove implementation Project Reactor
5152
```
5253

5354
---
@@ -105,9 +106,7 @@ repositories {
105106

106107
dependencies {
107108

108-
implementation("fr.sandro642.github:ConnectLib:0.2.7.1-STABLE")
109-
// Importation de Project Reactor pour une utilisation complète de ConnectLib
110-
implementation ("io.projectreactor:reactor-core:3.6.9")
109+
implementation("fr.sandro642.github:ConnectLib:0.2.7.2-STABLE")
111110

112111
}
113112

src/main/java/fr/sandro642/github/api/ApiClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public Mono<ApiFactory> callAPIGet(String routeName) {
7474
.retrieve()
7575
.bodyToMono(String.class)
7676
.subscribeOn(Schedulers.boundedElastic())
77+
.doOnNext(thread ->
78+
ConnectLib.Logger().INFO("Thread en cours d'utilisation: " + Thread.currentThread().getName()))
7779
.map(rawJson -> {
7880
response.parseFromRawJson(rawJson);
7981
return response;
@@ -96,6 +98,8 @@ public Mono<ApiFactory> callAPIPost(String routeName, Map<String, Object> body)
9698
.retrieve()
9799
.bodyToMono(String.class)
98100
.subscribeOn(Schedulers.boundedElastic())
101+
.doOnNext(thread ->
102+
ConnectLib.Logger().INFO("Thread en cours d'utilisation: " + Thread.currentThread().getName()))
99103
.map(rawJson -> {
100104
response.parseFromRawJson(rawJson);
101105
return response;
@@ -118,6 +122,8 @@ public Mono<ApiFactory> callAPIPut(String routeName, Map<String, Object> body) {
118122
.retrieve()
119123
.bodyToMono(String.class)
120124
.subscribeOn(Schedulers.boundedElastic())
125+
.doOnNext(thread ->
126+
ConnectLib.Logger().INFO("Thread en cours d'utilisation: " + Thread.currentThread().getName()))
121127
.map(rawJson -> {
122128
response.parseFromRawJson(rawJson);
123129
return response;
@@ -140,6 +146,8 @@ public Mono<ApiFactory> callAPIPatch(String routeName, Map<String, Object> body)
140146
.retrieve()
141147
.bodyToMono(String.class)
142148
.subscribeOn(Schedulers.boundedElastic())
149+
.doOnNext(thread ->
150+
ConnectLib.Logger().INFO("Thread en cours d'utilisation: " + Thread.currentThread().getName()))
143151
.map(rawJson -> {
144152
response.parseFromRawJson(rawJson);
145153
return response;
@@ -160,6 +168,8 @@ public Mono<ApiFactory> callAPIDelete(String routeName) {
160168
.retrieve()
161169
.bodyToMono(String.class)
162170
.subscribeOn(Schedulers.boundedElastic())
171+
.doOnNext(thread ->
172+
ConnectLib.Logger().INFO("Thread en cours d'utilisation: " + Thread.currentThread().getName()))
163173
.map(rawJson -> {
164174
response.parseFromRawJson(rawJson);
165175
return response;

src/main/java/fr/sandro642/github/example/ExampleUsages.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,44 +50,39 @@ public void initializeLib() {
5050

5151
// Example method to demonstrate usage
5252
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"));
53+
try {
54+
// This method can be used to demonstrate how to interact with the API
55+
// For example, making a GET request to the EXAMPLE_ROUTE
56+
CompletableFuture<ApiFactory> apiFactoryCompletableFuture = ConnectLib.JobGetInfos()
57+
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE)
58+
.getResponse();
59+
60+
ApiFactory response = apiFactoryCompletableFuture.get(5, TimeUnit.SECONDS);
61+
62+
System.out.println(response.display());
63+
System.out.println("Response Code: " + response.getData("code"));
64+
System.out.println("Response Message: " + response.getData("message"));
65+
System.out.println("Response Data: " + response.getSpecData("data", "exampleKey"));
66+
} catch (Exception e) {
67+
e.printStackTrace();
68+
}
6469
}
6570

6671
// Example method to demonstrate asynchronous usage
6772
public void exampleMethodAsync() {
6873
try {
6974
// This method can be used to demonstrate how to interact with the API asynchronously
7075

71-
// Create a CompletableFuture to handle the asynchronous response
72-
CompletableFuture<ApiFactory> futureResponse = new CompletableFuture<>();
73-
74-
ConnectLib.JobGetInfos()
76+
CompletableFuture<ApiFactory> apiFactoryCompletableFuture = ConnectLib.JobGetInfos()
7577
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE)
76-
.getResponse()
77-
.subscribe(
78-
futureResponse::complete,
79-
futureResponse::completeExceptionally
80-
);
78+
.getResponse();
8179

82-
// Handle the response when it completes
83-
ApiFactory response = futureResponse.get(10, TimeUnit.SECONDS);
80+
ApiFactory response = apiFactoryCompletableFuture.get(5, TimeUnit.SECONDS);
8481

8582
System.out.println(response.display());
8683
System.out.println("Response Code: " + response.getData("code"));
8784
System.out.println("Response Message: " + response.getData("message"));
8885
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());
9186
} catch (Exception e) {
9287
e.printStackTrace();
9388
}

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

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import fr.sandro642.github.enums.MethodType;
77
import fr.sandro642.github.enums.VersionType;
88
import reactor.core.publisher.Mono;
9+
import reactor.core.scheduler.Scheduler;
910

1011
import java.util.Map;
12+
import java.util.concurrent.CompletableFuture;
13+
import java.util.function.Consumer;
1114

1215
/**
1316
* JobGetInfos is a utility class for managing API requests in the ConnectLib library.
@@ -187,49 +190,77 @@ public <R> JobGetInfos getRoutes(VersionType versionType, MethodType methodType,
187190
* makes the API call, and returns the response as an ApiFactory object.
188191
* @return ApiFactory containing the response from the API, or null if an error occurs.
189192
*/
190-
public Mono<ApiFactory> getResponse() {
193+
public CompletableFuture<ApiFactory> getResponse() {
191194
try {
192-
193195
String route = (String) ConnectLib.StoreAndRetrieve().store.get("currentRoute");
194196
MethodType method = (MethodType) ConnectLib.StoreAndRetrieve().store.get("currentMethod");
195197
Map<String, Object> body = (Map<String, Object>) ConnectLib.StoreAndRetrieve().store.get("currentBody");
196198

197199
if (route == null || method == null) {
198-
throw new RuntimeException("Route or method not set. Please call getRoutes() first.");
200+
return CompletableFuture.failedFuture(
201+
new RuntimeException("Route or method not set. Please call getRoutes() first.")
202+
);
199203
}
200204

201-
Mono<ApiFactory> response;
205+
CompletableFuture<ApiFactory> responseFuture = new CompletableFuture<>();
206+
207+
// Callback pour nettoyer le store après la réponse
208+
Consumer<ApiFactory> onSuccess = response -> {
209+
ConnectLib.StoreAndRetrieve().store.remove("currentRoute");
210+
ConnectLib.StoreAndRetrieve().store.remove("currentMethod");
211+
ConnectLib.StoreAndRetrieve().store.remove("currentBody");
212+
responseFuture.complete(response);
213+
};
214+
215+
Consumer<Throwable> onError = error -> {
216+
ConnectLib.StoreAndRetrieve().store.remove("currentRoute");
217+
ConnectLib.StoreAndRetrieve().store.remove("currentMethod");
218+
ConnectLib.StoreAndRetrieve().store.remove("currentBody");
219+
responseFuture.completeExceptionally(error);
220+
};
202221

203222
switch(method) {
204223
case GET:
205-
response = apiClient.callAPIGet(route);
224+
apiClient.callAPIGet(route).subscribe(
225+
response -> onSuccess.accept((ApiFactory) response),
226+
onError
227+
);
206228
break;
207229
case POST:
208-
response = apiClient.callAPIPost(route, body);
230+
apiClient.callAPIPost(route, body).subscribe(
231+
response -> onSuccess.accept((ApiFactory) response),
232+
onError
233+
);
209234
break;
210235
case PUT:
211-
response = apiClient.callAPIPut(route, body);
236+
apiClient.callAPIPut(route, body).subscribe(
237+
response -> onSuccess.accept((ApiFactory) response),
238+
onError
239+
);
212240
break;
213241
case PATCH:
214-
response = apiClient.callAPIPatch(route, body);
242+
apiClient.callAPIPatch(route, body).subscribe(
243+
response -> onSuccess.accept((ApiFactory) response),
244+
onError
245+
);
215246
break;
216247
case DELETE:
217-
response = apiClient.callAPIDelete(route);
248+
apiClient.callAPIDelete(route).subscribe(
249+
response -> onSuccess.accept((ApiFactory) response),
250+
onError
251+
);
218252
break;
219253
default:
220254
ConnectLib.Logger().ERROR("Unsupported method type: " + method);
221-
return null;
255+
return CompletableFuture.failedFuture(
256+
new IllegalArgumentException("Unsupported method type: " + method)
257+
);
222258
}
223259

224-
// Nettoie le store après utilisation
225-
ConnectLib.StoreAndRetrieve().store.remove("currentRoute");
226-
ConnectLib.StoreAndRetrieve().store.remove("currentMethod");
227-
ConnectLib.StoreAndRetrieve().store.remove("currentBody");
228-
229-
return response;
260+
return responseFuture;
230261

231262
} catch (Exception e) {
232-
return null;
263+
return CompletableFuture.failedFuture(e);
233264
}
234265
}
235266
}

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,12 @@ public static void main(String[] args) {
4848
ConnectLib.initialize(ResourceType.TEST_RESOURCES, TestRoutes.class);
4949

5050
try {
51-
CompletableFuture<ApiFactory> futureResponse = new CompletableFuture<>();
5251

53-
ConnectLib.JobGetInfos()
52+
CompletableFuture<ApiFactory> apiFactoryCompletableFuture = ConnectLib.JobGetInfos()
5453
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, TestRoutes.VERSION)
55-
.getResponse()
56-
.subscribe(
57-
futureResponse::complete,
58-
futureResponse::completeExceptionally
59-
);
54+
.getResponse();
6055

61-
ApiFactory response = futureResponse.get(5, TimeUnit.SECONDS);
56+
ApiFactory response = apiFactoryCompletableFuture.get(5, TimeUnit.SECONDS);
6257

6358
System.out.println("Response: " + response.display());
6459

@@ -73,10 +68,11 @@ public void testUseFullRoute() {
7368
ConnectLib.initialize(ResourceType.TEST_RESOURCES, TestRoutes.class);
7469

7570
try {
76-
ApiFactory response = ConnectLib.JobGetInfos()
71+
CompletableFuture<ApiFactory> factoryCompletableFuture = ConnectLib.JobGetInfos()
7772
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, TestRoutes.VERSION)
78-
.getResponse()
79-
.block();
73+
.getResponse();
74+
75+
ApiFactory response = factoryCompletableFuture.get(5, TimeUnit.SECONDS);
8076

8177
System.out.println("Response: " + response.display());
8278

0 commit comments

Comments
 (0)