From b7c95c461e414426c405bf64125048f861b0f11b Mon Sep 17 00:00:00 2001 From: Miguel Prieto Date: Fri, 4 Oct 2024 10:25:23 -0400 Subject: [PATCH] - Cleanup + refactoring: allow ConductorClient builder extension - Changes in orkes client module --- .../client/http/ConductorClient.java | 128 +++++++----------- .../io/orkes/conductor/client/ApiClient.java | 33 +++-- .../conductor/client/util/ClientTestUtil.java | 6 +- 3 files changed, 73 insertions(+), 94 deletions(-) diff --git a/conductor-clients/java/conductor-java-sdk/conductor-client/src/main/java/com/netflix/conductor/client/http/ConductorClient.java b/conductor-clients/java/conductor-java-sdk/conductor-client/src/main/java/com/netflix/conductor/client/http/ConductorClient.java index 6a0af40f2..9cf7a7d21 100644 --- a/conductor-clients/java/conductor-java-sdk/conductor-client/src/main/java/com/netflix/conductor/client/http/ConductorClient.java +++ b/conductor-clients/java/conductor-java-sdk/conductor-client/src/main/java/com/netflix/conductor/client/http/ConductorClient.java @@ -74,38 +74,38 @@ public class ConductorClient { private final KeyManager[] keyManagers; private final List headerSuppliers; - public static Builder builder() { - return new Builder(); + public static Builder builder() { + return new Builder<>(); } @SneakyThrows - protected ConductorClient(Builder builder) { + protected ConductorClient(Builder builder) { builder.validateAndAssignDefaults(); final OkHttpClient.Builder okHttpBuilder = builder.okHttpClientBuilder; this.objectMapper = builder.objectMapperSupplier.get(); - this.basePath = builder.basePath(); - this.verifyingSsl = builder.verifyingSsl(); - this.sslCaCert = builder.sslCaCert(); - this.keyManagers = builder.keyManagers(); + this.basePath = builder.basePath; + this.verifyingSsl = builder.verifyingSsl; + this.sslCaCert = builder.sslCaCert; + this.keyManagers = builder.keyManagers; this.headerSuppliers = builder.headerSupplier(); - if (builder.connectTimeout() > -1) { - okHttpBuilder.connectTimeout(builder.connectTimeout(), TimeUnit.MILLISECONDS); + if (builder.connectTimeout > -1) { + okHttpBuilder.connectTimeout(builder.connectTimeout, TimeUnit.MILLISECONDS); } - if (builder.readTimeout() > -1) { - okHttpBuilder.readTimeout(builder.readTimeout(), TimeUnit.MILLISECONDS); + if (builder.readTimeout > -1) { + okHttpBuilder.readTimeout(builder.readTimeout, TimeUnit.MILLISECONDS); } - if (builder.writeTimeout() > -1) { - okHttpBuilder.writeTimeout(builder.writeTimeout(), TimeUnit.MILLISECONDS); + if (builder.writeTimeout > -1) { + okHttpBuilder.writeTimeout(builder.writeTimeout, TimeUnit.MILLISECONDS); } - if (builder.getProxy() != null) { - okHttpBuilder.proxy(builder.getProxy()); + if (builder.proxy != null) { + okHttpBuilder.proxy(builder.proxy); } - ConnectionPoolConfig connectionPoolConfig = builder.getConnectionPoolConfig(); + ConnectionPoolConfig connectionPoolConfig = builder.connectionPoolConfig; if (connectionPoolConfig != null) { okHttpBuilder.connectionPool(new ConnectionPool( connectionPoolConfig.getMaxIdleConnections(), @@ -125,11 +125,11 @@ protected ConductorClient(Builder builder) { } public ConductorClient() { - this(new Builder()); + this(new Builder<>()); } public ConductorClient(String basePath) { - this(new Builder().basePath(basePath)); + this(new Builder<>().basePath(basePath)); } public String getBasePath() { @@ -433,7 +433,7 @@ private ConductorClientResponse execute(Call call, Type returnType) { } } - public static class Builder { + public static class Builder> { private final OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder(); private String basePath = "http://localhost:8080/api"; private boolean verifyingSsl = true; @@ -447,85 +447,54 @@ public static class Builder { private Supplier objectMapperSupplier = () -> new ObjectMapperProvider().getObjectMapper(); private final List headerSuppliers = new ArrayList<>(); - public String basePath() { - return basePath; + protected T self() { + //noinspection unchecked + return (T) this; } - public Builder basePath(String basePath) { + public T basePath(String basePath) { this.basePath = basePath; - return this; - } - - public boolean verifyingSsl() { - return verifyingSsl; + return self(); } - public Builder verifyingSsl(boolean verifyingSsl) { + public T verifyingSsl(boolean verifyingSsl) { this.verifyingSsl = verifyingSsl; - return this; - } - - public InputStream sslCaCert() { - return sslCaCert; + return self(); } - public Builder sslCaCert(InputStream sslCaCert) { + public T sslCaCert(InputStream sslCaCert) { this.sslCaCert = sslCaCert; - return this; + return self(); } - public KeyManager[] keyManagers() { - return keyManagers; - } - - public Builder keyManagers(KeyManager[] keyManagers) { + public T keyManagers(KeyManager[] keyManagers) { this.keyManagers = keyManagers; - return this; - } - - public long connectTimeout() { - return connectTimeout; + return self(); } - public Builder connectTimeout(long connectTimeout) { + public T connectTimeout(long connectTimeout) { this.connectTimeout = connectTimeout; - return this; + return self(); } - public long readTimeout() { - return readTimeout; - } - - public Builder readTimeout(long readTimeout) { + public T readTimeout(long readTimeout) { this.readTimeout = readTimeout; - return this; + return self(); } - public long writeTimeout() { - return writeTimeout; - } - - public Builder writeTimeout(long writeTimeout) { + public T writeTimeout(long writeTimeout) { this.writeTimeout = writeTimeout; - return this; + return self(); } - public Builder proxy(Proxy proxy) { + public T proxy(Proxy proxy) { this.proxy = proxy; - return this; + return self(); } - public ConnectionPoolConfig getConnectionPoolConfig() { - return this.connectionPoolConfig; - } - - public Builder connectionPoolConfig(ConnectionPoolConfig config) { + public T connectionPoolConfig(ConnectionPoolConfig config) { this.connectionPoolConfig = config; - return this; - } - - Proxy getProxy() { - return proxy; + return self(); } /** @@ -534,9 +503,9 @@ Proxy getProxy() { * @param configurer * @return */ - public Builder configureOkHttp(Consumer configurer) { + public T configureOkHttp(Consumer configurer) { configurer.accept(this.okHttpClientBuilder); - return this; + return self(); } /** @@ -545,17 +514,17 @@ public Builder configureOkHttp(Consumer configurer) { * @param objectMapperSupplier * @return */ - public Builder objectMapperSupplier(Supplier objectMapperSupplier) { + public T objectMapperSupplier(Supplier objectMapperSupplier) { this.objectMapperSupplier = objectMapperSupplier; - return this; + return self(); } - public Builder addHeaderSupplier(HeaderSupplier headerSupplier) { + public T addHeaderSupplier(HeaderSupplier headerSupplier) { this.headerSuppliers.add(headerSupplier); - return this; + return self(); } - public List headerSupplier() { + protected List headerSupplier() { return headerSuppliers; } @@ -563,7 +532,7 @@ public ConductorClient build() { return new ConductorClient(this); } - void validateAndAssignDefaults() { + protected void validateAndAssignDefaults() { if (StringUtils.isBlank(basePath)) { throw new IllegalArgumentException("basePath cannot be blank"); } @@ -571,7 +540,6 @@ void validateAndAssignDefaults() { if (basePath.endsWith("/")) { basePath = basePath.substring(0, basePath.length() - 1); } - } } } diff --git a/conductor-clients/java/conductor-java-sdk/orkes-client/src/main/java/io/orkes/conductor/client/ApiClient.java b/conductor-clients/java/conductor-java-sdk/orkes-client/src/main/java/io/orkes/conductor/client/ApiClient.java index 015db0254..c7b8d449d 100644 --- a/conductor-clients/java/conductor-java-sdk/orkes-client/src/main/java/io/orkes/conductor/client/ApiClient.java +++ b/conductor-clients/java/conductor-java-sdk/orkes-client/src/main/java/io/orkes/conductor/client/ApiClient.java @@ -17,7 +17,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.function.Consumer; import java.util.stream.Collectors; import org.jetbrains.annotations.NotNull; @@ -33,7 +32,6 @@ import okhttp3.Call; import okhttp3.Callback; -import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -44,22 +42,18 @@ public final class ApiClient extends ConductorClient { public ApiClient(String rootUri, String keyId, String secret) { - super(ConductorClient.builder() + this(ApiClient.builder() .basePath(rootUri) - .addHeaderSupplier(new OrkesAuthentication(keyId, secret))); - } - - public ApiClient(String rootUri, String keyId, String secret, Consumer configurer) { - super(ConductorClient.builder() - .basePath(rootUri) - .configureOkHttp(configurer) - .addHeaderSupplier(new OrkesAuthentication(keyId, secret))); + .credentials(keyId, secret)); } public ApiClient(String rootUri) { super(rootUri); } + private ApiClient(ApiClientBuilder builder) { + super(builder); + } public Call buildCall( String path, @@ -149,4 +143,21 @@ public ApiResponse execute(Call call, Type returnType) { } } + public static ApiClientBuilder builder() { + return new ApiClientBuilder(); + } + + public static class ApiClientBuilder extends Builder { + + public ApiClientBuilder credentials(String key, String secret) { + this.addHeaderSupplier(new OrkesAuthentication(key, secret)); + return this; + } + + @Override + public ApiClient build() { + return new ApiClient(this); + } + } + } diff --git a/conductor-clients/java/conductor-java-sdk/tests/src/test/java/io/orkes/conductor/client/util/ClientTestUtil.java b/conductor-clients/java/conductor-java-sdk/tests/src/test/java/io/orkes/conductor/client/util/ClientTestUtil.java index bcd735932..3eed57749 100644 --- a/conductor-clients/java/conductor-java-sdk/tests/src/test/java/io/orkes/conductor/client/util/ClientTestUtil.java +++ b/conductor-clients/java/conductor-java-sdk/tests/src/test/java/io/orkes/conductor/client/util/ClientTestUtil.java @@ -16,8 +16,8 @@ import com.netflix.conductor.client.http.ConductorClient; +import io.orkes.conductor.client.ApiClient; import io.orkes.conductor.client.OrkesClients; -import io.orkes.conductor.client.http.OrkesAuthentication; public class ClientTestUtil { private static final String ENV_ROOT_URI = "CONDUCTOR_SERVER_URL"; @@ -41,9 +41,9 @@ public static ConductorClient getClient() { String keySecret = getEnv(ENV_SECRET); Assertions.assertNotNull(keySecret, ENV_SECRET + " env not set"); - return ConductorClient.builder() + return ApiClient.builder() .basePath(basePath) - .addHeaderSupplier(new OrkesAuthentication(keyId, keySecret)) + .credentials(keyId, keySecret) .readTimeout(30_000) .connectTimeout(30_000) .writeTimeout(30_000)