Skip to content

Commit

Permalink
- Cleanup + refactoring: allow ConductorClient builder extension
Browse files Browse the repository at this point in the history
- Changes in orkes client module
  • Loading branch information
jmigueprieto committed Oct 4, 2024
1 parent 3a93794 commit b7c95c4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,38 @@ public class ConductorClient {
private final KeyManager[] keyManagers;
private final List<HeaderSupplier> 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(),
Expand All @@ -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() {
Expand Down Expand Up @@ -433,7 +433,7 @@ private <T> ConductorClientResponse<T> execute(Call call, Type returnType) {
}
}

public static class Builder {
public static class Builder<T extends Builder<T>> {
private final OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
private String basePath = "http://localhost:8080/api";
private boolean verifyingSsl = true;
Expand All @@ -447,85 +447,54 @@ public static class Builder {
private Supplier<ObjectMapper> objectMapperSupplier = () -> new ObjectMapperProvider().getObjectMapper();
private final List<HeaderSupplier> 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();
}

/**
Expand All @@ -534,9 +503,9 @@ Proxy getProxy() {
* @param configurer
* @return
*/
public Builder configureOkHttp(Consumer<OkHttpClient.Builder> configurer) {
public T configureOkHttp(Consumer<OkHttpClient.Builder> configurer) {
configurer.accept(this.okHttpClientBuilder);
return this;
return self();
}

/**
Expand All @@ -545,33 +514,32 @@ public Builder configureOkHttp(Consumer<OkHttpClient.Builder> configurer) {
* @param objectMapperSupplier
* @return
*/
public Builder objectMapperSupplier(Supplier<ObjectMapper> objectMapperSupplier) {
public T objectMapperSupplier(Supplier<ObjectMapper> 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> headerSupplier() {
protected List<HeaderSupplier> headerSupplier() {
return headerSuppliers;
}

public ConductorClient build() {
return new ConductorClient(this);
}

void validateAndAssignDefaults() {
protected void validateAndAssignDefaults() {
if (StringUtils.isBlank(basePath)) {
throw new IllegalArgumentException("basePath cannot be blank");
}

if (basePath.endsWith("/")) {
basePath = basePath.substring(0, basePath.length() - 1);
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +32,6 @@

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

Expand All @@ -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<OkHttpClient.Builder> 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,
Expand Down Expand Up @@ -149,4 +143,21 @@ public <T> ApiResponse<T> execute(Call call, Type returnType) {
}
}

public static ApiClientBuilder builder() {
return new ApiClientBuilder();
}

public static class ApiClientBuilder extends Builder<ApiClientBuilder> {

public ApiClientBuilder credentials(String key, String secret) {
this.addHeaderSupplier(new OrkesAuthentication(key, secret));
return this;
}

@Override
public ApiClient build() {
return new ApiClient(this);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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)
Expand Down

0 comments on commit b7c95c4

Please sign in to comment.