-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiClient.java
More file actions
224 lines (198 loc) · 10.5 KB
/
ApiClient.java
File metadata and controls
224 lines (198 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package fr.sandro642.github.api;
import fr.sandro642.github.ConnectLib;
import fr.sandro642.github.enums.lang.CategoriesType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
/**
* ApiClient is a class that provides methods to interact with the ConnectLib API.
* It extends ApiFactory and uses WebClient to make HTTP requests.
* This class supports various HTTP methods including GET, POST, PUT, PATCH, and DELETE.
* It handles the API responses and errors, logging them appropriately.
*
* @author Sandro642
* @version 1.0
*/
public class ApiClient extends ApiFactory {
/**
* connectLib is an instance of ConnectLib that provides access to the library's configuration and utilities.
*/
private final ConnectLib connectLib = new ConnectLib();
/**
* WebClient is a non-blocking, reactive HTTP client for making requests to the API.
* It is initialized with the base URL from the ConnectLib configuration.
*/
private final WebClient webClient;
/**
* lastResponse is an AtomicReference that holds the last response received from the API.
* It is used to store the response for later retrieval or processing.
*/
private final AtomicReference<ApiFactory> lastResponse = new AtomicReference<>();
/**
* response is an instance of ApiFactory that is used to parse and store the raw JSON response from the API.
* It provides methods to handle the response data.
*/
private final ApiFactory apiFactory = new ApiFactory();
/**
* 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;
if (baseUrl == null) {
connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "construct.urlbase"));
}
this.webClient = WebClient.builder()
.baseUrl(baseUrl)
.build();
}
/**
* Method to call the API with a GET request.
*
* @param routeName Name of the route to call.
* @return a Mono that emits the ApiFactory response containing the parsed JSON data.
*/
public Mono<ApiFactory> callAPIGet(String routeName) {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.get", Map.of("routename", routeName)));
record ResponseData(int statusCode, String body) {
}
return webClient.get()
.uri(routeName)
.exchangeToMono(response ->
response.bodyToMono(String.class)
.map(rawJson -> new ResponseData(response.statusCode().value(), rawJson))
)
.subscribeOn(Schedulers.boundedElastic())
.doOnNext(responseData -> {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));
})
.map(responseData -> {
apiFactory.setStatusCode(responseData.statusCode());
apiFactory.parseFromRawJson(responseData.body);
return apiFactory;
})
.doOnNext(lastResponse::set)
.doOnError(error -> connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "GET", "exception", error.getMessage()))));
}
/**
* Method to call the API with a POST request.
*
* @param routeName Name of the route to call.
* @param body Body of the request (can be null for a request without body).
* @return a Mono that emits the ApiFactory response containing the parsed JSON data.
*/
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)));
record ResponseData(int statusCode, String body) {
}
return webClient.post()
.uri(routeName)
.bodyValue(body != null ? body : Map.of())
.exchangeToMono(response ->
response.bodyToMono(String.class)
.map(rawJson -> new ResponseData(response.statusCode().value(), rawJson))
)
.subscribeOn(Schedulers.boundedElastic())
.doOnNext(responseData -> {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));
})
.map(responseData -> {
apiFactory.setStatusCode(responseData.statusCode());
apiFactory.parseFromRawJson(responseData.body());
return apiFactory;
})
.doOnNext(lastResponse::set)
.doOnError(error -> connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "POST", "exception", error.getMessage()))));
}
/**
* Method to call the API with a PUT request.
*
* @param routeName Name of the route to call.
* @param body Body of the request (can be null for a request without body).
* @return a Mono that emits the ApiFactory response containing the parsed JSON data.
*/
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)));
record ResponseData(int statusCode, String body) {
}
return webClient.put()
.uri(routeName)
.bodyValue(body != null ? body : Map.of())
.exchangeToMono(response ->
response.bodyToMono(String.class)
.map(rawJson -> new ResponseData(response.statusCode().value(), rawJson))
)
.subscribeOn(Schedulers.boundedElastic())
.doOnNext(responseData -> {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));
})
.map(responseData -> {
apiFactory.setStatusCode(responseData.statusCode());
apiFactory.parseFromRawJson(responseData.body());
return apiFactory;
})
.doOnNext(lastResponse::set)
.doOnError(error -> connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "PUT", "exception", error.getMessage()))));
}
/**
* Method to call the API with a PATCH request.
*
* @param routeName Name of the route to call.
* @param body Body of the request (can be null for a request without body).
* @return a Mono that emits the ApiFactory response containing the parsed JSON data.
*/
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)));
record ResponseData(int statusCode, String body) {
}
return webClient.patch()
.uri(routeName)
.bodyValue(body != null ? body : Map.of())
.exchangeToMono(response ->
response.bodyToMono(String.class)
.map(rawJson -> new ResponseData(response.statusCode().value(), rawJson))
)
.subscribeOn(Schedulers.boundedElastic())
.doOnNext(responseData -> {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));
})
.map(responseData -> {
apiFactory.setStatusCode(responseData.statusCode());
apiFactory.parseFromRawJson(responseData.body());
return apiFactory;
})
.doOnNext(lastResponse::set)
.doOnError(error -> connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "PATCH", "exception", error.getMessage()))));
}
/**
* Method to call the API with a DELETE request.
*
* @param routeName Name of the route to call.
* @return a Mono that emits the ApiFactory response containing the parsed JSON data.
*/
public Mono<ApiFactory> callAPIDelete(String routeName) {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.delete", Map.of("routename", routeName)));
record ResponseData(int statusCode, String body) {
}
return webClient.delete()
.uri(routeName)
.exchangeToMono(response ->
response.bodyToMono(String.class)
.map(rawJson -> new ResponseData(response.statusCode().value(), rawJson))
)
.subscribeOn(Schedulers.boundedElastic())
.doOnNext(responseData -> {
connectLib.Logger().INFO(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "call.threadinuse", "thread", Thread.currentThread().getName()));
})
.map(responseData -> {
apiFactory.setStatusCode(responseData.statusCode());
apiFactory.parseFromRawJson(responseData.body());
return apiFactory;
})
.doOnNext(lastResponse::set)
.doOnError(error -> connectLib.Logger().CRITICAL(connectLib.LangManager().getMessage(CategoriesType.APICLIENT_CLASS, "general.error", Map.of("method", "DELETE", "exception", error.getMessage()))));
}
}