diff options
author | Marcin Migdal <marcin.migdal@nokia.com> | 2019-03-05 14:58:14 +0100 |
---|---|---|
committer | Marcin Migdal <marcin.migdal@nokia.com> | 2019-03-05 14:59:34 +0100 |
commit | 47ffd5638c8799186a49683f6b4e4bd126a02214 (patch) | |
tree | acf206de666a4a83fe230978a093a9496ee48c37 | |
parent | 70818e77b3c4ba56644748be2c502fb74b24a7dd (diff) |
Spring removal
Change-Id: I7f886d6817d3491f27b9f217d52ccaf3e421c530
Issue-ID: DCAEGEN2-1310
Signed-off-by: mmigdal <marcin.migdal@nokia.com>
Signed-off-by: Marcin Migdal <marcin.migdal@nokia.com>
-rw-r--r-- | rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClient.java | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClient.java b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClient.java index cba5c6af..33c39fa6 100644 --- a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClient.java +++ b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClient.java @@ -29,10 +29,13 @@ import java.util.Collections; import java.util.Map; import java.util.function.BiConsumer; import java.util.stream.Collectors; +import org.onap.dcaegen2.services.sdk.rest.services.model.ClientModel; +import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder; import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; +import reactor.netty.ByteBufFlux; import reactor.netty.http.client.HttpClient; import reactor.netty.http.client.HttpClientRequest; import reactor.netty.http.client.HttpClientResponse; @@ -64,12 +67,9 @@ public class CloudHttpClient { return get(url, context, Collections.EMPTY_MAP, bodyClass); } - public <T> Mono<T> get(String url, RequestDiagnosticContext context, Map<String, String> customHeaders, Class<T> bodyClass) { - final HttpClient clientWithHeaders = httpClient - .doOnRequest((req, conn) -> logRequest(context, req)) - .doOnResponse((rsp, conn) -> logResponse(context, rsp)) - .headers(hdrs -> context.remoteCallHttpHeaders().forEach((BiConsumer<String, String>) hdrs::set)) - .headers(hdrs -> customHeaders.forEach(hdrs::set)); + public <T> Mono<T> get(String url, RequestDiagnosticContext context, Map<String, String> customHeaders, + Class<T> bodyClass) { + final HttpClient clientWithHeaders = getHttpClientWithHeaders(context, customHeaders); return callHttpGet(clientWithHeaders, url, bodyClass); } @@ -77,6 +77,27 @@ public class CloudHttpClient { return callHttpGet(httpClient, url, bodyClass); } + public Mono<Integer> post(String url, RequestDiagnosticContext context, Map<String, String> customHeaders, + JsonBodyBuilder jsonBodyBuilder, ClientModel clientModel) { + final HttpClient clientWithHeaders = getHttpClientWithHeaders(context, customHeaders); + return callHttpPost(clientWithHeaders, url, jsonBodyBuilder, clientModel); + } + + public Mono<Integer> patch(String url, RequestDiagnosticContext context, Map<String, String> customHeaders, + JsonBodyBuilder jsonBodyBuilder, ClientModel clientModel) { + final HttpClient clientWithHeaders = getHttpClientWithHeaders(context, customHeaders); + return callHttpPatch(clientWithHeaders, url, jsonBodyBuilder, clientModel); + } + + private HttpClient getHttpClientWithHeaders(RequestDiagnosticContext context, Map<String, String> customHeaders) { + final HttpClient clientWithHeaders = httpClient + .doOnRequest((req, conn) -> logRequest(context, req)) + .doOnResponse((rsp, conn) -> logResponse(context, rsp)) + .headers(hdrs -> context.remoteCallHttpHeaders().forEach((BiConsumer<String, String>) hdrs::set)) + .headers(hdrs -> customHeaders.forEach(hdrs::set)); + return clientWithHeaders; + } + private <T> Mono<T> callHttpGet(HttpClient client, String url, Class<T> bodyClass) { return client.get() .uri(url) @@ -86,6 +107,20 @@ public class CloudHttpClient { .map(body -> parseJson(body, bodyClass)); } + private <T extends ClientModel> Mono<Integer> callHttpPost(HttpClient client, String url, + JsonBodyBuilder<T> jsonBodyBuilder, T clientModel) { + return client.baseUrl(url).post() + .send(ByteBufFlux.fromString(Mono.just(jsonBodyBuilder.createJsonBody(clientModel)))) + .responseSingle((httpClientResponse, byteBufMono) -> Mono.just(httpClientResponse.status().code())); + } + + private <T extends ClientModel> Mono<Integer> callHttpPatch(HttpClient client, String url, + JsonBodyBuilder<T> jsonBodyBuilder, T clientModel) { + return client.baseUrl(url).patch() + .send(ByteBufFlux.fromString(Mono.just(jsonBodyBuilder.createJsonBody(clientModel)))) + .responseSingle((httpClientResponse, byteBufMono) -> Mono.just(httpClientResponse.status().code())); + } + private Exception createException(String url, HttpClientResponse response) { return new IOException(String.format("Request failed for URL '%s'. Response code: %s", url, |