Skip to content

Commit

Permalink
Refactor to allow customization of conductor client library
Browse files Browse the repository at this point in the history
The HTTP facing element of conductor client is refactored to allow effective customization.
Also, supplying of http headers (as simple as an auth token when required) is taken care with
providing defaults and hence with no changes in behavior.
  • Loading branch information
Manu Nazareth committed Aug 26, 2024
1 parent 29f5a9a commit f3d1ddf
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 18 deletions.
138 changes: 129 additions & 9 deletions client/src/main/java/com/netflix/conductor/client/http/ClientBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,48 @@ public void setRootURI(String root) {
}

protected void delete(String url, Object... uriVariables) {
deleteWithUriVariables(null, url, uriVariables);
deleteWithUriVariables(null, null, url, uriVariables);
}

protected void delete(Map<String, Object> headers, String url, Object... uriVariables) {
deleteWithUriVariables(null, headers, url, uriVariables);
}

protected void deleteWithUriVariables(
Object[] queryParams, String url, Object... uriVariables) {
delete(queryParams, url, uriVariables, null);
delete(queryParams, Map.of(), url, uriVariables, null);
}

protected void deleteWithUriVariables(
Object[] queryParams, Map<String, Object> headers, String url, Object... uriVariables) {
delete(queryParams, headers, url, uriVariables, null);
}

protected BulkResponse deleteWithRequestBody(Object[] queryParams, String url, Object body) {
return delete(queryParams, url, null, body);
return deleteWithRequestBody(queryParams, Map.of(), url, body);
}

protected BulkResponse deleteWithRequestBody(
Object[] queryParams, Map<String, Object> headers, String url, Object body) {
return delete(queryParams, headers, url, null, body);
}

private BulkResponse delete(
Object[] queryParams, String url, Object[] uriVariables, Object body) {
return delete(queryParams, Map.of(), url, uriVariables, body);
}

private BulkResponse delete(
Object[] queryParams,
Map<String, Object> headers,
String url,
Object[] uriVariables,
Object body) {
URI uri = null;
BulkResponse response = null;
try {
uri = getURIBuilder(root + url, queryParams).build(uriVariables);
response = requestHandler.delete(uri, body);
response = requestHandler.delete(uri, headers, body);
} catch (UniformInterfaceException e) {
handleUniformInterfaceException(e, uri);
} catch (RuntimeException e) {
Expand All @@ -109,18 +132,32 @@ private BulkResponse delete(
}

protected void put(String url, Object[] queryParams, Object request, Object... uriVariables) {
put(url, queryParams, Map.of(), request, uriVariables);
}

protected void put(
String url,
Object[] queryParams,
Map<String, Object> headers,
Object request,
Object... uriVariables) {
URI uri = null;
try {
uri = getURIBuilder(root + url, queryParams).build(uriVariables);
requestHandler.getWebResourceBuilder(uri, request).put();
requestHandler.getWebResourceBuilder(uri, headers, request).put();
} catch (RuntimeException e) {
handleException(uri, e);
}
}

protected void postForEntityWithRequestOnly(String url, Object request) {
postForEntityWithRequestOnly(url, Map.of(), request);
}

protected void postForEntityWithRequestOnly(
String url, Map<String, Object> headers, Object request) {
Class<?> type = null;
postForEntity(url, request, null, type);
postForEntity(url, request, null, headers, type);
}

protected void postForEntityWithUriVariablesOnly(String url, Object... uriVariables) {
Expand All @@ -134,10 +171,37 @@ protected <T> T postForEntity(
Object[] queryParams,
Class<T> responseType,
Object... uriVariables) {
return postForEntity(url, request, queryParams, Map.of(), responseType, uriVariables);
}

protected <T> T postForEntity(
String url,
Object request,
Object[] queryParams,
Map<String, Object> headers,
Class<T> responseType,
Object... uriVariables) {
return postForEntity(
url,
request,
queryParams,
headers,
responseType,
builder -> builder.post(responseType),
uriVariables);
}

protected <T> T postForEntity(
String url,
Object request,
Object[] queryParams,
GenericType<T> responseType,
Object... uriVariables) {
return postForEntity(
url,
request,
queryParams,
Map.of(),
responseType,
builder -> builder.post(responseType),
uriVariables);
Expand All @@ -147,12 +211,14 @@ protected <T> T postForEntity(
String url,
Object request,
Object[] queryParams,
Map<String, Object> headers,
GenericType<T> responseType,
Object... uriVariables) {
return postForEntity(
url,
request,
queryParams,
headers,
responseType,
builder -> builder.post(responseType),
uriVariables);
Expand All @@ -165,10 +231,23 @@ private <T> T postForEntity(
Object responseType,
Function<Builder, T> postWithEntity,
Object... uriVariables) {
return postForEntity(
url, request, queryParams, Map.of(), responseType, postWithEntity, uriVariables);
}

private <T> T postForEntity(
String url,
Object request,
Object[] queryParams,
Map<String, Object> headers,
Object responseType,
Function<Builder, T> postWithEntity,
Object... uriVariables) {
URI uri = null;
try {
uri = getURIBuilder(root + url, queryParams).build(uriVariables);
Builder webResourceBuilder = requestHandler.getWebResourceBuilder(uri, request);
Builder webResourceBuilder =
requestHandler.getWebResourceBuilder(uri, headers, request);
if (responseType == null) {
webResourceBuilder.post();
return null;
Expand All @@ -185,7 +264,25 @@ private <T> T postForEntity(
protected <T> T getForEntity(
String url, Object[] queryParams, Class<T> responseType, Object... uriVariables) {
return getForEntity(
url, queryParams, response -> response.getEntity(responseType), uriVariables);
url,
queryParams,
Map.of(),
response -> response.getEntity(responseType),
uriVariables);
}

protected <T> T getForEntity(
String url,
Object[] queryParams,
Map<String, Object> headers,
Class<T> responseType,
Object... uriVariables) {
return getForEntity(
url,
queryParams,
headers,
response -> response.getEntity(responseType),
uriVariables);
}

protected <T> T getForEntity(
Expand All @@ -194,16 +291,39 @@ protected <T> T getForEntity(
url, queryParams, response -> response.getEntity(responseType), uriVariables);
}

protected <T> T getForEntity(
String url,
Object[] queryParams,
Map<String, Object> headers,
GenericType<T> responseType,
Object... uriVariables) {
return getForEntity(
url,
queryParams,
headers,
response -> response.getEntity(responseType),
uriVariables);
}

private <T> T getForEntity(
String url,
Object[] queryParams,
Function<ClientResponse, T> entityProvider,
Object... uriVariables) {
return getForEntity(url, queryParams, Map.of(), entityProvider, uriVariables);
}

private <T> T getForEntity(
String url,
Object[] queryParams,
Map<String, Object> headers,
Function<ClientResponse, T> entityProvider,
Object... uriVariables) {
URI uri = null;
ClientResponse clientResponse;
try {
uri = getURIBuilder(root + url, queryParams).build(uriVariables);
clientResponse = requestHandler.get(uri);
clientResponse = requestHandler.get(uri, headers);
if (clientResponse.getStatus() < 300) {
return entityProvider.apply(clientResponse);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package com.netflix.conductor.client.http;

import java.net.URI;
import java.util.Map;

import javax.ws.rs.core.MediaType;

Expand Down Expand Up @@ -57,29 +58,56 @@ public ClientRequestHandler(
}

public BulkResponse delete(URI uri, Object body) {
return delete(uri, Map.of(), body);
}

public BulkResponse delete(URI uri, Map<String, Object> headers, Object body) {
if (body != null) {
return client.resource(uri)
return getWebResourceBuilder(client.resource(uri), headers)
.type(MediaType.APPLICATION_JSON_TYPE)
.delete(BulkResponse.class, body);
} else {
client.resource(uri).delete();
getWebResourceBuilder(client.resource(uri), headers).delete();
}
return null;
}

public ClientResponse get(URI uri) {
return client.resource(uri)
return get(uri, Map.of());
}

public ClientResponse get(URI uri, Map<String, Object> headers) {
return getWebResourceBuilder(client.resource(uri), headers)
.accept(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN)
.get(ClientResponse.class);
}

public WebResource.Builder getWebResourceBuilder(URI URI, Object entity) {
return client.resource(URI)
public WebResource.Builder getWebResourceBuilder(URI uri, Object entity) {
return getWebResourceBuilder(uri, Map.of(), entity);
}

public WebResource.Builder getWebResourceBuilder(
URI uri, Map<String, Object> headers, Object entity) {
return getWebResourceBuilder(client.resource(uri), headers)
.type(MediaType.APPLICATION_JSON)
.entity(entity)
.accept(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON);
}

private WebResource.Builder getWebResourceBuilder(
WebResource resource, Map<String, Object> headers) {
WebResource.Builder builder = resource.getRequestBuilder();
if (headers == null || headers.isEmpty()) {
return builder;
}

for (Map.Entry<String, Object> entry : headers.entrySet()) {
builder = builder.header(entry.getKey(), entry.getValue());
}

return builder;
}

private boolean isNewerJacksonVersion() {
Version version = com.fasterxml.jackson.databind.cfg.PackageVersion.VERSION;
return version.getMajorVersion() == 2 && version.getMinorVersion() >= 12;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package com.netflix.conductor.client.http;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.Validate;

Expand Down Expand Up @@ -91,8 +92,12 @@ public MetadataClient(
* @param workflowDef the workflow definition
*/
public void registerWorkflowDef(WorkflowDef workflowDef) {
registerWorkflowDef(workflowDef, Map.of());
}

protected void registerWorkflowDef(WorkflowDef workflowDef, Map<String, Object> headers) {
Validate.notNull(workflowDef, "Workflow definition cannot be null");
postForEntityWithRequestOnly("metadata/workflow", workflowDef);
postForEntityWithRequestOnly("metadata/workflow", headers, workflowDef);
}

public void validateWorkflowDef(WorkflowDef workflowDef) {
Expand All @@ -118,10 +123,16 @@ public void updateWorkflowDefs(List<WorkflowDef> workflowDefs) {
* @return Workflow definition for the given workflow and version
*/
public WorkflowDef getWorkflowDef(String name, Integer version) {
return getWorkflowDef(name, version, Map.of());
}

protected WorkflowDef getWorkflowDef(
String name, Integer version, Map<String, Object> headers) {
Validate.notBlank(name, "name cannot be blank");
return getForEntity(
"metadata/workflow/{name}",
new Object[] {"version", version},
headers,
WorkflowDef.class,
name);
}
Expand Down
Loading

0 comments on commit f3d1ddf

Please sign in to comment.