Skip to content

Commit

Permalink
Refactoring of ApiClient and OrkesConductorClientAutoConfiguration sh…
Browse files Browse the repository at this point in the history
…ould provide an ApiClient Bean
  • Loading branch information
jmigueprieto committed Sep 9, 2024
1 parent 9b09d19 commit d29db33
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ protected <T> T handleResponse(Response response, Type returnType) {
}
}

private Request buildRequest(String method,
protected Request buildRequest(String method,
String path,
List<Param> pathParams,
List<Param> queryParams,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=3.0.0-alpha11
version=3.0.0-alpha12-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -58,62 +56,23 @@ public ApiClient(String rootUri) {
public Call buildCall(
String path,
String method,
List<Pair> pathParams,
List<Pair> queryParams,
List<Pair> collectionQueryParams,
Object body,
Map<String, String> 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<String, String> 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<Param> toParamList(List<Pair> pairList) {
List<Param> params = new ArrayList<>();
if (pairList != null) {
params.addAll(pairList.stream()
.map(it -> new Param(it.getName(), it.getValue()))
.collect(Collectors.toList()));
}

return params;
}

/**
Expand Down Expand Up @@ -169,10 +128,10 @@ public <T> ApiResponse<T> 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 <T> The return type corresponding to (same with) returnType
* @param call Call
* @param <T> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down

0 comments on commit d29db33

Please sign in to comment.