Skip to content

Commit 087f2cf

Browse files
committed
Feature: implement serialization for store and retrieve data management
1 parent fac972d commit 087f2cf

10 files changed

Lines changed: 71 additions & 15 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "fr.sandro642.github"
9-
version = "0.0.0" // Version de votre projet
9+
version = "0.1.3.9-SNAPSHOT" // Version de votre projet
1010

1111
// Ajoutez cette tâche à votre build.gradle.kts
1212
tasks.register("printVersion") {

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ Hook -----------------------|
1414
```
1515
---
1616

17+
Changelog :
18+
19+
```java
20+
- [0.1.3.9-SNAPSHOT] : Ajout de la sérialisation des données pour une compatibilisation parfaite des données avec les HOOK.
21+
```
22+
23+
---
24+
1725
## 🌟 Pourquoi ConnectorAPI ?
1826

1927
Imaginez une API qui ne se contente pas de relier des services, mais qui devient le chef d’orchestre de vos intégrations. ConnectorAPI est conçu pour :

src/main/java/fr/sandro642/github/ConnectorAPI.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ public static void initialize(ResourceType resourceType) {
3939

4040
storeAndRetrieve.store.put(storeAndRetrieve.FILE_LOCATION_KEY, resourceType.getPath());
4141

42+
ConnectorAPI.SerialMap().saveData(storeAndRetrieve.store, "store_and_retrieve.yml");
43+
4244
// Charge l'URL depuis le fichier YAML
4345
String baseUrl = yamlUtils.getURL();
4446
if (baseUrl != null) {
4547
storeAndRetrieve.store.put(storeAndRetrieve.URL_KEY, baseUrl);
4648
}
49+
50+
ConnectorAPI.SerialMap().saveData(storeAndRetrieve.store, "store_and_retrieve.yml");
4751
}
4852

4953
/**

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import reactor.core.publisher.Mono;
88
import reactor.core.scheduler.Schedulers;
99

10+
import java.util.HashMap;
1011
import java.util.Map;
1112
import java.util.concurrent.atomic.AtomicReference;
1213

@@ -30,7 +31,9 @@ public class ApiClient {
3031
* Constructeur de ApiClient qui initialise WebClient avec l'URL de base.
3132
*/
3233
public ApiClient() {
33-
String baseUrl = (String) ConnectorAPI.StoreAndRetrieve().get(ConnectorAPI.StoreAndRetrieve().URL_KEY);
34+
HashMap<String, Object> storeLoad = ConnectorAPI.SerialMap().loadData("store_and_retrieve.yml");
35+
36+
String baseUrl = (String) storeLoad.get(ConnectorAPI.StoreAndRetrieve().URL_KEY);
3437

3538
if (baseUrl == null) {
3639
throw new RuntimeException("URL de base non trouvée. Assurez-vous d'avoir initialisé ConnectorAPI. ");

src/main/java/fr/sandro642/github/hook/MCSupport.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.sandro642.github.hook;
22

3+
import fr.sandro642.github.ConnectorAPI;
34
import org.bukkit.plugin.Plugin;
45

56
/**
@@ -36,6 +37,8 @@ private MCSupport() {
3637
*/
3738
public boolean isMCProject() {
3839
try {
40+
ConnectorAPI.SerialMap().loadData("store_and_retrieve.yml");
41+
3942
String fileLocation = (String) fr.sandro642.github.ConnectorAPI.StoreAndRetrieve().store.get(fr.sandro642.github.ConnectorAPI.StoreAndRetrieve().FILE_LOCATION_KEY);
4043
return fileLocation != null && fileLocation.contains(fr.sandro642.github.jobs.misc.ResourceType.MC_RESOURCES.getPath());
4144
} catch (Exception e) {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import fr.sandro642.github.api.ApiClient;
55
import fr.sandro642.github.api.ApiResponse;
66
import fr.sandro642.github.jobs.misc.MethodType;
7-
import fr.sandro642.github.jobs.misc.ResourceType;
87
import fr.sandro642.github.jobs.misc.VersionType;
98
import fr.sandro642.github.utils.YamlUtils;
109

10+
import java.util.HashMap;
1111
import java.util.Map;
1212

1313
/**
@@ -153,6 +153,8 @@ public JobGetInfos getRoutes(VersionType versionType, MethodType methodType, Str
153153
ConnectorAPI.StoreAndRetrieve().store.put("currentParams", params);
154154
}
155155

156+
ConnectorAPI.SerialMap().saveData(ConnectorAPI.StoreAndRetrieve().store, "store_and_retrieve.yml");
157+
156158
ConnectorAPI.Logger().INFO("Route construite: " + fullRoute);
157159

158160
} catch (Exception e) {
@@ -169,9 +171,11 @@ public JobGetInfos getRoutes(VersionType versionType, MethodType methodType, Str
169171
*/
170172
public ApiResponse<Void> getResponse() {
171173
try {
172-
String route = (String) ConnectorAPI.StoreAndRetrieve().store.get("currentRoute");
173-
MethodType method = (MethodType) ConnectorAPI.StoreAndRetrieve().store.get("currentMethod");
174-
Map<String, Object> body = (Map<String, Object>) ConnectorAPI.StoreAndRetrieve().store.get("currentBody");
174+
HashMap<String, Object> storeLoad = ConnectorAPI.SerialMap().loadData("store_and_retrieve.yml");
175+
176+
String route = (String) storeLoad.get("currentRoute");
177+
MethodType method = (MethodType) storeLoad.get("currentMethod");
178+
Map<String, Object> body = (Map<String, Object>) storeLoad.get("currentBody");
175179

176180
if (route == null || method == null) {
177181
throw new RuntimeException("Route ou méthode non définie. Appelez getRoutes() d'abord.");
@@ -200,6 +204,8 @@ public ApiResponse<Void> getResponse() {
200204
ConnectorAPI.StoreAndRetrieve().store.remove("currentMethod");
201205
ConnectorAPI.StoreAndRetrieve().store.remove("currentBody");
202206

207+
ConnectorAPI.SerialMap().saveData(ConnectorAPI.StoreAndRetrieve().store, "store_and_retrieve.yml");
208+
203209
return response;
204210

205211
} catch (Exception e) {

src/main/java/fr/sandro642/github/utils/SerialMap.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void saveData(HashMap<String, Object> map, String fileName) {
4141
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
4242
outputStream.writeObject(map);
4343
outputStream.close();
44-
ConnectorAPI.Logger().INFO("✓ Sauvegarde réussie dans " + fileName);
44+
//ConnectorAPI.Logger().INFO("✓ Sauvegarde réussie dans " + fileName);
4545
} catch (Exception e) {
4646
ConnectorAPI.Logger().ERROR("✗ Erreur sauvegarde " + fileName);
4747
}
@@ -58,10 +58,11 @@ public HashMap<String, Object> loadData(String fileName) {
5858
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName));
5959
HashMap<String, Object> data = (HashMap<String, Object>) inputStream.readObject();
6060
inputStream.close();
61-
System.out.println("✓ Chargement réussi depuis " + fileName);
61+
//ConnectorAPI.Logger().INFO("✓ Chargement réussi depuis " + fileName);
6262
return data;
6363
} catch (Exception e) {
64-
System.out.println("✗ Erreur chargement " + fileName);
64+
ConnectorAPI.Logger().ERROR("✗ Erreur chargement " + fileName);
65+
e.printStackTrace();
6566
return new HashMap<>();
6667
}
6768
}

src/main/java/fr/sandro642/github/utils/StoreAndRetrieve.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.sandro642.github.utils;
22

3+
import java.util.HashMap;
34
import java.util.concurrent.ConcurrentHashMap;
45
import java.util.Map;
56

@@ -22,7 +23,7 @@ public class StoreAndRetrieve {
2223
*/
2324
public final String URL_KEY = "baseUrl";
2425
public final String FILE_LOCATION_KEY = "fileLocation";
25-
public final Map<String, Object> store = new ConcurrentHashMap<>();
26+
public final HashMap<String, Object> store = new HashMap<>();
2627

2728
/**
2829
* Ajoute une valeur à la map avec la clé spécifiée.

src/main/java/fr/sandro642/github/utils/YamlUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.*;
99
import java.nio.file.Files;
1010
import java.nio.file.Paths;
11+
import java.util.HashMap;
1112
import java.util.Map;
1213

1314
/**
@@ -27,7 +28,9 @@ public class YamlUtils {
2728
* @return l'URL de base définie dans le fichier YAML, ou null si une erreur se produit
2829
*/
2930
public String getURL() {
30-
String yamlFilePath = ConnectorAPI.StoreAndRetrieve().store.get(ConnectorAPI.StoreAndRetrieve().FILE_LOCATION_KEY) + "/infos.yml";
31+
HashMap<String, Object> storeLoad = ConnectorAPI.SerialMap().loadData("store_and_retrieve.yml");
32+
33+
String yamlFilePath = storeLoad.get(ConnectorAPI.StoreAndRetrieve().FILE_LOCATION_KEY) + "/infos.yml";
3134

3235
try (InputStream inputStream = Files.newInputStream(Paths.get(yamlFilePath))) {
3336
Yaml yaml = new Yaml();
@@ -44,7 +47,9 @@ public String getURL() {
4447
* @return
4548
*/
4649
public String getRoute(String routeName) {
47-
String yamlFilePath = ConnectorAPI.StoreAndRetrieve().store.get(ConnectorAPI.StoreAndRetrieve().FILE_LOCATION_KEY) + "/infos.yml";
50+
HashMap<String, Object> storeLoad = ConnectorAPI.SerialMap().loadData("store_and_retrieve.yml");
51+
52+
String yamlFilePath = storeLoad.get(ConnectorAPI.StoreAndRetrieve().FILE_LOCATION_KEY) + "/infos.yml";
4853

4954
try (InputStream inputStream = Files.newInputStream(Paths.get(yamlFilePath))) {
5055
Yaml yaml = new Yaml();

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import fr.sandro642.github.jobs.misc.VersionType;
99
import org.junit.jupiter.api.Test;
1010

11+
import java.util.HashMap;
12+
1113
/**
1214
* Main est une classe de test pour vérifier la création du fichier YML et pleins d'autres fonctionnalités de la librairie.
1315
* @author Sandro642
@@ -22,9 +24,6 @@ public void getUrl() {
2224
ConnectorAPI.initialize(ResourceType.TEST_RESOURCES);
2325

2426
System.out.println(ConnectorAPI.YamlUtils().getURL());
25-
26-
27-
2827
}
2928

3029
@Test
@@ -69,4 +68,30 @@ public static void main(String[] args) {
6968
}
7069
}
7170

71+
@Test
72+
public void saveDataMap() {
73+
HashMap<String, Object> data = new HashMap<>();
74+
75+
data.put("version", "1.0.0");
76+
data.put("status", "ok");
77+
78+
ConnectorAPI.SerialMap().saveData(data, "test_map.yml");
79+
}
80+
81+
@Test
82+
public void loadDataMap() {
83+
HashMap<String, Object> data = ConnectorAPI.SerialMap().loadData("test_map.yml");
84+
85+
System.out.println("data version: " + data.get("version"));
86+
}
87+
88+
@Test
89+
public void resetDataMap() {
90+
HashMap<String, Object> data = ConnectorAPI.SerialMap().loadData("test_map.yml");
91+
92+
data.clear();
93+
94+
ConnectorAPI.SerialMap().saveData(data, "test_map.yml");
95+
}
96+
7297
}

0 commit comments

Comments
 (0)