Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename getResult API #33

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions src/main/java/io/github/cadenceoss/iwf/core/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.cadenceoss.iwf.gen.api.ApiClient;
import io.github.cadenceoss.iwf.gen.api.DefaultApi;

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

public class Client {
Expand Down Expand Up @@ -53,13 +54,24 @@ public String StartWorkflow(
return workflowStartResponse.getWorkflowRunId();
}

public <T> T GetSingleWorkflowStateOutputWithLongWait(
/**
* For most cases, a workflow only has one result(one completion state)
* Use this API to retrieve the output of the state
* @param valueClass the type class of the output
* @param workflowId the workflowId
* @param workflowRunId optional runId
* @return
* @param <T> type of the output
*/
public <T> T GetSimpleWorkflowResultWithLongWait(
Class<T> valueClass,
final String workflowId) {
final String workflowId,
final String workflowRunId) {
WorkflowGetResponse workflowGetResponse = defaultApi.apiV1WorkflowGetWithLongWaitPost(
new WorkflowGetRequest()
.needsResults(true)
.workflowId(workflowId)
.workflowRunId(workflowRunId)
);

String checkErrorMessage = "this workflow should have exactly one state output";
Expand All @@ -73,6 +85,33 @@ public <T> T GetSingleWorkflowStateOutputWithLongWait(
return objectEncoder.decode(output.getCompletedStateOutput(), valueClass);
}

public <T> T GetSimpleWorkflowResultWithLongWait(
Class<T> valueClass,
final String workflowId) {
return GetSimpleWorkflowResultWithLongWait(valueClass, workflowId, "");
}

/**
* In some cases, a workflow may have more than one completion states
* @param workflowId
* @param workflowRunId
* @return a list of the state output for completion states. User code will figure how to use ObjectEncoder to decode the output
*/
public List<StateCompletionOutput> GetComplexWorkflowResultWithLongWait(
final String workflowId, final String workflowRunId) {
WorkflowGetResponse workflowGetResponse = defaultApi.apiV1WorkflowGetWithLongWaitPost(
new WorkflowGetRequest()
.needsResults(true)
.workflowId(workflowId)
.workflowRunId(workflowRunId)
);

return workflowGetResponse.getResults();
}

public List<StateCompletionOutput> GetComplexWorkflowResultWithLongWait(final String workflowId) {
return GetComplexWorkflowResultWithLongWait(workflowId, "");
}
public void SignalWorkflow(
final Class<? extends Workflow> workflowClass,
final String workflowId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void testBasicWorkflow() throws InterruptedException {
final Integer input = Integer.valueOf(0);
client.StartWorkflow(BasicWorkflow.class, BasicWorkflowS1.StateId, input, wfId, startOptions);
// wait for workflow to finish
final Integer output = client.GetSingleWorkflowStateOutputWithLongWait(Integer.class, wfId);
final Integer output = client.GetSimpleWorkflowResultWithLongWait(Integer.class, wfId);
Assertions.assertEquals(input + 2, output);
}

Expand All @@ -46,7 +46,7 @@ public void testBasicSignalWorkflow() throws InterruptedException {
BasicSignalWorkflow.class, BasicSignalWorkflowState1.STATE_ID, input, wfId, startOptions);
client.SignalWorkflow(
BasicSignalWorkflow.class, wfId, runId, BasicSignalWorkflowState1.SIGNAL_NAME, Integer.valueOf(2));
final Integer output = client.GetSingleWorkflowStateOutputWithLongWait(Integer.class, wfId);
final Integer output = client.GetSimpleWorkflowResultWithLongWait(Integer.class, wfId);
Assertions.assertEquals(3, output);
}
}