Skip to content

Commit 8eca367

Browse files
committed
Refactor ApiClient and ApiFactory: change ConnectLib to non-final and improve code readability
1 parent 875a73c commit 8eca367

23 files changed

Lines changed: 259 additions & 315 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Imagine an API that doesn't just connect services, but becomes the conductor of
100100
More examples HERE: [ExampleUsages.java](src/main/java/fr/sandro642/github/example/ExampleUsages.java)
101101

102102
---
103-
103+
[build.gradle](build.gradle)
104104
## 📚 Project Structure
105105

106106
- `src/main/java/fr/sandro642/github/` : main source code

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.8.1-DEV_BUILD'
8+
version = '0.3.8.2-STABLE'
99

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

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

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

33
import fr.sandro642.github.enums.LangType;
4-
import fr.sandro642.github.enums.ResourceType;
54
import fr.sandro642.github.enums.lang.CategoriesType;
65
import fr.sandro642.github.hook.HookManager;
76
import fr.sandro642.github.hook.LangSupport;
87
import fr.sandro642.github.hook.MCSupport;
9-
import fr.sandro642.github.jobs.JobGetInfos;
108
import fr.sandro642.github.misc.*;
9+
import fr.sandro642.github.jobs.JobGetInfos;
10+
import fr.sandro642.github.enums.ResourceType;
1111
import fr.sandro642.github.update.RetrieveLastVersion;
1212

1313
import java.util.HashMap;
@@ -16,7 +16,6 @@
1616
/**
1717
* ConnectLib is the main class of the ConnectorAPI library for Standard API.
1818
* It provides methods to initialize the library with resource types, routes, and schemas,
19-
*
2019
* @author Sandro642
2120
* @version 1.0
2221
* @see ConnectLib#JobGetInfos()
@@ -34,16 +33,15 @@ public class ConnectLib {
3433
private static Logger logger = new Logger();
3534
private static StoreAndRetrieve storeAndRetrieve = new StoreAndRetrieve();
3635
private static YamlUtils yamlUtils = new YamlUtils();
37-
private static final Map<String, String> routes = new HashMap<>();
36+
private static final Map<String,String> routes = new HashMap<>();
3837
private static Logs logs = new Logs();
3938
private static LangManager langManager;
4039
private static RetrieveLastVersion rlv;
4140

4241
/**
4342
* Init the ConnectLib with the specified resource type and routes.
44-
*
4543
* @param resourceType the type of resource to initialize
46-
* @param routes the routes to be used in the ConnectLib
44+
* @param routes the routes to be used in the ConnectLib
4745
*/
4846
public void Init(ResourceType resourceType, LangType langType, Class<? extends Enum<?>>... routes) {
4947
try {
@@ -61,6 +59,8 @@ public void Init(ResourceType resourceType, LangType langType, Class<? extends E
6159

6260
langManager = new LangManager();
6361

62+
63+
6464
Map<Enum<?>, String> routesEnums = new HashMap<>();
6565
for (Class<? extends Enum<?>> route : routes) {
6666
if (route == null) {
@@ -89,7 +89,6 @@ public void Init(ResourceType resourceType, LangType langType, Class<? extends E
8989

9090
/**
9191
* Return the route associated with the given route name.
92-
*
9392
* @param routeName the name of the route to retrieve
9493
* @return the route as a String
9594
*/
@@ -104,7 +103,6 @@ public String getRoute(String routeName) {
104103

105104
/**
106105
* Return an instance of JobGetInfos.
107-
*
108106
* @return JobGetInfos instance
109107
*/
110108
public JobGetInfos JobGetInfos() {
@@ -113,7 +111,6 @@ public JobGetInfos JobGetInfos() {
113111

114112
/**
115113
* Return the instance of Logger.
116-
*
117114
* @return Logger instance
118115
*/
119116
public Logger Logger() {
@@ -125,7 +122,6 @@ public Logger Logger() {
125122

126123
/**
127124
* Return the instance of StoreAndRetrieve.
128-
*
129125
* @return StoreAndRetrieve instance
130126
*/
131127
public StoreAndRetrieve StoreAndRetrieve() {
@@ -137,7 +133,6 @@ public StoreAndRetrieve StoreAndRetrieve() {
137133

138134
/**
139135
* Return the instance of YamlUtils.
140-
*
141136
* @return YamlUtils instance
142137
*/
143138
public YamlUtils YamlUtils() {
@@ -149,7 +144,6 @@ public YamlUtils YamlUtils() {
149144

150145
/**
151146
* Return the instance of Logs.
152-
*
153147
* @return Logs instance
154148
*/
155149
public MCSupport MCSupport() {
@@ -158,7 +152,6 @@ public MCSupport MCSupport() {
158152

159153
/**
160154
* Return the instance of HookManager.
161-
*
162155
* @return HookManager instance
163156
*/
164157
public HookManager HookManager() {
@@ -167,7 +160,6 @@ public HookManager HookManager() {
167160

168161
/**
169162
* Return the instance of LangSupport.
170-
*
171163
* @return LangSupport instance
172164
*/
173165
public LangSupport LangSupport() {
@@ -177,7 +169,6 @@ public LangSupport LangSupport() {
177169

178170
/**
179171
* Return the instance of LangManager.
180-
*
181172
* @return LangManager instance
182173
*/
183174
public LangManager LangManager() {

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ApiClient extends ApiFactory {
2424
/**
2525
* connectLib is an instance of ConnectLib that provides access to the library's configuration and utilities.
2626
*/
27-
private final ConnectLib connectLib = new ConnectLib();
27+
private ConnectLib connectLib = new ConnectLib();
2828

2929
/**
3030
* WebClient is a non-blocking, reactive HTTP client for making requests to the API.
@@ -63,15 +63,13 @@ public ApiClient(String baseUrlLambda) {
6363

6464
/**
6565
* Method to call the API with a GET request.
66-
*
6766
* @param routeName Name of the route to call.
6867
* @return a Mono that emits the ApiFactory response containing the parsed JSON data.
6968
*/
7069
public Mono<ApiFactory> callAPIGet(String routeName) {
7170
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.get", Map.of("routename", routeName)));
7271

73-
record ResponseData(int statusCode, String body) {
74-
}
72+
record ResponseData(int statusCode, String body) {}
7573

7674
return webClient.get()
7775
.uri(routeName)
@@ -94,16 +92,14 @@ record ResponseData(int statusCode, String body) {
9492

9593
/**
9694
* Method to call the API with a POST request.
97-
*
9895
* @param routeName Name of the route to call.
99-
* @param body Body of the request (can be null for a request without body).
96+
* @param body Body of the request (can be null for a request without body).
10097
* @return a Mono that emits the ApiFactory response containing the parsed JSON data.
10198
*/
10299
public Mono<ApiFactory> callAPIPost(String routeName, Map<String, Object> body) {
103100
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.post", Map.of("routename", routeName)));
104101

105-
record ResponseData(int statusCode, String body) {
106-
}
102+
record ResponseData(int statusCode, String body) {}
107103

108104
return webClient.post()
109105
.uri(routeName)
@@ -127,16 +123,14 @@ record ResponseData(int statusCode, String body) {
127123

128124
/**
129125
* Method to call the API with a PUT request.
130-
*
131126
* @param routeName Name of the route to call.
132-
* @param body Body of the request (can be null for a request without body).
127+
* @param body Body of the request (can be null for a request without body).
133128
* @return a Mono that emits the ApiFactory response containing the parsed JSON data.
134129
*/
135130
public Mono<ApiFactory> callAPIPut(String routeName, Map<String, Object> body) {
136131
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.put", Map.of("routename", routeName)));
137132

138-
record ResponseData(int statusCode, String body) {
139-
}
133+
record ResponseData(int statusCode, String body) {}
140134

141135
return webClient.put()
142136
.uri(routeName)
@@ -160,16 +154,14 @@ record ResponseData(int statusCode, String body) {
160154

161155
/**
162156
* Method to call the API with a PATCH request.
163-
*
164157
* @param routeName Name of the route to call.
165-
* @param body Body of the request (can be null for a request without body).
158+
* @param body Body of the request (can be null for a request without body).
166159
* @return a Mono that emits the ApiFactory response containing the parsed JSON data.
167160
*/
168161
public Mono<ApiFactory> callAPIPatch(String routeName, Map<String, Object> body) {
169162
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.patch", Map.of("routename", routeName)));
170163

171-
record ResponseData(int statusCode, String body) {
172-
}
164+
record ResponseData(int statusCode, String body) {}
173165

174166
return webClient.patch()
175167
.uri(routeName)
@@ -193,15 +185,13 @@ record ResponseData(int statusCode, String body) {
193185

194186
/**
195187
* Method to call the API with a DELETE request.
196-
*
197188
* @param routeName Name of the route to call.
198189
* @return a Mono that emits the ApiFactory response containing the parsed JSON data.
199190
*/
200191
public Mono<ApiFactory> callAPIDelete(String routeName) {
201192
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.delete", Map.of("routename", routeName)));
202193

203-
record ResponseData(int statusCode, String body) {
204-
}
194+
record ResponseData(int statusCode, String body) {}
205195

206196
return webClient.delete()
207197
.uri(routeName)

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ApiFactory {
2121
/**
2222
* connectLib is an instance of ConnectLib that provides access to the library's configuration and utilities.
2323
*/
24-
private final ConnectLib connectLib = new ConnectLib();
24+
private ConnectLib connectLib = new ConnectLib();
2525

2626
/**
2727
* rawData is a Map that holds the parsed JSON data.
@@ -49,8 +49,7 @@ protected void parseFromRawJson(String rawJson) {
4949
this.rawJson = rawJson;
5050
try {
5151
ObjectMapper mapper = new ObjectMapper();
52-
this.rawData = mapper.readValue(rawJson, new TypeReference<Map<String, Object>>() {
53-
});
52+
this.rawData = mapper.readValue(rawJson, new TypeReference<Map<String, Object>>() {});
5453
} catch (Exception e) {
5554
connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.APIFACTORY_CLASS, "parsefromrawjson.error", Map.of("json", rawJson, "exception", e.getMessage())));
5655
}
@@ -100,7 +99,8 @@ public <O, K> Object getSpecData(O type, K value) {
10099
}
101100

102101
Object nested = rawData.get(type.toString().toLowerCase());
103-
if (nested instanceof Map<?, ?> nestedMap) {
102+
if (nested instanceof Map) {
103+
Map<?, ?> nestedMap = (Map<?, ?>) nested;
104104
return nestedMap.get(value.toString().toLowerCase());
105105
}
106106
} catch (Exception e) {
@@ -136,11 +136,11 @@ public int getStatusCode() {
136136
*/
137137
public Object display() {
138138
try {
139-
if (rawData == null) {
140-
connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.APIFACTORY_CLASS, "general.mustbe"));
141-
return null;
142-
}
143-
return rawData;
139+
if (rawData == null) {
140+
connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.APIFACTORY_CLASS, "general.mustbe"));
141+
return null;
142+
}
143+
return rawData;
144144
} catch (Exception e) {
145145
connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.APIFACTORY_CLASS, "display.error", "exception", e.getMessage()));
146146
}

src/main/java/fr/sandro642/github/enums/LangType.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public enum LangType {
2323

2424
/**
2525
* Constructor to initialize the language code.
26-
*
2726
* @param lang
2827
*/
2928
LangType(String lang) {
@@ -32,7 +31,6 @@ public enum LangType {
3231

3332
/**
3433
* Get the language code.
35-
*
3634
* @return the language code string
3735
*/
3836
public String getLang() {

src/main/java/fr/sandro642/github/enums/lang/CategoriesType.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public enum CategoriesType {
3030

3131
/**
3232
* Constructor to initialize the category.
33-
*
3433
* @param category
3534
*/
3635
CategoriesType(String category) {
@@ -39,7 +38,6 @@ public enum CategoriesType {
3938

4039
/**
4140
* Get the category.
42-
*
4341
* @return the category string
4442
*/
4543
public String getCategory() {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import fr.sandro642.github.enums.ResourceType;
88
import fr.sandro642.github.enums.VersionType;
99
import fr.sandro642.github.jobs.RouteImport;
10+
import fr.sandro642.github.misc.EnumLoader;
1011

1112
import java.util.Map;
1213
import java.util.concurrent.CompletableFuture;
@@ -23,7 +24,7 @@
2324

2425
public class ExampleUsages {
2526

26-
private final ConnectLib connectLib = new ConnectLib();
27+
private ConnectLib connectLib = new ConnectLib();
2728

2829
public enum ExampleRoutes implements RouteImport {
2930
EXAMPLE_ROUTE("/api/example/route");
@@ -92,7 +93,7 @@ public void exampleMethodAsync() {
9293
// Example to use all methods in JobGetInfos
9394
public void exampleJobGetInfos() {
9495
Map<String, ?> body = Map.of();
95-
Map<String, ?> params = Map.of();
96+
Map<String,?> params = Map.of();
9697
Map<String, ?> query = Map.of();
9798

9899
connectLib.JobGetInfos().getRoutes(VersionType.V1_BRANCH, MethodType.GET, ExampleRoutes.EXAMPLE_ROUTE, body, params, query);

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import fr.sandro642.github.ConnectLib;
44
import fr.sandro642.github.enums.ResourceType;
55
import fr.sandro642.github.enums.lang.CategoriesType;
6+
import fr.sandro642.github.misc.Logger;
67

78
/**
89
* HookManager is a class that manages hooks for different resource types.
910
* It allows for the initialization and management of hooks based on the specified resource type.
10-
*
11-
* @author Sandro642
12-
* @version 1.0
1311
* @see HookManager#BASE_PATH()
1412
* @see HookManager#FILE_LOCATION_KEY()
13+
* @author Sandro642
14+
* @version 1.0
1515
*/
1616

1717
public class HookManager {
@@ -26,7 +26,7 @@ public class HookManager {
2626
* connectLib is an instance of ConnectLib that provides access to the library's configuration and utilities.
2727
* It is used throughout the HookManager class to log messages and access other functionalities.
2828
*/
29-
private final ConnectLib connectLib = new ConnectLib();
29+
private ConnectLib connectLib = new ConnectLib();
3030

3131
/**
3232
* Initializes the HookManager with the specified resource type.
@@ -43,8 +43,8 @@ public class HookManager {
4343
* @return the initialized resource type
4444
*/
4545
public ResourceType initHook(ResourceType resourceType) {
46-
HookManager.resourceType = resourceType;
47-
return HookManager.resourceType;
46+
this.resourceType = resourceType;
47+
return this.resourceType;
4848
}
4949

5050
/**
@@ -55,7 +55,7 @@ public ResourceType initHook(ResourceType resourceType) {
5555
public void FILE_LOCATION_KEY() {
5656
switch (resourceType) {
5757
case MC_RESOURCES:
58-
connectLib.StoreAndRetrieve().store.put(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY, connectLib.MCSupport().getPluginPath());
58+
connectLib.StoreAndRetrieve().store.put(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY, connectLib.MCSupport().getPluginPath());
5959
break;
6060
case MAIN_RESOURCES, TEST_RESOURCES:
6161
connectLib.StoreAndRetrieve().store.put(connectLib.StoreAndRetrieve().FILE_LOCATION_KEY, resourceType.getPath());

0 commit comments

Comments
 (0)