-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobGetInfos.java
More file actions
352 lines (308 loc) · 14.9 KB
/
Copy pathJobGetInfos.java
File metadata and controls
352 lines (308 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package fr.sandro642.github.jobs;
import fr.sandro642.github.ConnectLib;
import fr.sandro642.github.api.ApiClient;
import fr.sandro642.github.api.ApiFactory;
import fr.sandro642.github.enums.MethodType;
import fr.sandro642.github.enums.lang.CategoriesType;
import fr.sandro642.github.provider.URLProvider;
import fr.sandro642.github.provider.VersionProvider;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 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
*/
public class JobGetInfos {
/**
* ApiClient is used to make API calls.
* It is initialized in the constructor of JobGetInfos.
*/
private ApiClient apiClient;
/**
* connectLib instance to access its methods and properties.
*/
private ConnectLib connectLib = new ConnectLib();
/**
* URLProvider instance to provide custom URL branches.
* If not set, the default URL from the configuration will be used.
*/
private URLProvider urlBranch;
/**
* Constructor of JobGetInfos.
* Initializes the ApiClient and loads the YAML configuration.
*/
public JobGetInfos() {
connectLib.YamlUtils();
}
/**
* 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.
*/
private String getRouteName(Enum<?> routeName) {
return routeName.name().toLowerCase();
}
/**
* 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)
* @return JobGetInfos for chaining
*/
public JobGetInfos getRoutes(VersionProvider versionType, MethodType methodType, Enum<?> routeName, Map<String, ?> body) {
return getRoutes(versionType, methodType, getRouteName(routeName), body, null, null);
}
/**
* 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
* @return JobGetInfos for chaining
*/
public JobGetInfos getRoutes(VersionProvider versionType, MethodType methodType, Enum<?> routeName) {
return getRoutes(versionType, methodType, getRouteName(routeName), null, 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
* @return JobGetInfos for chaining
*/
public JobGetInfos getRoutes(MethodType methodType, Enum<?> routeName) {
return getRoutes(null, methodType, getRouteName(routeName), null, null, null);
}
/**
* 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)
* @return JobGetInfos for chaining
*/
public JobGetInfos getRoutes(MethodType methodType, Enum<?> routeName, Map<String, ?> body) {
return getRoutes(null, methodType, getRouteName(routeName), body, null, 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 getRoutes(MethodType methodType, Enum<?> routeName, Map<String, ?> body, Map<String, ?> params) {
return getRoutes(null, methodType, getRouteName(routeName), body, params, null);
}
/**
* Get routes from the YAML file and builds the full URL.
* @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(MethodType methodType, String routeName) {
return getRoutes(null, methodType, routeName, null, null, null);
}
/**
* 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)
* @param methodType Type de méthode HTTP (GET, POST)
* @param routeName Nom de la route dans le fichier YAML
* @return JobGetInfos pour chaînage
*/
public JobGetInfos getRoutes(VersionProvider versionType, MethodType methodType, String routeName) {
return getRoutes(versionType, methodType, routeName, null, null, null);
}
/**
* Get routes from the YAML file and builds the full URL with a request body.
* @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)
* @return JobGetInfos for chaining
*/
public JobGetInfos getRoutes(VersionProvider versionType, MethodType methodType, String routeName, Map<String, ?> body) {
return getRoutes(versionType, methodType, routeName, body, null, null);
}
/**
* 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
* @return JobGetInfos for chaining
*/
public JobGetInfos getRoutes(MethodType methodType, String routeName, Map<String, ?> body, Map<String, ?> params, Map<String, ?> query) {
return getRoutes(null, methodType, routeName, body, params, query);
}
/**
* 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 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, Enum<?> routeName, Map<String, ?> body, Map<String, ?> params, Map<String, ?> query) {
return getRoutes(null, methodType, getRouteName(routeName), body, params, query);
}
/**
* Get routes from the YAML file and builds the full URL with additional 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 params Additional parameters for the request
* @param query Additional query parameters for the request
* @return JobGetInfos for chaining
*/
public <R> JobGetInfos getRoutes(VersionProvider versionType, MethodType methodType, R routeName, Map<String, ?> body, Map<String, ?> params, Map<String, ?> query) {
try {
String route = connectLib.getRoute(routeName.toString().toLowerCase());
String fullRoute;
if (versionType != null && versionType.getVersion() != null) {
String cleanRoute = route.startsWith("/") ? route.substring(1) : route;
fullRoute = "/" + versionType.getVersion() + "/" + cleanRoute;
} else {
fullRoute = route;
}
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, ?> entry : params.entrySet()) {
String paramKey = "{" + entry.getKey() + "}";
String paramValue = entry.getValue().toString();
fullRoute = fullRoute.replace(paramKey, paramValue);
}
}
// New query string handling logic
if (query != null && !query.isEmpty()) {
// Pattern for capturing parameters between $
Pattern pattern = Pattern.compile("\\$([^$]+)\\$");
Matcher matcher = pattern.matcher(fullRoute);
StringBuilder queryString = new StringBuilder("?");
boolean firstParam = true;
// Iterate through all matches (parameters between $)
while (matcher.find()) {
String paramName = matcher.group(1); // Get parameter name without $
// Check if this parameter exists in the query map
if (query.containsKey(paramName)) {
if (!firstParam) {
queryString.append("&");
}
queryString.append(paramName).append("=").append(query.get(paramName));
firstParam = false;
}
}
// Append query string to the route only if parameters were added
if (queryString.length() > 1) {
// Clean the route by removing $parameter$ placeholders and extra &
String cleanRoute = fullRoute.replaceAll("\\$[^$]+\\$", "").replaceAll("&+", "");
fullRoute = cleanRoute + queryString.toString();
}
}
connectLib.StoreAndRetrieve().store.put("currentRoute", fullRoute);
connectLib.StoreAndRetrieve().store.put("currentMethod", methodType);
if (body != null) {
connectLib.StoreAndRetrieve().store.put("currentBody", body);
}
if (params != null) {
connectLib.StoreAndRetrieve().store.put("currentParams", params);
}
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.JOBS_PACKAGE, "getroutes.maderoute", "route", fullRoute));
} catch (Exception e) {
connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.JOBS_PACKAGE, "getroutes.error", "exception", e.getMessage()));
}
return this;
}
/**
* 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
*/
public JobGetInfos urlBranch(URLProvider urlBranch) {
this.urlBranch = urlBranch;
return this;
}
/**
* 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<ApiFactory> execute() {
try {
String route = (String) connectLib.StoreAndRetrieve().store.get("currentRoute");
MethodType method = (MethodType) connectLib.StoreAndRetrieve().store.get("currentMethod");
Map<String, Object> body = (Map<String, Object>) connectLib.StoreAndRetrieve().store.get("currentBody");
if (route == null || method == null) {
connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.JOBS_PACKAGE, "getresponse.mustbe"));
}
/** Determine the URL branch to use */
String urlBranchLambda;
if (urlBranch == null) {
urlBranchLambda = (String) connectLib.StoreAndRetrieve().store.get(connectLib.StoreAndRetrieve().URL_KEY);
} else {
urlBranchLambda = urlBranch.getURL();
}
apiClient = new ApiClient(urlBranchLambda);
CompletableFuture<ApiFactory> responseFuture = new CompletableFuture<>();
// Callback to clean up the store after the response
Consumer<ApiFactory> onSuccess = response -> {
connectLib.StoreAndRetrieve().store.remove("currentRoute");
connectLib.StoreAndRetrieve().store.remove("currentMethod");
connectLib.StoreAndRetrieve().store.remove("currentBody");
responseFuture.complete(response);
};
Consumer<Throwable> onError = error -> {
connectLib.StoreAndRetrieve().store.remove("currentRoute");
connectLib.StoreAndRetrieve().store.remove("currentMethod");
connectLib.StoreAndRetrieve().store.remove("currentBody");
responseFuture.completeExceptionally(error);
};
switch(method) {
case GET:
apiClient.callAPIGet(route).subscribe(
response -> onSuccess.accept(response),
onError
);
break;
case POST:
apiClient.callAPIPost(route, body).subscribe(
response -> onSuccess.accept(response),
onError
);
break;
case PUT:
apiClient.callAPIPut(route, body).subscribe(
response -> onSuccess.accept(response),
onError
);
break;
case PATCH:
apiClient.callAPIPatch(route, body).subscribe(
response -> onSuccess.accept(response),
onError
);
break;
case DELETE:
apiClient.callAPIDelete(route).subscribe(
response -> onSuccess.accept(response),
onError
);
break;
default:
connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.JOBS_PACKAGE, "getresponse.error", "type", method.toString()));
System.exit(2);
}
return responseFuture;
} catch (Exception e) {
return CompletableFuture.failedFuture(e);
}
}
}