Skip to content

Commit 8d19662

Browse files
authored
Merge pull request #36 from Sandro642/feature/query
Bump version to 0.2.9-STABLE, add support for query parameters in rou…
2 parents 8b558a0 + 342b141 commit 8d19662

5 files changed

Lines changed: 85 additions & 17 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.8.4-STABLE'
8+
version = '0.2.9-STABLE'
99

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

readme.md

Lines changed: 3 additions & 2 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.8.4-STABLE
20+
Stable Version: 0.2.9-STABLE
2121
```
2222

2323
---
@@ -49,6 +49,7 @@ Changelog:
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.
5151
- [0.2.7.2-STABLE]: Remove implementation Project Reactor
52+
- [0.2.9-STABLE]: Added support query variables in routes, allowing you to pass parameters directly in the URL.
5253
```
5354

5455
---
@@ -106,7 +107,7 @@ repositories {
106107

107108
dependencies {
108109

109-
implementation("fr.sandro642.github:ConnectLib:0.2.8.4-STABLE")
110+
implementation("fr.sandro642.github:ConnectLib:0.2.9-STABLE")
110111

111112
}
112113

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ public void exampleMethodAsync() {
9292
public void exampleJobGetInfos() {
9393
Map<String, ?> body = Map.of();
9494
Map<String,?> params = Map.of();
95+
Map<String, ?> query = Map.of();
9596

96-
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE, body, params);
97+
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE, body, params, query);
9798
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.POST, ExampleRoutes.EXAMPLE_ROUTE, body);
98-
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.PUT, ExampleRoutes.EXAMPLE_ROUTE, null, params);
99+
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.PUT, ExampleRoutes.EXAMPLE_ROUTE, null, params, query);
99100
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.PATCH, ExampleRoutes.EXAMPLE_ROUTE);
100101
ConnectLib.JobGetInfos().getRoutes(MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE, body, params);
101102
ConnectLib.JobGetInfos().getRoutes(MethodType.POST, ExampleRoutes.EXAMPLE_ROUTE, body);

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

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.Map;
1010
import java.util.concurrent.CompletableFuture;
1111
import java.util.function.Consumer;
12+
import java.util.regex.Matcher;
13+
import java.util.regex.Pattern;
1214

1315
/**
1416
* JobGetInfos is a utility class for managing API requests in the ConnectLib library.
@@ -54,7 +56,7 @@ private String getRouteName(Enum<?> routeName) {
5456
* @return JobGetInfos for chaining
5557
*/
5658
public JobGetInfos getRoutes(VersionType versionType, MethodType methodType, Enum<?> routeName, Map<String, ?> body) {
57-
return getRoutes(versionType, methodType, getRouteName(routeName), body, null);
59+
return getRoutes(versionType, methodType, getRouteName(routeName), body, null, null);
5860
}
5961

6062
/**
@@ -65,7 +67,7 @@ public JobGetInfos getRoutes(VersionType versionType, MethodType methodType, Enu
6567
* @return JobGetInfos for chaining
6668
*/
6769
public JobGetInfos getRoutes(VersionType versionType, MethodType methodType, Enum<?> routeName) {
68-
return getRoutes(versionType, methodType, getRouteName(routeName), null, null);
70+
return getRoutes(versionType, methodType, getRouteName(routeName), null, null, null);
6971
}
7072

7173
/**
@@ -75,7 +77,7 @@ public JobGetInfos getRoutes(VersionType versionType, MethodType methodType, Enu
7577
* @return JobGetInfos for chaining
7678
*/
7779
public JobGetInfos getRoutes(MethodType methodType, Enum<?> routeName) {
78-
return getRoutes(null, methodType, getRouteName(routeName), null, null);
80+
return getRoutes(null, methodType, getRouteName(routeName), null, null, null);
7981
}
8082

8183
/**
@@ -86,7 +88,7 @@ public JobGetInfos getRoutes(MethodType methodType, Enum<?> routeName) {
8688
* @return JobGetInfos for chaining
8789
*/
8890
public JobGetInfos getRoutes(MethodType methodType, Enum<?> routeName, Map<String, ?> body) {
89-
return getRoutes(null, methodType, getRouteName(routeName), body, null);
91+
return getRoutes(null, methodType, getRouteName(routeName), body, null, null);
9092
}
9193

9294
/**
@@ -97,7 +99,7 @@ public JobGetInfos getRoutes(MethodType methodType, Enum<?> routeName, Map<Strin
9799
* @return JobGetInfos for chaining
98100
*/
99101
public JobGetInfos getRoutes(MethodType methodType, Enum<?> routeName, Map<String, ?> body, Map<String, ?> params) {
100-
return getRoutes(null, methodType, getRouteName(routeName), body, params);
102+
return getRoutes(null, methodType, getRouteName(routeName), body, params, null);
101103
}
102104

103105
/**
@@ -107,7 +109,7 @@ public JobGetInfos getRoutes(MethodType methodType, Enum<?> routeName, Map<Strin
107109
* @return JobGetInfos for chaining
108110
*/
109111
public JobGetInfos getRoutes(MethodType methodType, String routeName) {
110-
return getRoutes(null, methodType, routeName, null, null);
112+
return getRoutes(null, methodType, routeName, null, null, null);
111113
}
112114

113115
/**
@@ -118,7 +120,7 @@ public JobGetInfos getRoutes(MethodType methodType, String routeName) {
118120
* @return JobGetInfos pour chaînage
119121
*/
120122
public JobGetInfos getRoutes(VersionType versionType, MethodType methodType, String routeName) {
121-
return getRoutes(versionType, methodType, routeName, null, null);
123+
return getRoutes(versionType, methodType, routeName, null, null, null);
122124
}
123125

124126
/**
@@ -130,7 +132,33 @@ public JobGetInfos getRoutes(VersionType versionType, MethodType methodType, Str
130132
* @return JobGetInfos for chaining
131133
*/
132134
public JobGetInfos getRoutes(VersionType versionType, MethodType methodType, String routeName, Map<String, ?> body) {
133-
return getRoutes(versionType, methodType, routeName, body, null);
135+
return getRoutes(versionType, methodType, routeName, body, null, null);
136+
}
137+
138+
/**
139+
* Get routes from the YAML file and builds the full URL with a request body and parameters.
140+
* @param methodType Type of HTTP method (GET, POST)
141+
* @param routeName Name of the route in the YAML file
142+
* @param body Body of the request for POST (can be null for GET)
143+
* @param params Additional parameters for the request
144+
* @param query Additional query parameters for the request
145+
* @return JobGetInfos for chaining
146+
*/
147+
public JobGetInfos getRoutes(MethodType methodType, String routeName, Map<String, ?> body, Map<String, ?> params, Map<String, ?> query) {
148+
return getRoutes(null, methodType, routeName, body, params, query);
149+
}
150+
151+
/**
152+
* Get routes from the YAML file and builds the full URL with additional parameters.
153+
* @param methodType Type of HTTP method (GET, POST)
154+
* @param routeName Name of the route in the YAML file
155+
* @param body Body of the request for POST (can be null for GET)
156+
* @param params Additional parameters for the request
157+
* @param query Additional query parameters for the request
158+
* @return JobGetInfos for chaining
159+
*/
160+
public JobGetInfos getRoutes(MethodType methodType, Enum<?> routeName, Map<String, ?> body, Map<String, ?> params, Map<String, ?> query) {
161+
return getRoutes(null, methodType, getRouteName(routeName), body, params, query);
134162
}
135163

136164
/**
@@ -139,9 +167,10 @@ public JobGetInfos getRoutes(VersionType versionType, MethodType methodType, Str
139167
* @param methodType Type of HTTP method (GET, POST)
140168
* @param routeName Name of the route in the YAML file
141169
* @param params Additional parameters for the request
170+
* @param query Additional query parameters for the request
142171
* @return JobGetInfos for chaining
143172
*/
144-
public <R> JobGetInfos getRoutes(VersionType versionType, MethodType methodType, R routeName, Map<String, ?> body, Map<String, ?> params) {
173+
public <R> JobGetInfos getRoutes(VersionType versionType, MethodType methodType, R routeName, Map<String, ?> body, Map<String, ?> params, Map<String, ?> query) {
145174
try {
146175
String route = ConnectLib.getRoute(routeName.toString().toLowerCase());
147176

@@ -157,11 +186,41 @@ public <R> JobGetInfos getRoutes(VersionType versionType, MethodType methodType,
157186
for (Map.Entry<String, ?> entry : params.entrySet()) {
158187
String paramKey = "{" + entry.getKey() + "}";
159188
String paramValue = entry.getValue().toString();
160-
161189
fullRoute = fullRoute.replace(paramKey, paramValue);
162190
}
163191
}
164192

193+
// New query string handling logic
194+
if (query != null && !query.isEmpty()) {
195+
// Pattern for capturing parameters between $
196+
Pattern pattern = Pattern.compile("\\$([^$]+)\\$");
197+
Matcher matcher = pattern.matcher(fullRoute);
198+
199+
StringBuilder queryString = new StringBuilder("?");
200+
boolean firstParam = true;
201+
202+
// Iterate through all matches (parameters between $)
203+
while (matcher.find()) {
204+
String paramName = matcher.group(1); // Get parameter name without $
205+
206+
// Check if this parameter exists in the query map
207+
if (query.containsKey(paramName)) {
208+
if (!firstParam) {
209+
queryString.append("&");
210+
}
211+
queryString.append(paramName).append("=").append(query.get(paramName));
212+
firstParam = false;
213+
}
214+
}
215+
216+
// Append query string to the route only if parameters were added
217+
if (queryString.length() > 1) {
218+
// Clean the route by removing $parameter$ placeholders and extra &
219+
String cleanRoute = fullRoute.replaceAll("\\$[^$]+\\$", "").replaceAll("&+", "");
220+
fullRoute = cleanRoute + queryString.toString();
221+
}
222+
}
223+
165224
ConnectLib.StoreAndRetrieve().store.put("currentRoute", fullRoute);
166225
ConnectLib.StoreAndRetrieve().store.put("currentMethod", methodType);
167226

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import fr.sandro642.github.misc.EnumLoader;
1010
import org.junit.jupiter.api.Test;
1111

12+
import java.util.Map;
1213
import java.util.concurrent.CompletableFuture;
1314
import java.util.concurrent.TimeUnit;
1415

@@ -22,7 +23,9 @@
2223
public class MainTest {
2324

2425
public enum TestRoutes implements EnumLoader.RouteImport {
25-
HELLO("/hello");
26+
HELLO("/hello"),
27+
GREET("/greet$name$")
28+
;
2629

2730
final String route;
2831

@@ -66,8 +69,12 @@ public void testUseFullRoute() {
6669
ConnectLib.initialize(ResourceType.TEST_RESOURCES, TestRoutes.class);
6770

6871
try {
72+
Map<String, String> query = Map.of(
73+
"name", "Sandro642"
74+
);
75+
6976
CompletableFuture<ApiFactory> factoryCompletableFuture = ConnectLib.JobGetInfos()
70-
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, TestRoutes.HELLO)
77+
.getRoutes(MethodType.GET, TestRoutes.GREET, null, null, query)
7178
.getResponse();
7279

7380
ApiFactory response = factoryCompletableFuture.get(5, TimeUnit.SECONDS);

0 commit comments

Comments
 (0)