From ab718d73ce25edf2c8a92d10cea13d594be46e62 Mon Sep 17 00:00:00 2001 From: Piotr Jaszczyk Date: Wed, 20 Mar 2019 14:49:13 +0100 Subject: Create an evolution of HTTP Client * simplify the API * use new http client in old one for compatibility * deprecate old one Issue-ID: DCAEGEN2-1010 Change-Id: Ief681ba536a37b29c10d133c61a1326a003ed308 Signed-off-by: Piotr Jaszczyk --- .../services/cbs/client/api/CbsClientFactory.java | 4 ++-- .../rest/services/cbs/client/impl/CbsClientImpl.java | 20 +++++++++++++++----- .../sdk/rest/services/cbs/client/impl/CbsLookup.java | 17 +++++++++++++---- 3 files changed, 30 insertions(+), 11 deletions(-) (limited to 'rest-services/cbs-client/src/main') diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java index 989bd2db..379daf97 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java @@ -20,7 +20,7 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api; import org.jetbrains.annotations.NotNull; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsClientImpl; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsLookup; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; @@ -53,7 +53,7 @@ public class CbsClientFactory { */ public static @NotNull Mono createCbsClient(EnvProperties env) { return Mono.defer(() -> { - final CloudHttpClient httpClient = new CloudHttpClient(); + final RxHttpClient httpClient = RxHttpClient.create(); final CbsLookup lookup = new CbsLookup(httpClient); return lookup.lookup(env) .map(addr -> CbsClientImpl.create(httpClient, addr, env.appName())); diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java index 9be08e3c..72c1b267 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java @@ -24,22 +24,24 @@ import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.URL; import org.jetbrains.annotations.NotNull; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; import reactor.core.publisher.Mono; public class CbsClientImpl implements CbsClient { - private final CloudHttpClient httpClient; + private final RxHttpClient httpClient; private final String fetchUrl; - CbsClientImpl(CloudHttpClient httpClient, URL fetchUrl) { + CbsClientImpl(RxHttpClient httpClient, URL fetchUrl) { this.httpClient = httpClient; this.fetchUrl = fetchUrl.toString(); } - public static CbsClientImpl create(CloudHttpClient httpClient, InetSocketAddress cbsAddress, String serviceName) { + public static CbsClientImpl create(RxHttpClient httpClient, InetSocketAddress cbsAddress, String serviceName) { return new CbsClientImpl(httpClient, constructUrl(cbsAddress, serviceName)); } @@ -57,6 +59,14 @@ public class CbsClientImpl implements CbsClient { @Override public @NotNull Mono get(RequestDiagnosticContext diagnosticContext) { - return Mono.defer(() -> httpClient.get(fetchUrl, diagnosticContext, JsonObject.class)); + return Mono.defer(() -> { + final ImmutableHttpRequest request = ImmutableHttpRequest.builder() + .method(HttpMethod.GET) + .url(fetchUrl) + .diagnosticContext(diagnosticContext) + .build(); + return httpClient.call(request) + .map(resp -> resp.bodyAsJson(JsonObject.class)); + }); } } diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java index 89daebc8..3d528c33 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java @@ -23,7 +23,10 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.net.InetSocketAddress; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.ServiceLookupException; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; import reactor.core.publisher.Mono; @@ -36,9 +39,9 @@ public class CbsLookup { private static final String CONSUL_JSON_SERVICE_ADDRESS = "ServiceAddress"; private static final String CONSUL_JSON_SERVICE_PORT = "ServicePort"; - private final CloudHttpClient httpClient; + private final RxHttpClient httpClient; - public CbsLookup(CloudHttpClient httpClient) { + public CbsLookup(RxHttpClient httpClient) { this.httpClient = httpClient; } @@ -54,7 +57,13 @@ public class CbsLookup { } private Mono fetchHttpData(String consulUrl) { - return httpClient.get(consulUrl, JsonArray.class); + return httpClient.call( + ImmutableHttpRequest.builder() + .method(HttpMethod.GET) + .url(consulUrl) + .build()) + .doOnNext(HttpResponse::throwIfUnsuccessful) + .map(resp -> resp.bodyAsJson(JsonArray.class)); } private Mono firstService(JsonArray services) { -- cgit 1.2.3-korg