From d29db33cc0c2dd83ad300264a3e3f4d60468ba9b Mon Sep 17 00:00:00 2001 From: Miguel Prieto Date: Mon, 9 Sep 2024 12:17:35 -0400 Subject: [PATCH] Refactoring of ApiClient and OrkesConductorClientAutoConfiguration should provide an ApiClient Bean --- .../client/http/ConductorClient.java | 2 +- .../java/conductor-java-sdk/gradle.properties | 2 +- .../io/orkes/conductor/client/ApiClient.java | 73 ++++--------------- ...OrkesConductorClientAutoConfiguration.java | 31 +++++--- 4 files changed, 38 insertions(+), 70 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 5c7ec1a17..e977236d6 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 @@ -287,7 +287,7 @@ protected T handleResponse(Response response, Type returnType) { } } - private Request buildRequest(String method, + protected Request buildRequest(String method, String path, List pathParams, List queryParams, diff --git a/conductor-clients/java/conductor-java-sdk/gradle.properties b/conductor-clients/java/conductor-java-sdk/gradle.properties index f59c86b56..dfd648a67 100644 --- a/conductor-clients/java/conductor-java-sdk/gradle.properties +++ b/conductor-clients/java/conductor-java-sdk/gradle.properties @@ -1 +1 @@ -version=3.0.0-alpha11 +version=3.0.0-alpha12-SNAPSHOT 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 850d84464..cf4147769 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 @@ -13,15 +13,16 @@ package io.orkes.conductor.client; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; -import java.net.URLEncoder; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.jetbrains.annotations.NotNull; 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; @@ -31,10 +32,7 @@ import okhttp3.Call; import okhttp3.Callback; -import okhttp3.HttpUrl; -import okhttp3.MediaType; import okhttp3.Request; -import okhttp3.RequestBody; import okhttp3.Response; /** @@ -58,62 +56,23 @@ public ApiClient(String rootUri) { public Call buildCall( String path, String method, + List pathParams, List queryParams, - List collectionQueryParams, Object body, Map headers) { - HttpUrl.Builder urlBuilder = HttpUrl.parse(path).newBuilder(); - - if (queryParams != null) { - for (Pair param : queryParams) { - urlBuilder.addQueryParameter(param.getName(), param.getValue()); - } - } - - if (collectionQueryParams != null) { - for (Pair param : collectionQueryParams) { - urlBuilder.addQueryParameter(param.getName(), param.getValue()); - } - } - - RequestBody requestBody = null; - if (body != null) { - if (body instanceof String) { - requestBody = RequestBody.create((String) body, MediaType.parse("application/json; charset=utf-8")); - } else { - // Handle other body types (e.g., JSON objects) as needed - requestBody = RequestBody.create(body.toString(), MediaType.parse("application/json; charset=utf-8")); - } - } - - Request.Builder requestBuilder = new Request.Builder() - .url(urlBuilder.build()) - .method(method, requestBody); - - if (headers != null) { - for (Map.Entry header : headers.entrySet()) { - requestBuilder.addHeader(header.getKey(), header.getValue()); - } - } - - Request request = requestBuilder.build(); - + Request request = buildRequest(method, path, toParamList(pathParams), toParamList(queryParams), headers, body); return okHttpClient.newCall(request); } - /** - * Escape the given string to be used as URL query value. - * - * @param str String to be escaped - * @return Escaped string - */ - @Deprecated - public String escapeString(String str) { - try { - return URLEncoder.encode(str, "utf8").replaceAll("\\+", "%20"); - } catch (UnsupportedEncodingException e) { - return str; + private List toParamList(List pairList) { + List params = new ArrayList<>(); + if (pairList != null) { + params.addAll(pairList.stream() + .map(it -> new Param(it.getName(), it.getValue())) + .collect(Collectors.toList())); } + + return params; } /** @@ -169,10 +128,10 @@ public ApiResponse execute(Call call) throws ApiException { * Execute HTTP call and deserialize the HTTP response body into the given return type. * * @param returnType The return type used to deserialize HTTP response body - * @param The return type corresponding to (same with) returnType - * @param call Call + * @param The return type corresponding to (same with) returnType + * @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. + * object deserialized from response body and would be null when returnType is null. * @throws ApiException If fail to execute the call */ @Deprecated diff --git a/conductor-clients/java/conductor-java-sdk/orkes-spring/src/main/java/io/orkes/conductor/client/spring/OrkesConductorClientAutoConfiguration.java b/conductor-clients/java/conductor-java-sdk/orkes-spring/src/main/java/io/orkes/conductor/client/spring/OrkesConductorClientAutoConfiguration.java index 8b215b16c..dc544d021 100644 --- a/conductor-clients/java/conductor-java-sdk/orkes-spring/src/main/java/io/orkes/conductor/client/spring/OrkesConductorClientAutoConfiguration.java +++ b/conductor-clients/java/conductor-java-sdk/orkes-spring/src/main/java/io/orkes/conductor/client/spring/OrkesConductorClientAutoConfiguration.java @@ -17,13 +17,11 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; -import com.netflix.conductor.client.http.ConductorClient; - +import io.orkes.conductor.client.ApiClient; import io.orkes.conductor.client.AuthorizationClient; import io.orkes.conductor.client.OrkesClients; import io.orkes.conductor.client.SchedulerClient; import io.orkes.conductor.client.SecretClient; -import io.orkes.conductor.client.http.OrkesAuthentication; import io.orkes.conductor.client.http.OrkesEventClient; import io.orkes.conductor.client.http.OrkesMetadataClient; import io.orkes.conductor.client.http.OrkesTaskClient; @@ -35,25 +33,36 @@ @Slf4j public class OrkesConductorClientAutoConfiguration { + public static final String CONDUCTOR_SERVER_URL ="conductor.server.url"; + public static final String CONDUCTOR_SECURITY_CLIENT_KEY_ID ="conductor.security.client.key-id"; + public static final String CONDUCTOR_SECURITY_CLIENT_SECRET ="conductor.security.client.secret"; //TODO add more properties e.g.: ssl off, timeout settings, etc. and these should be client properties!!! - public static final String CONDUCTOR_SERVER_URL = "conductor.client.basepath"; + public static final String CONDUCTOR_CLIENT_BASE_PATH = "conductor.client.basepath"; public static final String CONDUCTOR_CLIENT_KEY_ID = "conductor.client.key-id"; public static final String CONDUCTOR_CLIENT_SECRET = "conductor.client.secret"; @Bean - public ConductorClient orkesConductorClient(Environment env) { - String basePath = env.getProperty(CONDUCTOR_SERVER_URL); + public ApiClient orkesConductorClient(Environment env) { + String basePath = env.getProperty(CONDUCTOR_CLIENT_BASE_PATH); + if (basePath == null) { + basePath = env.getProperty(CONDUCTOR_SERVER_URL); + } + String keyId = env.getProperty(CONDUCTOR_CLIENT_KEY_ID); + if (keyId == null) { + keyId = env.getProperty(CONDUCTOR_SECURITY_CLIENT_KEY_ID); + } + String secret = env.getProperty(CONDUCTOR_CLIENT_SECRET); + if (secret == null) { + secret = env.getProperty(CONDUCTOR_SECURITY_CLIENT_SECRET); + } - return ConductorClient.builder() - .basePath(basePath) - .addHeaderSupplier(new OrkesAuthentication(keyId, secret)) - .build(); + return new ApiClient(basePath, keyId, secret); } @Bean - public OrkesClients orkesClients(ConductorClient client) { + public OrkesClients orkesClients(ApiClient client) { return new OrkesClients(client); }