Skip to content

Commit d832c03

Browse files
committed
Add status code handling to API client methods and update version to 0.3.5-STABLE
1 parent a158164 commit d832c03

8 files changed

Lines changed: 149 additions & 36 deletions

File tree

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

1010
tasks.register('printVersion') {
1111
doLast {

readme.md

Lines changed: 5 additions & 4 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.4-STABLE
20+
Stable Version: 0.3.5-STABLE
2121
```
2222

2323
---
@@ -34,8 +34,8 @@ Hook -----------------------|
3434
---
3535
## LangManager
3636
```
37-
EN : Released - Version 1.2
38-
FR : Sorti - Version 1.1
37+
EN : Released - Version 1.3 - Latest Version
38+
FR : Sorti - Version 1.2 - Latest Version
3939
```
4040

4141
---
@@ -59,6 +59,7 @@ Changelog:
5959
- [0.2.7.2-STABLE]: Remove implementation Project Reactor
6060
- [0.2.9-STABLE]: Added support query variables in routes, allowing you to pass parameters directly in the URL.
6161
- [0.3.3-STABLE]: Minecraft Version 1.16 - Latest Version support + LangType 2.0 - Remove AnnotHandler, you can use logger.showLogs(); to display logs.
62+
- [0.3.5-STABLE]: Now you can print the StatusCode of the response with ( response.getStatusCode(); )
6263
```
6364

6465
---
@@ -116,7 +117,7 @@ repositories {
116117

117118
dependencies {
118119

119-
implementation("fr.sandro642.github:ConnectLib:0.3.3-STABLE")
120+
implementation("fr.sandro642.github:ConnectLib:0.3.5-STABLE")
120121

121122
}
122123

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

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,22 @@ public ApiClient() {
6868
*/
6969
public Mono<ApiFactory> callAPIGet(String routeName) {
7070
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.get", Map.of("routename", routeName)));
71+
72+
record ResponseData(int statusCode, String body) {}
73+
7174
return webClient.get()
7275
.uri(routeName)
73-
.retrieve()
74-
.bodyToMono(String.class)
76+
.exchangeToMono(response ->
77+
response.bodyToMono(String.class)
78+
.map(rawJson -> new ResponseData(response.statusCode().value(), rawJson))
79+
)
7580
.subscribeOn(Schedulers.boundedElastic())
76-
.doOnNext(thread ->
77-
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName())))
78-
.map(rawJson -> {
79-
apiFactory.parseFromRawJson(rawJson);
81+
.doOnNext(responseData -> {
82+
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));
83+
})
84+
.map(responseData -> {
85+
apiFactory.setStatusCode(responseData.statusCode());
86+
apiFactory.parseFromRawJson(responseData.body);
8087
return apiFactory;
8188
})
8289
.doOnNext(lastResponse::set)
@@ -91,16 +98,23 @@ public Mono<ApiFactory> callAPIGet(String routeName) {
9198
*/
9299
public Mono<ApiFactory> callAPIPost(String routeName, Map<String, Object> body) {
93100
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.post", Map.of("routename", routeName)));
101+
102+
record ResponseData(int statusCode, String body) {}
103+
94104
return webClient.post()
95105
.uri(routeName)
96106
.bodyValue(body != null ? body : Map.of())
97-
.retrieve()
98-
.bodyToMono(String.class)
107+
.exchangeToMono(response ->
108+
response.bodyToMono(String.class)
109+
.map(rawJson -> new ResponseData(response.statusCode().value(), rawJson))
110+
)
99111
.subscribeOn(Schedulers.boundedElastic())
100-
.doOnNext(thread ->
101-
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName())))
102-
.map(rawJson -> {
103-
apiFactory.parseFromRawJson(rawJson);
112+
.doOnNext(responseData -> {
113+
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));
114+
})
115+
.map(responseData -> {
116+
apiFactory.setStatusCode(responseData.statusCode());
117+
apiFactory.parseFromRawJson(responseData.body());
104118
return apiFactory;
105119
})
106120
.doOnNext(lastResponse::set)
@@ -115,16 +129,23 @@ public Mono<ApiFactory> callAPIPost(String routeName, Map<String, Object> body)
115129
*/
116130
public Mono<ApiFactory> callAPIPut(String routeName, Map<String, Object> body) {
117131
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.put", Map.of("routename", routeName)));
132+
133+
record ResponseData(int statusCode, String body) {}
134+
118135
return webClient.put()
119136
.uri(routeName)
120137
.bodyValue(body != null ? body : Map.of())
121-
.retrieve()
122-
.bodyToMono(String.class)
138+
.exchangeToMono(response ->
139+
response.bodyToMono(String.class)
140+
.map(rawJson -> new ResponseData(response.statusCode().value(), rawJson))
141+
)
123142
.subscribeOn(Schedulers.boundedElastic())
124-
.doOnNext(thread ->
125-
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName())))
126-
.map(rawJson -> {
127-
apiFactory.parseFromRawJson(rawJson);
143+
.doOnNext(responseData -> {
144+
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));
145+
})
146+
.map(responseData -> {
147+
apiFactory.setStatusCode(responseData.statusCode());
148+
apiFactory.parseFromRawJson(responseData.body());
128149
return apiFactory;
129150
})
130151
.doOnNext(lastResponse::set)
@@ -139,16 +160,23 @@ public Mono<ApiFactory> callAPIPut(String routeName, Map<String, Object> body) {
139160
*/
140161
public Mono<ApiFactory> callAPIPatch(String routeName, Map<String, Object> body) {
141162
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.patch", Map.of("routename", routeName)));
163+
164+
record ResponseData(int statusCode, String body) {}
165+
142166
return webClient.patch()
143167
.uri(routeName)
144168
.bodyValue(body != null ? body : Map.of())
145-
.retrieve()
146-
.bodyToMono(String.class)
169+
.exchangeToMono(response ->
170+
response.bodyToMono(String.class)
171+
.map(rawJson -> new ResponseData(response.statusCode().value(), rawJson))
172+
)
147173
.subscribeOn(Schedulers.boundedElastic())
148-
.doOnNext(thread ->
149-
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName())))
150-
.map(rawJson -> {
151-
apiFactory.parseFromRawJson(rawJson);
174+
.doOnNext(responseData -> {
175+
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));
176+
})
177+
.map(responseData -> {
178+
apiFactory.setStatusCode(responseData.statusCode());
179+
apiFactory.parseFromRawJson(responseData.body());
152180
return apiFactory;
153181
})
154182
.doOnNext(lastResponse::set)
@@ -162,15 +190,22 @@ public Mono<ApiFactory> callAPIPatch(String routeName, Map<String, Object> body)
162190
*/
163191
public Mono<ApiFactory> callAPIDelete(String routeName) {
164192
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.delete", Map.of("routename", routeName)));
193+
194+
record ResponseData(int statusCode, String body) {}
195+
165196
return webClient.delete()
166197
.uri(routeName)
167-
.retrieve()
168-
.bodyToMono(String.class)
198+
.exchangeToMono(response ->
199+
response.bodyToMono(String.class)
200+
.map(rawJson -> new ResponseData(response.statusCode().value(), rawJson))
201+
)
169202
.subscribeOn(Schedulers.boundedElastic())
170-
.doOnNext(thread ->
171-
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName())))
172-
.map(rawJson -> {
173-
apiFactory.parseFromRawJson(rawJson);
203+
.doOnNext(responseData -> {
204+
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));
205+
})
206+
.map(responseData -> {
207+
apiFactory.setStatusCode(responseData.statusCode());
208+
apiFactory.parseFromRawJson(responseData.body());
174209
return apiFactory;
175210
})
176211
.doOnNext(lastResponse::set)

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public class ApiFactory {
3535
*/
3636
private String rawJson;
3737

38+
/**
39+
* statusCode is an integer that holds the HTTP status code of the API response.
40+
* It can be set using the setStatusCode method.
41+
*/
42+
private int statusCode;
43+
3844
/**
3945
* Default constructor for ApiFactory.
4046
* Initializes the rawData and rawJson fields to null.
@@ -49,6 +55,15 @@ protected void parseFromRawJson(String rawJson) {
4955
}
5056
}
5157

58+
/**
59+
* Method to retrieve the raw JSON data as a string.
60+
*
61+
* @return The raw JSON data as a string, or null if not initialized.
62+
*/
63+
protected void setStatusCode(int statusCode) {
64+
this.statusCode = statusCode;
65+
}
66+
5267
/**
5368
* Method to retrieve data from the rawData map based on the provided type.
5469
* The type is converted to lowercase to ensure case-insensitive matching.
@@ -96,6 +111,25 @@ public <O, K> Object getSpecData(O type, K value) {
96111
return null;
97112
}
98113

114+
/**
115+
* Method to retrieve the HTTP status code of the API response.
116+
*
117+
* @return The HTTP status code of the API response.
118+
*/
119+
public int getStatusCode() {
120+
try {
121+
if (rawData == null) {
122+
connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.APIFACTORY_CLASS, "general.mustbe"));
123+
return -1;
124+
}
125+
126+
return statusCode;
127+
} catch (Exception e) {
128+
connectLib.Logger().ERROR(connectLib.LangManager().getMessage(CategoriesType.APIFACTORY_CLASS, "getstatus.error", "exception", e.getMessage()));
129+
}
130+
return -1;
131+
}
132+
99133
/**
100134
* Method to display the raw data stored in the ApiFactory.
101135
* This method returns the rawData map, which contains the parsed JSON data.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public void exampleMethodSync() {
6363
System.out.println("Response Code: " + response.getData("code"));
6464
System.out.println("Response Message: " + response.getData("message"));
6565
System.out.println("Response Data: " + response.getSpecData("data", "exampleKey"));
66+
System.out.println("Status Code: " + response.getStatusCode());
6667
} catch (Exception e) {
6768
e.printStackTrace();
6869
}

src/main/resources/lang/EN_US.lang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ general.mustbe: Data has not been initialized. Please call parseFromRawJson() fi
4040
getdata.error: Unable to retrieve data for type: %type%, Exception: %exception%.
4141
getspecdata.error: Unable to retrieve specific data for type: %type% and value %value%. Exception: %exception%.
4242
display.error: Error while displaying data. Exception: %exception%.
43+
getstatus.error: Error while retrieving status code. Exception: %exception%.
4344

4445
[apiclient.class]
4546
construct.urlbase: Base URL not found in configuration. Please set the base URL in the configuration file.

src/main/resources/lang/FR_EU.lang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ general.mustbe: Les données n'ont pas été initialisées. Veuillez d'abord app
4040
getdata.error: Impossible de récupérer les données pour le type: %type%, Exception: %exception%.
4141
getspecdata.error: Impossible de récupérer les données spécifiques pour le type: %type% et la valeur %value%. Exception: %exception%.
4242
display.error: Erreur lors de l'affichage des données. Exception: %exception%.
43+
getstatus.error: Erreur lors de la récupération du code d'état. Exception: %exception%.
4344

4445
[apiclient.class]
4546
construct.urlbase: URL de base introuvable dans la configuration. Veuillez définir l'URL de base dans le fichier de configuration.

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public static void main(String[] args) {
7373
public void testUseFullRoute() {
7474
connectLib.Init(ResourceType.TEST_RESOURCES, LangType.FRENCH, TestRoutes.class);
7575

76+
connectLib.Logger().showLogs();
7677
try {
7778
Map<String, String> query = Map.of(
7879
"name", "Sandro642"
@@ -85,6 +86,7 @@ public void testUseFullRoute() {
8586
ApiFactory response = factoryCompletableFuture.get(5, TimeUnit.SECONDS);
8687

8788
System.out.println("Response: " + response.display());
89+
System.out.println("Status Code: " + response.getStatusCode());
8890

8991
} catch (Exception e) {
9092
e.printStackTrace();
@@ -93,7 +95,45 @@ public void testUseFullRoute() {
9395

9496
@Test
9597
public void testLangType() {
96-
connectLib.Init(ResourceType.TEST_RESOURCES, LangType.FRENCH, TestRoutes.class);
98+
connectLib.Init(ResourceType.TEST_RESOURCES, LangType.ENGLISH, TestRoutes.class);
99+
100+
connectLib.Logger().showLogs();
101+
102+
try {
103+
CompletableFuture<ApiFactory> factoryCompletableFuture = connectLib.JobGetInfos()
104+
.getRoutes(MethodType.GET, TestRoutes.HELLO)
105+
.getResponse();
106+
107+
ApiFactory response = factoryCompletableFuture.get(5, TimeUnit.SECONDS);
108+
109+
System.out.println("Response: " + response.display());
110+
System.out.println("Status Code: " + response.getStatusCode());
111+
112+
Thread.sleep(5000);
113+
114+
factoryCompletableFuture = connectLib.JobGetInfos()
115+
.getRoutes(MethodType.GET, TestRoutes.GREET, null, null, Map.of("name", "Sandro642"))
116+
.getResponse();
117+
118+
response = factoryCompletableFuture.get(5, TimeUnit.SECONDS);
119+
120+
System.out.println("Response: " + response.display());
121+
System.out.println("Status Code: " + response.getStatusCode());
122+
123+
Thread.sleep(5000);
124+
125+
factoryCompletableFuture = connectLib.JobGetInfos()
126+
.getRoutes(MethodType.GET, TestRoutes.GREET, null, null, Map.of("name", "Sandro642"))
127+
.getResponse();
128+
129+
response = factoryCompletableFuture.get(5, TimeUnit.SECONDS);
130+
131+
System.out.println("Response: " + response.display());
132+
System.out.println("Status Code: " + response.getStatusCode());
133+
134+
} catch (Exception e) {
135+
e.printStackTrace();
136+
}
97137
}
98138

99139
}

0 commit comments

Comments
 (0)