Skip to content

Commit

Permalink
Merge pull request #276 from conductor-oss/fix/orkes-client-fixes-ref…
Browse files Browse the repository at this point in the history
…actoring

[Java Client v4] ConductorClient refactoring + Orkes client module cleanup
  • Loading branch information
jmigueprieto authored Oct 7, 2024
2 parents 3b6a9c0 + b7c95c4 commit 0366a58
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 193 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
@@ -1 +1 @@
version=4.0.0-alpha-SNAPSHOT
version=4.0.0-alpha1-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,44 @@
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;

import com.netflix.conductor.client.exception.ConductorClientException;
import com.netflix.conductor.client.http.ConductorClient;
import com.netflix.conductor.client.http.Param;

import io.orkes.conductor.client.http.ApiCallback;
import io.orkes.conductor.client.http.ApiException;
import io.orkes.conductor.client.http.ApiResponse;
import io.orkes.conductor.client.http.OrkesAuthentication;
import io.orkes.conductor.client.http.Pair;

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

/**
* This class exists to maintain backward compatibility and facilitate the migration for
* users of orkes-conductor-client v2.
*/
@Deprecated
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);
}

@Deprecated
private ApiClient(ApiClientBuilder builder) {
super(builder);
}

public Call buildCall(
String path,
String method,
Expand Down Expand Up @@ -91,7 +84,6 @@ private List<Param> toParamList(List<Pair> pairList) {
* @param call An instance of the Call object
* @param callback ApiCallback&lt;T&gt;
*/
@Deprecated
public <T> void executeAsync(Call call, ApiCallback<T> callback) {
executeAsync(call, null, callback);
}
Expand All @@ -105,15 +97,14 @@ public <T> void executeAsync(Call call, ApiCallback<T> callback) {
* @param callback ApiCallback
*/
@SuppressWarnings("unchecked")
@Deprecated
public <T> void executeAsync(Call call, final Type returnType, final ApiCallback<T> callback) {
call.enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
public void onResponse(@NotNull Call call, @NotNull Response response) {
T result;
try {
result = (T) handleResponse(response, returnType);
} catch (ApiException e) {
} catch (ConductorClientException e) {
callback.onFailure(e, response.code(), response.headers().toMultimap());
return;
}
Expand All @@ -123,13 +114,12 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO

@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
callback.onFailure(new ApiException(e), 0, null);
callback.onFailure(new ConductorClientException(e), 0, null);
}
});
}

@Deprecated
public <T> ApiResponse<T> execute(Call call) throws ApiException {
public <T> ApiResponse<T> execute(Call call) throws ConductorClientException {
return execute(call, null);
}

Expand All @@ -141,16 +131,32 @@ public <T> ApiResponse<T> execute(Call call) throws ApiException {
* @param call Call
* @return ApiResponse object containing response status, headers and data, which is a Java
* object deserialized from response body and would be null when returnType is null.
* @throws ApiException If fail to execute the call
* @throws ConductorClientException If fail to execute the call
*/
@Deprecated
public <T> ApiResponse<T> execute(Call call, Type returnType) throws ApiException {
public <T> ApiResponse<T> execute(Call call, Type returnType) {
try {
Response response = call.execute();
T data = handleResponse(response, returnType);
return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
} catch (IOException e) {
throw new ApiException(e);
throw new ConductorClientException(e);
}
}

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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import java.util.List;
import java.util.Map;

@Deprecated
import com.netflix.conductor.client.exception.ConductorClientException;

/**
* Callback for asynchronous API call.
*
Expand All @@ -29,7 +30,7 @@ public interface ApiCallback<T> {
* @param statusCode Status code of the response if available, otherwise it would be 0
* @param responseHeaders Headers of the response if available, otherwise it would be null
*/
void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders);
void onFailure(ConductorClientException e, int statusCode, Map<String, List<String>> responseHeaders);

/**
* This is called when the API call succeeded.
Expand Down
Loading

0 comments on commit 0366a58

Please sign in to comment.