99
1010import java .util .Map ;
1111
12- import org .apache .logging .log4j .LogManager ;
13- import org .apache .logging .log4j .Logger ;
1412import org .folio .okapi .common .WebClientFactory ;
1513import org .folio .rest .core .models .RequestContext ;
1614import org .folio .rest .exception .HttpException ;
2119import io .vertx .core .MultiMap ;
2220import io .vertx .core .Promise ;
2321import io .vertx .core .http .HttpMethod ;
22+ import io .vertx .core .http .HttpResponseExpectation ;
2423import io .vertx .core .json .JsonObject ;
2524import io .vertx .ext .web .client .HttpResponse ;
2625import io .vertx .ext .web .client .WebClient ;
2726import io .vertx .ext .web .client .WebClientOptions ;
28- import io .vertx .ext .web .client .predicate .ErrorConverter ;
29- import io .vertx .ext .web .client .predicate .ResponsePredicate ;
27+ import lombok .extern .log4j .Log4j2 ;
3028
29+ @ Log4j2
3130public class RestClient {
3231
33- private static final Logger log = LogManager .getLogger ();
34- private static final ErrorConverter ERROR_CONVERTER = ErrorConverter .createFullBody (
35- result -> {
36- String errorResponse = result .response ().bodyAsString ();
37- if (isJsonOfType (errorResponse , Errors .class )) {
38- return new HttpException (result .response ().statusCode (), mapToErrors (errorResponse ));
39- }
40- else {
41- return getErrorByCode (errorResponse )
42- .map (errorCode -> new HttpException (result .response ().statusCode (), errorCode ))
43- .orElse (new HttpException (result .response ().statusCode (), errorResponse ));
44- }
45- });
46- protected static final ResponsePredicate SUCCESS_RESPONSE_PREDICATE =
47- ResponsePredicate .create (ResponsePredicate .SC_SUCCESS , ERROR_CONVERTER );
4832 public static final String REQUEST_MESSAGE_LOG_INFO = "Calling {} {}" ;
4933
5034 public <T > Future <T > post (String endpoint , T entity , Class <T > responseType , RequestContext requestContext ) {
@@ -53,8 +37,8 @@ public <T> Future<T> post(String endpoint, T entity, Class<T> responseType, Requ
5337 return getVertxWebClient (requestContext .context ())
5438 .postAbs (buildAbsEndpoint (caseInsensitiveHeader , endpoint ))
5539 .putHeaders (caseInsensitiveHeader )
56- .expect (SUCCESS_RESPONSE_PREDICATE )
5740 .sendJson (entity )
41+ .compose (RestClient ::convertHttpResponse )
5842 .map (HttpResponse ::bodyAsJsonObject )
5943 .map (body -> body .mapTo (responseType ))
6044 .onFailure (log ::error );
@@ -66,8 +50,8 @@ public <T> Future<Void> postEmptyResponse(String endpoint, T entity, RequestCont
6650 return getVertxWebClient (requestContext .context ())
6751 .postAbs (buildAbsEndpoint (caseInsensitiveHeader , endpoint ))
6852 .putHeaders (caseInsensitiveHeader )
69- .expect (SUCCESS_RESPONSE_PREDICATE )
7053 .sendJson (entity )
54+ .compose (RestClient ::convertHttpResponse )
7155 .onFailure (log ::error )
7256 .mapEmpty ();
7357 }
@@ -78,8 +62,8 @@ public <REQ, RES> Future<RES> postBatch(String endpoint, REQ requestEntity, Clas
7862 return getVertxWebClient (requestContext .context ())
7963 .postAbs (buildAbsEndpoint (caseInsensitiveHeader , endpoint ))
8064 .putHeaders (caseInsensitiveHeader )
81- .expect (SUCCESS_RESPONSE_PREDICATE )
8265 .sendJson (requestEntity )
66+ .compose (RestClient ::convertHttpResponse )
8367 .map (HttpResponse ::bodyAsJsonObject )
8468 .map (body -> body .mapTo (responseType ))
8569 .onFailure (log ::error );
@@ -101,8 +85,8 @@ public <T> Future<Void> put(String endpoint, T dataObject, RequestContext reques
10185 return getVertxWebClient (requestContext .context ())
10286 .putAbs (buildAbsEndpoint (caseInsensitiveHeader , endpoint ))
10387 .putHeaders (caseInsensitiveHeader )
104- .expect (SUCCESS_RESPONSE_PREDICATE )
10588 .sendJson (recordData )
89+ .compose (RestClient ::convertHttpResponse )
10690 .onFailure (log ::error )
10791 .mapEmpty ();
10892 }
@@ -116,8 +100,8 @@ public <T> Future<T> put(String endpoint, T dataObject, Class<T> responseType ,R
116100 return getVertxWebClient (requestContext .context ())
117101 .putAbs (buildAbsEndpoint (caseInsensitiveHeader , endpoint ))
118102 .putHeaders (caseInsensitiveHeader )
119- .expect (SUCCESS_RESPONSE_PREDICATE )
120103 .sendJson (recordData )
104+ .compose (RestClient ::convertHttpResponse )
121105 .map (HttpResponse ::bodyAsJsonObject )
122106 .map (jsonObject -> jsonObject .mapTo (responseType ))
123107 .onFailure (log ::error );
@@ -132,8 +116,8 @@ public Future<Void> delete(String endpointById, boolean skipError404, RequestCon
132116 getVertxWebClient (requestContext .context ())
133117 .deleteAbs (buildAbsEndpoint (caseInsensitiveHeader , endpointById ))
134118 .putHeaders (caseInsensitiveHeader )
135- .expect (SUCCESS_RESPONSE_PREDICATE )
136119 .send ()
120+ .compose (RestClient ::convertHttpResponse )
137121 .onSuccess (f -> promise .complete ())
138122 .onFailure (t -> handleErrorResponse (promise , t , skipError404 ));
139123
@@ -177,8 +161,8 @@ public <T> Future<T> get(String endpoint, boolean skipError404, Class<T> respons
177161 getVertxWebClient (requestContext .context ())
178162 .getAbs (absEndpoint )
179163 .putHeaders (caseInsensitiveHeader )
180- .expect (SUCCESS_RESPONSE_PREDICATE )
181164 .send ()
165+ .compose (RestClient ::convertHttpResponse )
182166 .map (HttpResponse ::bodyAsJsonObject )
183167 .map (jsonObject -> jsonObject .mapTo (responseType ))
184168 .onSuccess (promise ::complete )
@@ -187,20 +171,35 @@ public <T> Future<T> get(String endpoint, boolean skipError404, Class<T> respons
187171 return promise .future ();
188172 }
189173
190- protected WebClient getVertxWebClient (Context context ) {
174+ private static WebClient getVertxWebClient (Context context ) {
191175 WebClientOptions options = new WebClientOptions ();
192176 options .setLogActivity (true );
193177 options .setKeepAlive (true );
194178 options .setConnectTimeout (2000 );
195179 options .setIdleTimeout (5000 );
196- options .setTryUseCompression (true );
197-
180+ options .setDecompressionSupported (true );
198181 return WebClientFactory .getWebClient (context .owner (), options );
199182 }
200183
201- protected String buildAbsEndpoint (MultiMap okapiHeaders , String endpoint ) {
184+ private static String buildAbsEndpoint (MultiMap okapiHeaders , String endpoint ) {
202185 var okapiURL = okapiHeaders .get (OKAPI_URL );
203186 return okapiURL + endpoint ;
204187 }
205188
189+ private static <T > Future <HttpResponse <T >> convertHttpResponse (HttpResponse <T > response ) {
190+ if (HttpResponseExpectation .SC_SUCCESS .test (response )) {
191+ return Future .succeededFuture (response );
192+ }
193+ HttpException exception ;
194+ var errorResponse = response .bodyAsString ();
195+ if (isJsonOfType (errorResponse , Errors .class )) {
196+ exception = new HttpException (response .statusCode (), mapToErrors (errorResponse ));
197+ } else {
198+ exception = getErrorByCode (errorResponse )
199+ .map (errorCode -> new HttpException (response .statusCode (), errorCode ))
200+ .orElse (new HttpException (response .statusCode (), errorResponse ));
201+ }
202+ return Future .failedFuture (exception );
203+ }
204+
206205}
0 commit comments