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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Changelog:
- [0.2.6.4-STABLE]: Added asynchronous job execution, allowing you to run tasks in the background without blocking your main application thread.
- [0.2.7.2-STABLE]: Remove implementation Project Reactor
- [0.2.9-STABLE]: Added support query variables in routes, allowing you to pass parameters directly in the URL.
- [0.4.0-STABLE]: Web implementation, now you can have a dashboard to see your connectors and jobs.
```

---
Expand Down
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.3.9.3-STABLE'
version = '0.4.0-STABLE'

// Générer une classe de version automatiquement
task generateVersionClass {
Expand Down
64 changes: 62 additions & 2 deletions src/main/java/fr/sandro642/github/ConnectLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import fr.sandro642.github.misc.*;
import fr.sandro642.github.jobs.JobGetInfos;
import fr.sandro642.github.enums.ResourceType;
import fr.sandro642.github.spring.Application;
import fr.sandro642.github.spring.controller.DataController;
import fr.sandro642.github.update.RetrieveLastVersion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -38,18 +42,25 @@ public class ConnectLib {
private static LangManager langManager;
private static RetrieveLastVersion rlv;

private static boolean isInitialized = false;

private static List<String> enumNames = new ArrayList<>();
private static List<String> enumUrls = new ArrayList<>();
private static Map<Enum<?>, String> routesEnums = new HashMap<>();

/**
* Init the ConnectLib with the specified resource type and routes.
* @param resourceType the type of resource to initialize
* @param routes the routes to be used in the ConnectLib
*/
public void Init(ResourceType resourceType, LangType langType, Class<? extends Enum<?>>... routes) {
public ConnectLib Init(ResourceType resourceType, LangType langType, Class<? extends Enum<?>>... routes) {
try {
logger = new Logger();
storeAndRetrieve = new StoreAndRetrieve();
yamlUtils = new YamlUtils();
logs = new Logs();
rlv = new RetrieveLastVersion();
isInitialized = true;

rlv.isLatestVersion();

Expand All @@ -59,6 +70,21 @@ public void Init(ResourceType resourceType, LangType langType, Class<? extends E

langManager = new LangManager();

for (Class<? extends Enum<?>> route : routes) {
if (route == null) {
Logger().ERROR(langManager.getMessage(CategoriesType.CONNECTLIB_CLASS, "initialise.routeclass"));
continue;
}

Map<Enum<?>, String> converted = EnumLoader.convertRouteImport(route);
if (converted != null && !converted.isEmpty()) {
routesEnums.putAll(converted);
for (Map.Entry<Enum<?>, String> entry : converted.entrySet()) {
enumNames.add(entry.getKey().name());
enumUrls.add(entry.getValue());
}
}
}


Map<Enum<?>, String> routesEnums = new HashMap<>();
Expand All @@ -85,6 +111,32 @@ public void Init(ResourceType resourceType, LangType langType, Class<? extends E
} catch (Exception e) {
Logger().ERROR(langManager.getMessage(CategoriesType.CONNECTLIB_CLASS, "initialise.catcherror", Map.of("exception", e.getMessage())));
}
return this;
}

/**
* Get the routes map.
* @return a map of route names to their corresponding paths
*/
public Map<String, String> getRoutesMap() {
return new HashMap<>(routes);
}

/**
* Check if the ConnectLib supports web services.
* This method starts the Spring application.
*/
public ConnectLib webServices() {
SpringApp().startApplication().subscribe();
return this;
}

/**
* Check if the ConnectLib is initialized.
* @return true if initialized, false otherwise
*/
public boolean isInitialized() {
return isInitialized;
}

/**
Expand Down Expand Up @@ -160,7 +212,7 @@ public HookManager HookManager() {

/**
* Return the instance of LangSupport.
* @return LangSupport instance
* @return LangSupport instance²
*/
public LangSupport LangSupport() {
return LangSupport.getInstance();
Expand All @@ -174,4 +226,12 @@ public LangSupport LangSupport() {
public LangManager LangManager() {
return langManager;
}

/**
* Return the instance of Application.
* @return Application instance
*/
public Application SpringApp() {
return Application.getInstance();
}
}
55 changes: 54 additions & 1 deletion src/main/java/fr/sandro642/github/api/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import fr.sandro642.github.ConnectLib;
import fr.sandro642.github.enums.lang.CategoriesType;
import fr.sandro642.github.spring.controller.DataController;
import fr.sandro642.github.spring.dto.Request;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
Expand Down Expand Up @@ -44,13 +46,19 @@ public class ApiClient extends ApiFactory {
*/
private final ApiFactory apiFactory = new ApiFactory();

/**
* baseUrl is a static string that holds the base URL for the API.
* It is initialized in the constructor and used for making requests.
*/
private static String baseUrl;

/**
* Constructor for ApiClient.
* It initializes the WebClient with the base URL from the ConnectLib configuration.
* If the base URL is not found, it throws a RuntimeException.
*/
public ApiClient(String baseUrlLambda) {
String baseUrl = baseUrlLambda;
baseUrl = baseUrlLambda;

if (baseUrl == null) {
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "construct.urlbase"));
Expand All @@ -69,6 +77,8 @@ public ApiClient(String baseUrlLambda) {
public Mono<ApiFactory> callAPIGet(String routeName) {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.get", Map.of("routename", routeName)));

Request r = DataController.getInstance().createRequest(routeName, baseUrl);

record ResponseData(int statusCode, String body) {}

return webClient.get()
Expand All @@ -80,6 +90,13 @@ record ResponseData(int statusCode, String body) {}
.subscribeOn(Schedulers.boundedElastic())
.doOnNext(responseData -> {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));

String newStatus = (responseData.statusCode() >= 200 && responseData.statusCode() < 300) ? "success" : "error";
try {
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
} catch (Exception e) {
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "GET", "exception", e.getMessage())));
}
})
.map(responseData -> {
apiFactory.setStatusCode(responseData.statusCode());
Expand All @@ -99,6 +116,8 @@ record ResponseData(int statusCode, String body) {}
public Mono<ApiFactory> callAPIPost(String routeName, Map<String, Object> body) {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.post", Map.of("routename", routeName)));

Request r = DataController.getInstance().createRequest(routeName, baseUrl);

record ResponseData(int statusCode, String body) {}

return webClient.post()
Expand All @@ -111,6 +130,13 @@ record ResponseData(int statusCode, String body) {}
.subscribeOn(Schedulers.boundedElastic())
.doOnNext(responseData -> {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));

String newStatus = (responseData.statusCode() >= 200 && responseData.statusCode() < 300) ? "success" : "error";
try {
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
} catch (Exception e) {
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "GET", "exception", e.getMessage())));
}
})
.map(responseData -> {
apiFactory.setStatusCode(responseData.statusCode());
Expand All @@ -130,6 +156,8 @@ record ResponseData(int statusCode, String body) {}
public Mono<ApiFactory> callAPIPut(String routeName, Map<String, Object> body) {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.put", Map.of("routename", routeName)));

Request r = DataController.getInstance().createRequest(routeName, baseUrl);

record ResponseData(int statusCode, String body) {}

return webClient.put()
Expand All @@ -142,6 +170,13 @@ record ResponseData(int statusCode, String body) {}
.subscribeOn(Schedulers.boundedElastic())
.doOnNext(responseData -> {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));

String newStatus = (responseData.statusCode() >= 200 && responseData.statusCode() < 300) ? "success" : "error";
try {
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
} catch (Exception e) {
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "GET", "exception", e.getMessage())));
}
})
.map(responseData -> {
apiFactory.setStatusCode(responseData.statusCode());
Expand All @@ -161,6 +196,8 @@ record ResponseData(int statusCode, String body) {}
public Mono<ApiFactory> callAPIPatch(String routeName, Map<String, Object> body) {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.patch", Map.of("routename", routeName)));

Request r = DataController.getInstance().createRequest(routeName, baseUrl);

record ResponseData(int statusCode, String body) {}

return webClient.patch()
Expand All @@ -173,6 +210,13 @@ record ResponseData(int statusCode, String body) {}
.subscribeOn(Schedulers.boundedElastic())
.doOnNext(responseData -> {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));

String newStatus = (responseData.statusCode() >= 200 && responseData.statusCode() < 300) ? "success" : "error";
try {
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
} catch (Exception e) {
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "GET", "exception", e.getMessage())));
}
})
.map(responseData -> {
apiFactory.setStatusCode(responseData.statusCode());
Expand All @@ -191,6 +235,8 @@ record ResponseData(int statusCode, String body) {}
public Mono<ApiFactory> callAPIDelete(String routeName) {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.delete", Map.of("routename", routeName)));

Request r = DataController.getInstance().createRequest(routeName, baseUrl);

record ResponseData(int statusCode, String body) {}

return webClient.delete()
Expand All @@ -202,6 +248,13 @@ record ResponseData(int statusCode, String body) {}
.subscribeOn(Schedulers.boundedElastic())
.doOnNext(responseData -> {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));

String newStatus = (responseData.statusCode() >= 200 && responseData.statusCode() < 300) ? "success" : "error";
try {
DataController.getInstance().updateRequestStatus(r.getId(), newStatus);
} catch (Exception e) {
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "GET", "exception", e.getMessage())));
}
})
.map(responseData -> {
apiFactory.setStatusCode(responseData.statusCode());
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/fr/sandro642/github/hook/HookManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public ResourceType initHook(ResourceType resourceType) {
return this.resourceType;
}

/**
* Returns the current resource type.
*
* @return the current resource type
*/
public ResourceType getResourceType() {
return resourceType;
}

/**
* Sets the file location key based on the resource type.
* This method updates the store with the file location key based on the resource type.
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/fr/sandro642/github/spring/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fr.sandro642.github.spring;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.util.HashMap;
import java.util.Map;

/**
* Main application class for starting the Spring Boot application.
*/

@SpringBootApplication
public class Application {

// Singleton instance
private static Application instance;

/**
* Starts the Spring Boot application in a non-blocking manner.
* @return a Mono that completes when the application has started
*/
public Mono<Void> startApplication() {
return Mono.fromRunnable(() -> {
SpringApplication app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF); // supprime le banner Spring
app.setLogStartupInfo(false); // désactive l'info de démarrage
Map<String, Object> props = new HashMap<>();
props.put("logging.level.root", "OFF"); // coupe l'affichage des logs
app.setDefaultProperties(props);
app.run();
}).subscribeOn(Schedulers.boundedElastic()).then();
}

/**
* Gets the singleton instance of the Application class.
* @return the singleton Application instance
*/
public static Application getInstance() {
if (instance == null) {
instance = new Application();
}
return instance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.sandro642.github.spring.controller;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
* Configuration class for controller beans.
*/

@Configuration
public class ControllerConfig {

/**
* Defines the primary DataController bean.
* @return the singleton instance of DataController
*/
@Bean
@Primary
public DataController dataController() {
return DataController.getInstance();
}
}
Loading