Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'fr.sandro642.github'
version = '0.2.6.3-STABLE'
version = '0.2.6.4-STABLE'

tasks.register('printVersion') {
doLast {
Expand Down
7 changes: 4 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ And if you thought APIs were complicated, think again! With ConnectLib, it's lik
---

```java
Stable Version: 0.2.6.3-STABLE
Stable Version: 0.2.6.4-STABLE
```

---
Expand Down Expand Up @@ -47,6 +47,7 @@ Changelog:
- [0.2.0-STABLE]: Wow, arrival of 0.2.0 in such a short time? There were things to do on this project ;)
- [0.2.2-STABLE]: Added log creation.
- [0.2.6.1-STABLE]: Patch dû à la compatibilité avec la création de log et le Hook Minecraft.
- [0.2.6.4-STABLE]: Added asynchronous job execution, allowing you to run tasks in the background without blocking your main application thread.
```

---
Expand Down Expand Up @@ -104,7 +105,7 @@ repositories {

dependencies {

implementation("fr.sandro642.github:ConnectLib:0.2.6.3-STABLE")
implementation("fr.sandro642.github:ConnectLib:0.2.6.4-STABLE")

}

Expand Down Expand Up @@ -140,7 +141,7 @@ public class Example {
}
```

More examples HERE: [ExampleUsage.java](src/main/java/fr/sandro642/github/example/ExampleUsage.java) Not available at the moment due to new features.
More examples HERE: [ExampleUsages.java](src/main/java/fr/sandro642/github/example/ExampleUsages.java)

---

Expand Down
110 changes: 110 additions & 0 deletions src/main/java/fr/sandro642/github/example/ExampleUsages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package fr.sandro642.github.example;

import fr.sandro642.github.ConnectLib;
import fr.sandro642.github.api.ApiFactory;
import fr.sandro642.github.enums.MethodType;
import fr.sandro642.github.enums.ResourceType;
import fr.sandro642.github.enums.VersionType;
import fr.sandro642.github.jobs.JobGetInfos;
import fr.sandro642.github.utils.ConvertEnum;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
* ExampleUsages is a placeholder class that can be used to demonstrate how to use the ConnectLib library.
* It can contain example methods or code snippets that show how to interact with the API, handle responses,
* and utilize the features provided by the ConnectLib library.
*
* @author Sandro642
* @version 1.0
*/

public class ExampleUsages {

public enum ExampleRoutes implements ConvertEnum.RouteImport {
EXAMPLE_ROUTE("/api/example/route");

final String route;

ExampleRoutes(String route) {
this.route = route;
}

@Override
public String route() {
return route;
}
}

public void initializeLib() {

// Optionally, you can specify routes if needed
ConnectLib.initialize(ResourceType.MAIN_RESOURCES, ExampleRoutes.class);
// You can also initialize without specifying routes
ConnectLib.initialize(ResourceType.MAIN_RESOURCES);
}

// Add methods here to demonstrate how to use the ConnectLib library
// For example, you can create methods to make API calls, handle responses, etc.

// Example method to demonstrate usage
public void exampleMethodSync() {
// This method can be used to demonstrate how to interact with the API
// For example, making a GET request to the EXAMPLE_ROUTE
ApiFactory response = ConnectLib.JobGetInfos()
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE)
.getResponse()
.block();

System.out.println(response.display());
System.out.println("Response Code: " + response.getData("code"));
System.out.println("Response Message: " + response.getData("message"));
System.out.println("Response Data: " + response.getSpecData("data", "exampleKey"));
}

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

// Create a CompletableFuture to handle the asynchronous response
CompletableFuture<ApiFactory> futureResponse = new CompletableFuture<>();

ConnectLib.JobGetInfos()
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE)
.getResponse()
.subscribe(
futureResponse::complete,
futureResponse::completeExceptionally
);

// Handle the response when it completes
ApiFactory response = futureResponse.get(10, TimeUnit.SECONDS);

System.out.println(response.display());
System.out.println("Response Code: " + response.getData("code"));
System.out.println("Response Message: " + response.getData("message"));
System.out.println("Response Data: " + response.getSpecData("data", "exampleKey"));
} catch (java.util.concurrent.TimeoutException e) {
System.err.println("The operation timed out: " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}

// Example to use all methods in JobGetInfos
public void exampleJobGetInfos() {
Map<String, ?> body = Map.of();
Map<String,?> params = Map.of();

ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE, body, params);
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.POST, ExampleRoutes.EXAMPLE_ROUTE, body);
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.PUT, ExampleRoutes.EXAMPLE_ROUTE, null, params);
ConnectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.PATCH, ExampleRoutes.EXAMPLE_ROUTE);
ConnectLib.JobGetInfos().getRoutes(MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE, body, params);
ConnectLib.JobGetInfos().getRoutes(MethodType.POST, ExampleRoutes.EXAMPLE_ROUTE, body);
ConnectLib.JobGetInfos().getRoutes(MethodType.DELETE, ExampleRoutes.EXAMPLE_ROUTE);
}

}
49 changes: 8 additions & 41 deletions src/main/java/fr/sandro642/github/jobs/JobGetInfos.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fr.sandro642.github.api.ApiFactory;
import fr.sandro642.github.enums.MethodType;
import fr.sandro642.github.enums.VersionType;
import reactor.core.publisher.Mono;

import java.util.Map;

Expand Down Expand Up @@ -108,40 +109,6 @@ public JobGetInfos getRoutes(MethodType methodType, String routeName) {
return getRoutes(null, methodType, routeName, 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
* @param body Body of the request for POST (can be null for GET)
* @return JobGetInfos for chaining
*/
public JobGetInfos getRoutesWithBody(MethodType methodType, String routeName, Map<String, ?> body) {
return getRoutes(null, methodType, routeName, body, 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 getRoutesWithParams(MethodType methodType, String routeName, Map<String, ?> params) {
return getRoutes(null, methodType, routeName, null, params);
}

/**
* Get routes from the YAML file and builds the full URL with a request body and 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
* @return JobGetInfos for chaining
*/
public JobGetInfos getRoutesBoth(MethodType methodType, String routeName, Map<String, ?> body, Map<String, ?> params) {
return getRoutes(null, methodType, routeName, body, params);
}

/**
* 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)
Expand Down Expand Up @@ -220,7 +187,7 @@ public <R> JobGetInfos getRoutes(VersionType versionType, MethodType methodType,
* 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 ApiFactory getResponse() {
public Mono<ApiFactory> getResponse() {
try {

String route = (String) ConnectLib.StoreAndRetrieve().store.get("currentRoute");
Expand All @@ -231,23 +198,23 @@ public ApiFactory getResponse() {
throw new RuntimeException("Route or method not set. Please call getRoutes() first.");
}

ApiFactory response;
Mono<ApiFactory> response;

switch(method) {
case GET:
response = apiClient.callAPIGet(route).block();
response = apiClient.callAPIGet(route);
break;
case POST:
response = apiClient.callAPIPost(route, body).block();
response = apiClient.callAPIPost(route, body);
break;
case PUT:
response = apiClient.callAPIPut(route, body).block();
response = apiClient.callAPIPut(route, body);
break;
case PATCH:
response = apiClient.callAPIPatch(route, body).block();
response = apiClient.callAPIPatch(route, body);
break;
case DELETE:
response = apiClient.callAPIDelete(route).block();
response = apiClient.callAPIDelete(route);
break;
default:
ConnectLib.Logger().ERROR("Unsupported method type: " + method);
Expand Down
25 changes: 20 additions & 5 deletions src/test/java/fr/sandro642/github/test/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import fr.sandro642.github.utils.ConvertEnum;
import org.junit.jupiter.api.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
* MainTest is a test class for the ConnectLib library.
* @author Sandro642
Expand Down Expand Up @@ -45,12 +48,21 @@ public static void main(String[] args) {
ConnectLib.initialize(ResourceType.TEST_RESOURCES, TestRoutes.class);

try {
CompletableFuture<ApiFactory> futureResponse = new CompletableFuture<>();

ApiFactory response = ConnectLib.JobGetInfos()
ConnectLib.JobGetInfos()
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, TestRoutes.VERSION)
.getResponse();
.getResponse()
.subscribe(
futureResponse::complete,
Comment thread
Sandro642 marked this conversation as resolved.
futureResponse::completeExceptionally
);

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

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


System.out.println(response.getSpecData("data", "version"));
} catch (Exception e) {
return;
}
Expand All @@ -62,8 +74,11 @@ public void testUseFullRoute() {

try {
ApiFactory response = ConnectLib.JobGetInfos()
.getRoutes(VersionType.V1_BRANCH, MethodType.POST, TestRoutes.INFO, null, null)
.getResponse();
.getRoutes(VersionType.V1_BRANCH, MethodType.GET, TestRoutes.VERSION)
.getResponse()
.block();

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

} catch (Exception e) {
e.printStackTrace();
Expand Down