Skip to content

Commit 9157ac1

Browse files
committed
Enhance API client request handling with dynamic request creation and status updates; add application running state management
1 parent 4bf6a05 commit 9157ac1

7 files changed

Lines changed: 65 additions & 713 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.4.8-STABLE'
8+
version = '0.4.8.1-STABLE'
99

1010
// Générer une classe de version automatiquement
1111
task generateVersionClass {

src/main/java/fr/sandro642/github/ConnectLib.java

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public ConnectLib init(ResourceType resourceType, LangType langType, Class<? ext
6767
rlv = new RetrieveLastVersion();
6868
isInitialized = true;
6969

70+
StoreAndRetrieve().put(StoreAndRetrieve().IS_APP_RUNNING, false);
71+
7072
rlv.isLatestVersion();
7173

7274
HookManager().initHook(resourceType);
@@ -127,35 +129,6 @@ public Map<String, String> getRoutesMap() {
127129
return new HashMap<>(routes);
128130
}
129131

130-
/**
131-
* Check if the ConnectLib supports web services.
132-
* This method starts the Spring application.
133-
*/
134-
public void webServices() {
135-
storeAndRetrieve.put(storeAndRetrieve.PORT, 3000);
136-
SpringApp().startApplication().subscribe();
137-
}
138-
139-
/**
140-
* Check if the ConnectLib supports web services on a specific port with a dashboard name.
141-
* This method starts the Spring application.
142-
* @param port the port number to run the web services on
143-
* If port is 0, the default port 3000 will be used
144-
* @param nameDashboard the name of the dashboard
145-
*/
146-
public void webServices(int port, String nameDashboard) {
147-
storeAndRetrieve.put(storeAndRetrieve.NAME_DASHBOARD, nameDashboard);
148-
149-
if (port == 0) {
150-
storeAndRetrieve.put(storeAndRetrieve.PORT, 3000);
151-
} else {
152-
storeAndRetrieve.put(storeAndRetrieve.PORT, port);
153-
}
154-
155-
156-
SpringApp().startApplication().subscribe();
157-
}
158-
159132
/**
160133
* Implement WAN connectivity using the provided server URL and component name.
161134
* This method performs HTTP requests to establish a connection and retrieve necessary information.

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

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ public ApiClient(String baseUrlLambda) {
7777
public Mono<ApiFactory> callAPIGet(String routeName) {
7878
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.get", Map.of("routename", routeName)));
7979

80-
Request r = DataController.getInstance().createRequest(routeName, baseUrl);
80+
AtomicReference<Request> requestRef = new AtomicReference<>();
81+
82+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().IS_APP_RUNNING).equals(true)) {
83+
requestRef.set(DataController.getInstance().createRequest(routeName, baseUrl));
84+
}
8185

8286
record ResponseData(int statusCode, String body) {}
8387

@@ -93,14 +97,19 @@ record ResponseData(int statusCode, String body) {}
9397

9498
String newStatus = (responseData.statusCode() >= 200 && responseData.statusCode() < 300) ? "success" : "error";
9599
try {
96-
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
100+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().IS_APP_RUNNING).equals(true)) {
101+
Request r = requestRef.get();
102+
if (r != null) {
103+
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
104+
}
105+
}
97106
} catch (Exception e) {
98107
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "GET", "exception", e.getMessage())));
99108
}
100109
})
101110
.map(responseData -> {
102111
apiFactory.setStatusCode(responseData.statusCode());
103-
apiFactory.parseFromRawJson(responseData.body);
112+
apiFactory.parseFromRawJson(responseData.body());
104113
return apiFactory;
105114
})
106115
.doOnNext(lastResponse::set)
@@ -116,7 +125,11 @@ record ResponseData(int statusCode, String body) {}
116125
public Mono<ApiFactory> callAPIPost(String routeName, Map<String, Object> body) {
117126
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.post", Map.of("routename", routeName)));
118127

119-
Request r = DataController.getInstance().createRequest(routeName, baseUrl);
128+
AtomicReference<Request> requestRef = new AtomicReference<>();
129+
130+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().IS_APP_RUNNING).equals(true)) {
131+
requestRef.set(DataController.getInstance().createRequest(routeName, baseUrl));
132+
}
120133

121134
record ResponseData(int statusCode, String body) {}
122135

@@ -133,7 +146,12 @@ record ResponseData(int statusCode, String body) {}
133146

134147
String newStatus = (responseData.statusCode() >= 200 && responseData.statusCode() < 300) ? "success" : "error";
135148
try {
136-
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
149+
Request r = requestRef.get();
150+
if (r != null) {
151+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().IS_APP_RUNNING).equals(true)) {
152+
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
153+
}
154+
}
137155
} catch (Exception e) {
138156
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "GET", "exception", e.getMessage())));
139157
}
@@ -156,7 +174,10 @@ record ResponseData(int statusCode, String body) {}
156174
public Mono<ApiFactory> callAPIPut(String routeName, Map<String, Object> body) {
157175
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.put", Map.of("routename", routeName)));
158176

159-
Request r = DataController.getInstance().createRequest(routeName, baseUrl);
177+
AtomicReference<Request> requestRef = new AtomicReference<>();
178+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().IS_APP_RUNNING).equals(true)) {
179+
requestRef.set(DataController.getInstance().createRequest(routeName, baseUrl));
180+
}
160181

161182
record ResponseData(int statusCode, String body) {}
162183

@@ -173,9 +194,14 @@ record ResponseData(int statusCode, String body) {}
173194

174195
String newStatus = (responseData.statusCode() >= 200 && responseData.statusCode() < 300) ? "success" : "error";
175196
try {
176-
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
197+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().IS_APP_RUNNING).equals(true)) {
198+
Request r = requestRef.get();
199+
if (r != null) {
200+
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
201+
}
202+
}
177203
} catch (Exception e) {
178-
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "GET", "exception", e.getMessage())));
204+
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "PUT", "exception", e.getMessage())));
179205
}
180206
})
181207
.map(responseData -> {
@@ -190,13 +216,16 @@ record ResponseData(int statusCode, String body) {}
190216
/**
191217
* Method to call the API with a PATCH request.
192218
* @param routeName Name of the route to call.
193-
* @param body Body of the request (can be null for a request without body).
219+
* @param body Body of the request (can be null for a request without body).
194220
* @return a Mono that emits the ApiFactory response containing the parsed JSON data.
195221
*/
196222
public Mono<ApiFactory> callAPIPatch(String routeName, Map<String, Object> body) {
197223
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.patch", Map.of("routename", routeName)));
198224

199-
Request r = DataController.getInstance().createRequest(routeName, baseUrl);
225+
AtomicReference<Request> requestRef = new AtomicReference<>();
226+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().IS_APP_RUNNING).equals(true)) {
227+
requestRef.set(DataController.getInstance().createRequest(routeName, baseUrl));
228+
}
200229

201230
record ResponseData(int statusCode, String body) {}
202231

@@ -213,9 +242,14 @@ record ResponseData(int statusCode, String body) {}
213242

214243
String newStatus = (responseData.statusCode() >= 200 && responseData.statusCode() < 300) ? "success" : "error";
215244
try {
216-
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
245+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().IS_APP_RUNNING).equals(true)) {
246+
Request r = requestRef.get();
247+
if (r != null) {
248+
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
249+
}
250+
}
217251
} catch (Exception e) {
218-
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "GET", "exception", e.getMessage())));
252+
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "PATCH", "exception", e.getMessage())));
219253
}
220254
})
221255
.map(responseData -> {
@@ -235,7 +269,10 @@ record ResponseData(int statusCode, String body) {}
235269
public Mono<ApiFactory> callAPIDelete(String routeName) {
236270
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.delete", Map.of("routename", routeName)));
237271

238-
Request r = DataController.getInstance().createRequest(routeName, baseUrl);
272+
AtomicReference<Request> requestRef = new AtomicReference<>();
273+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().IS_APP_RUNNING).equals(true)) {
274+
requestRef.set(DataController.getInstance().createRequest(routeName, baseUrl));
275+
}
239276

240277
record ResponseData(int statusCode, String body) {}
241278

@@ -251,9 +288,14 @@ record ResponseData(int statusCode, String body) {}
251288

252289
String newStatus = (responseData.statusCode() >= 200 && responseData.statusCode() < 300) ? "success" : "error";
253290
try {
254-
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
291+
if (connectLib.StoreAndRetrieve().get(connectLib.StoreAndRetrieve().IS_APP_RUNNING).equals(true)) {
292+
Request r = requestRef.get();
293+
if (r != null) {
294+
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
295+
}
296+
}
255297
} catch (Exception e) {
256-
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "GET", "exception", e.getMessage())));
298+
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "DELETE", "exception", e.getMessage())));
257299
}
258300
})
259301
.map(responseData -> {

src/main/java/fr/sandro642/github/misc/StoreAndRetrieve.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class StoreAndRetrieve {
2121
public final String URL_KEY = "baseUrl";
2222
public final String FILE_LOCATION_KEY = "fileLocation";
2323

24+
public final String IS_APP_RUNNING = "isAppRunning";
25+
2426
public final String NAME_DASHBOARD = "nameDashboard";
2527
public final String PORT = String.valueOf(3000);
2628
public final String DYNAMIC_PORT = "dynamicPort";

src/main/java/fr/sandro642/github/spring/Application.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public Mono<Void> startApplication() {
6666
app.setDefaultProperties(props);
6767
app.run();
6868

69+
connectLib.StoreAndRetrieve().put(connectLib.StoreAndRetrieve().IS_APP_RUNNING, true);
6970
}).subscribeOn(Schedulers.boundedElastic()).then();
7071
}
7172

0 commit comments

Comments
 (0)