Skip to content

Commit e2f9fd4

Browse files
authored
Merge pull request #49 from Sandro642/feature/multi-urlServer
Add URLProvider interface and update ApiClient to accept custom URLs
2 parents a4c1df3 + ccbf71c commit e2f9fd4

6 files changed

Lines changed: 67 additions & 31 deletions

File tree

README.md

Lines changed: 2 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.3.7-STABLE
20+
Stable Version: 0.3.8-STABLE
2121
```
2222

2323
---
@@ -115,7 +115,7 @@ repositories {
115115

116116
dependencies {
117117

118-
implementation("fr.sandro642.github:ConnectLib:0.3.7-STABLE")
118+
implementation("fr.sandro642.github:ConnectLib:0.3.8-STABLE")
119119

120120
}
121121

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.3.7-STABLE'
8+
version = '0.3.8-STABLE'
99

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public class ApiClient extends ApiFactory {
4949
* It initializes the WebClient with the base URL from the ConnectLib configuration.
5050
* If the base URL is not found, it throws a RuntimeException.
5151
*/
52-
public ApiClient() {
53-
String baseUrl = (String) connectLib.StoreAndRetrieve().store.get(connectLib.StoreAndRetrieve().URL_KEY);
52+
public ApiClient(String baseUrlLambda) {
53+
String baseUrl = baseUrlLambda;
5454

5555
if (baseUrl == null) {
5656
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "construct.urlbase"));

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import fr.sandro642.github.api.ApiClient;
55
import fr.sandro642.github.api.ApiFactory;
66
import fr.sandro642.github.enums.MethodType;
7-
import fr.sandro642.github.enums.VersionType;
87
import fr.sandro642.github.enums.lang.CategoriesType;
98

109
import java.util.Map;
@@ -27,19 +26,24 @@ public class JobGetInfos {
2726
* ApiClient is used to make API calls.
2827
* It is initialized in the constructor of JobGetInfos.
2928
*/
30-
private final ApiClient apiClient;
29+
private ApiClient apiClient;
3130

3231
/**
3332
* connectLib instance to access its methods and properties.
3433
*/
3534
private ConnectLib connectLib = new ConnectLib();
3635

36+
/**
37+
* URLProvider instance to provide custom URL branches.
38+
* If not set, the default URL from the configuration will be used.
39+
*/
40+
private URLProvider urlBranch;
41+
3742
/**
3843
* Constructor of JobGetInfos.
3944
* Initializes the ApiClient and loads the YAML configuration.
4045
*/
4146
public JobGetInfos() {
42-
this.apiClient = new ApiClient();
4347
connectLib.YamlUtils();
4448
}
4549

@@ -245,6 +249,18 @@ public <R> JobGetInfos getRoutes(VersionProvider versionType, MethodType methodT
245249
return this;
246250
}
247251

252+
/**
253+
* Set a custom URL branch for the API calls.
254+
* If not set, the default URL from the configuration will be used.
255+
* @param urlBranch The URLProvider instance providing the custom URL branch.
256+
* @return JobGetInfos for chaining
257+
*/
258+
public JobGetInfos urlBranch(URLProvider urlBranch) {
259+
260+
this.urlBranch = urlBranch;
261+
return this;
262+
}
263+
248264
/**
249265
* Get the response from the API based on the current route and method.
250266
* This method retrieves the stored route, method, and body from the store,
@@ -261,6 +277,16 @@ public CompletableFuture<ApiFactory> getResponse() {
261277
connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.JOBS_PACKAGE, "getresponse.mustbe"));
262278
}
263279

280+
/** Determine the URL branch to use */
281+
String urlBranchLambda;
282+
283+
if (urlBranch == null) {
284+
urlBranchLambda = (String) connectLib.StoreAndRetrieve().store.get(connectLib.StoreAndRetrieve().URL_KEY);
285+
} else {
286+
urlBranchLambda = urlBranch.getURL();
287+
}
288+
apiClient = new ApiClient(urlBranchLambda);
289+
264290
CompletableFuture<ApiFactory> responseFuture = new CompletableFuture<>();
265291

266292
// Callback to clean up the store after the response
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package fr.sandro642.github.jobs;
2+
3+
/**
4+
* URLProvider is an interface that provides a method to get the URL of a class implementing it.
5+
* Classes implementing this interface must define the getURL() method to return their URL as a String
6+
*/
7+
8+
public interface URLProvider {
9+
10+
/**
11+
* Gets the URL of the class implementing this interface.
12+
* @return the URL as a String
13+
*/
14+
String getURL();
15+
}

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

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import fr.sandro642.github.enums.MethodType;
88
import fr.sandro642.github.enums.ResourceType;
99
import fr.sandro642.github.jobs.RouteImport;
10+
import fr.sandro642.github.jobs.URLProvider;
1011
import fr.sandro642.github.jobs.VersionProvider;
1112
import fr.sandro642.github.misc.EnumLoader;
1213
import org.junit.jupiter.api.Test;
@@ -43,6 +44,22 @@ public String getRoute() {
4344
}
4445
}
4546

47+
public enum TestUrl implements URLProvider {
48+
LOCALHOST("http://localhost:8080")
49+
;
50+
51+
private final String url;
52+
53+
TestUrl(String url) {
54+
this.url = url;
55+
}
56+
57+
@Override
58+
public String getURL() {
59+
return url;
60+
}
61+
}
62+
4663
public enum TestCustomVersion implements VersionProvider {
4764
DEV_BRANCH("dev"),
4865
;
@@ -71,7 +88,7 @@ public static void main(String[] args) {
7188
connectLib.Logger().showLogs();
7289

7390
CompletableFuture<ApiFactory> apiFactoryCompletableFuture = connectLib.JobGetInfos()
74-
.getRoutes( MethodType.GET, TestRoutes.HELLO)
91+
.getRoutes(MethodType.GET, TestRoutes.HELLO)
7592
.getResponse();
7693

7794
ApiFactory response = apiFactoryCompletableFuture.get(5, TimeUnit.SECONDS);
@@ -125,28 +142,6 @@ public void testLangType() {
125142
System.out.println("Response: " + response.display());
126143
System.out.println("Status Code: " + response.getStatusCode());
127144

128-
Thread.sleep(5000);
129-
130-
factoryCompletableFuture = connectLib.JobGetInfos()
131-
.getRoutes(MethodType.GET, TestRoutes.GREET, null, null, Map.of("name", "Sandro642"))
132-
.getResponse();
133-
134-
response = factoryCompletableFuture.get(5, TimeUnit.SECONDS);
135-
136-
System.out.println("Response: " + response.display());
137-
System.out.println("Status Code: " + response.getStatusCode());
138-
139-
Thread.sleep(5000);
140-
141-
factoryCompletableFuture = connectLib.JobGetInfos()
142-
.getRoutes(MethodType.GET, TestRoutes.GREET, null, null, Map.of("name", "Sandro642"))
143-
.getResponse();
144-
145-
response = factoryCompletableFuture.get(5, TimeUnit.SECONDS);
146-
147-
System.out.println("Response: " + response.display());
148-
System.out.println("Status Code: " + response.getStatusCode());
149-
150145
} catch (Exception e) {
151146
e.printStackTrace();
152147
}

0 commit comments

Comments
 (0)