22
33import fr .sandro642 .github .ConnectLib ;
44import fr .sandro642 .github .enums .lang .CategoriesType ;
5- import fr .sandro642 .github .misc .Logger ;
65import org .springframework .web .reactive .function .client .WebClient ;
76import reactor .core .publisher .Mono ;
87import reactor .core .scheduler .Schedulers ;
2221
2322public class ApiClient extends ApiFactory {
2423
24+ /**
25+ * connectLib is an instance of ConnectLib that provides access to the library's configuration and utilities.
26+ */
27+ private ConnectLib connectLib = new ConnectLib ();
28+
2529 /**
2630 * WebClient is a non-blocking, reactive HTTP client for making requests to the API.
2731 * It is initialized with the base URL from the ConnectLib configuration.
@@ -34,28 +38,22 @@ public class ApiClient extends ApiFactory {
3438 */
3539 private final AtomicReference <ApiFactory > lastResponse = new AtomicReference <>();
3640
37- /**
38- * logger is an instance of Logger used for logging messages at different levels.
39- * It provides methods to log informational, warning, error, and critical messages.
40- */
41- private final Logger logger = new Logger ();
42-
4341 /**
4442 * response is an instance of ApiFactory that is used to parse and store the raw JSON response from the API.
4543 * It provides methods to handle the response data.
4644 */
47- private final ApiFactory response = new ApiFactory ();
45+ private final ApiFactory apiFactory = new ApiFactory ();
4846
4947 /**
5048 * Constructor for ApiClient.
5149 * It initializes the WebClient with the base URL from the ConnectLib configuration.
5250 * If the base URL is not found, it throws a RuntimeException.
5351 */
5452 public ApiClient () {
55- String baseUrl = (String ) ConnectLib .StoreAndRetrieve ().store .get (ConnectLib .StoreAndRetrieve ().URL_KEY );
53+ String baseUrl = (String ) connectLib .StoreAndRetrieve ().store .get (connectLib .StoreAndRetrieve ().URL_KEY );
5654
5755 if (baseUrl == null ) {
58- logger . CRITICAL (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "construct.urlbase" ));
56+ connectLib . Logger (). CRITICAL (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "construct.urlbase" ));
5957 }
6058
6159 this .webClient = WebClient .builder ()
@@ -69,20 +67,20 @@ public ApiClient() {
6967 * @return a Mono that emits the ApiFactory response containing the parsed JSON data.
7068 */
7169 public Mono <ApiFactory > callAPIGet (String routeName ) {
72- logger . INFO (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.get" , Map .of ("routename" , routeName )));
70+ connectLib . Logger (). INFO (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.get" , Map .of ("routename" , routeName )));
7371 return webClient .get ()
7472 .uri (routeName )
7573 .retrieve ()
7674 .bodyToMono (String .class )
7775 .subscribeOn (Schedulers .boundedElastic ())
7876 .doOnNext (thread ->
79- ConnectLib .Logger ().INFO (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.threadinuse" , "thread" , Thread .currentThread ().getName ())))
77+ connectLib .Logger ().INFO (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.threadinuse" , "thread" , Thread .currentThread ().getName ())))
8078 .map (rawJson -> {
81- response .parseFromRawJson (rawJson );
82- return response ;
79+ apiFactory .parseFromRawJson (rawJson );
80+ return apiFactory ;
8381 })
8482 .doOnNext (lastResponse ::set )
85- .doOnError (error -> logger . CRITICAL (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "general.error" , Map .of ("method" , "GET" , "exception" , error .getMessage ()))));
83+ .doOnError (error -> connectLib . Logger (). CRITICAL (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "general.error" , Map .of ("method" , "GET" , "exception" , error .getMessage ()))));
8684 }
8785
8886 /**
@@ -92,21 +90,21 @@ public Mono<ApiFactory> callAPIGet(String routeName) {
9290 * @return a Mono that emits the ApiFactory response containing the parsed JSON data.
9391 */
9492 public Mono <ApiFactory > callAPIPost (String routeName , Map <String , Object > body ) {
95- logger . INFO (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.post" , Map .of ("routename" , routeName )));
93+ connectLib . Logger (). INFO (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.post" , Map .of ("routename" , routeName )));
9694 return webClient .post ()
9795 .uri (routeName )
9896 .bodyValue (body != null ? body : Map .of ())
9997 .retrieve ()
10098 .bodyToMono (String .class )
10199 .subscribeOn (Schedulers .boundedElastic ())
102100 .doOnNext (thread ->
103- ConnectLib .Logger ().INFO (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.threadinuse" , "thread" , Thread .currentThread ().getName ())))
101+ connectLib .Logger ().INFO (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.threadinuse" , "thread" , Thread .currentThread ().getName ())))
104102 .map (rawJson -> {
105- response .parseFromRawJson (rawJson );
106- return response ;
103+ apiFactory .parseFromRawJson (rawJson );
104+ return apiFactory ;
107105 })
108106 .doOnNext (lastResponse ::set )
109- .doOnError (error -> logger . CRITICAL (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "general.error" , Map .of ("method" , "POST" , "exception" , error .getMessage ()))));
107+ .doOnError (error -> connectLib . Logger (). CRITICAL (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "general.error" , Map .of ("method" , "POST" , "exception" , error .getMessage ()))));
110108 }
111109
112110 /**
@@ -116,21 +114,21 @@ public Mono<ApiFactory> callAPIPost(String routeName, Map<String, Object> body)
116114 * @return a Mono that emits the ApiFactory response containing the parsed JSON data.
117115 */
118116 public Mono <ApiFactory > callAPIPut (String routeName , Map <String , Object > body ) {
119- logger . INFO (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.put" , Map .of ("routename" , routeName )));
117+ connectLib . Logger (). INFO (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.put" , Map .of ("routename" , routeName )));
120118 return webClient .put ()
121119 .uri (routeName )
122120 .bodyValue (body != null ? body : Map .of ())
123121 .retrieve ()
124122 .bodyToMono (String .class )
125123 .subscribeOn (Schedulers .boundedElastic ())
126124 .doOnNext (thread ->
127- ConnectLib .Logger ().INFO (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.threadinuse" , "thread" , Thread .currentThread ().getName ())))
125+ connectLib .Logger ().INFO (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.threadinuse" , "thread" , Thread .currentThread ().getName ())))
128126 .map (rawJson -> {
129- response .parseFromRawJson (rawJson );
130- return response ;
127+ apiFactory .parseFromRawJson (rawJson );
128+ return apiFactory ;
131129 })
132130 .doOnNext (lastResponse ::set )
133- .doOnError (error -> logger . CRITICAL (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "general.error" , Map .of ("method" , "PUT" , "exception" , error .getMessage ()))));
131+ .doOnError (error -> connectLib . Logger (). CRITICAL (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "general.error" , Map .of ("method" , "PUT" , "exception" , error .getMessage ()))));
134132 }
135133
136134 /**
@@ -140,21 +138,21 @@ public Mono<ApiFactory> callAPIPut(String routeName, Map<String, Object> body) {
140138 * @return a Mono that emits the ApiFactory response containing the parsed JSON data.
141139 */
142140 public Mono <ApiFactory > callAPIPatch (String routeName , Map <String , Object > body ) {
143- logger . INFO (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.patch" , Map .of ("routename" , routeName )));
141+ connectLib . Logger (). INFO (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.patch" , Map .of ("routename" , routeName )));
144142 return webClient .patch ()
145143 .uri (routeName )
146144 .bodyValue (body != null ? body : Map .of ())
147145 .retrieve ()
148146 .bodyToMono (String .class )
149147 .subscribeOn (Schedulers .boundedElastic ())
150148 .doOnNext (thread ->
151- ConnectLib .Logger ().INFO (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.threadinuse" , "thread" , Thread .currentThread ().getName ())))
149+ connectLib .Logger ().INFO (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.threadinuse" , "thread" , Thread .currentThread ().getName ())))
152150 .map (rawJson -> {
153- response .parseFromRawJson (rawJson );
154- return response ;
151+ apiFactory .parseFromRawJson (rawJson );
152+ return apiFactory ;
155153 })
156154 .doOnNext (lastResponse ::set )
157- .doOnError (error -> logger . CRITICAL (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "general.error" , Map .of ("method" , "PATCH" , "exception" , error .getMessage ()))));
155+ .doOnError (error -> connectLib . Logger (). CRITICAL (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "general.error" , Map .of ("method" , "PATCH" , "exception" , error .getMessage ()))));
158156 }
159157
160158 /**
@@ -163,19 +161,19 @@ public Mono<ApiFactory> callAPIPatch(String routeName, Map<String, Object> body)
163161 * @return a Mono that emits the ApiFactory response containing the parsed JSON data.
164162 */
165163 public Mono <ApiFactory > callAPIDelete (String routeName ) {
166- logger . INFO (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.delete" , Map .of ("routename" , routeName )));
164+ connectLib . Logger (). INFO (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.delete" , Map .of ("routename" , routeName )));
167165 return webClient .delete ()
168166 .uri (routeName )
169167 .retrieve ()
170168 .bodyToMono (String .class )
171169 .subscribeOn (Schedulers .boundedElastic ())
172170 .doOnNext (thread ->
173- ConnectLib .Logger ().INFO (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.threadinuse" , "thread" , Thread .currentThread ().getName ())))
171+ connectLib .Logger ().INFO (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "call.threadinuse" , "thread" , Thread .currentThread ().getName ())))
174172 .map (rawJson -> {
175- response .parseFromRawJson (rawJson );
176- return response ;
173+ apiFactory .parseFromRawJson (rawJson );
174+ return apiFactory ;
177175 })
178176 .doOnNext (lastResponse ::set )
179- .doOnError (error -> logger . CRITICAL (ConnectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "general.error" , Map .of ("method" , "DELETE" , "exception" , error .getMessage ()))));
177+ .doOnError (error -> connectLib . Logger (). CRITICAL (connectLib .LangManager ().getMessage (CategoriesType .APICLIENT_CLASS , "general.error" , Map .of ("method" , "DELETE" , "exception" , error .getMessage ()))));
180178 }
181179}
0 commit comments