Skip to content

Commit fd6c7b9

Browse files
authored
[Fix 887] Refactor openapi implementation (#888)
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent f8f488c commit fd6c7b9

File tree

2 files changed

+41
-56
lines changed

2 files changed

+41
-56
lines changed

impl/openapi/src/main/java/io/serverlessworkflow/impl/executors/openapi/OpenAPIExecutor.java

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,24 @@
1515
*/
1616
package io.serverlessworkflow.impl.executors.openapi;
1717

18-
import static io.serverlessworkflow.impl.executors.http.HttpExecutor.getTargetSupplier;
19-
2018
import io.serverlessworkflow.api.types.CallHTTP;
2119
import io.serverlessworkflow.api.types.CallOpenAPI;
20+
import io.serverlessworkflow.api.types.OpenAPIArguments;
2221
import io.serverlessworkflow.api.types.TaskBase;
23-
import io.serverlessworkflow.api.types.Workflow;
2422
import io.serverlessworkflow.impl.TaskContext;
25-
import io.serverlessworkflow.impl.WorkflowApplication;
2623
import io.serverlessworkflow.impl.WorkflowContext;
2724
import io.serverlessworkflow.impl.WorkflowDefinition;
28-
import io.serverlessworkflow.impl.WorkflowException;
2925
import io.serverlessworkflow.impl.WorkflowModel;
3026
import io.serverlessworkflow.impl.executors.CallableTask;
3127
import io.serverlessworkflow.impl.executors.http.HttpExecutor;
32-
import io.serverlessworkflow.impl.executors.http.TargetSupplier;
33-
import io.serverlessworkflow.impl.resources.ResourceLoader;
28+
import java.util.Iterator;
3429
import java.util.concurrent.CompletableFuture;
3530
import java.util.stream.Collectors;
3631

3732
public class OpenAPIExecutor implements CallableTask<CallOpenAPI> {
3833

39-
private CallOpenAPI task;
40-
private Workflow workflow;
41-
private WorkflowDefinition definition;
42-
private WorkflowApplication application;
43-
private TargetSupplier targetSupplier;
44-
private ResourceLoader resourceLoader;
4534
private OperationDefinitionSupplier operationDefinitionSupplier;
35+
private OpenAPIArguments with;
4636

4737
@Override
4838
public boolean accept(Class<? extends TaskBase> clazz) {
@@ -55,43 +45,39 @@ public CompletableFuture<WorkflowModel> apply(
5545

5646
OperationDefinition operation =
5747
operationDefinitionSupplier.get(workflowContext, taskContext, input);
48+
HttpCallAdapter httpCallAdapter =
49+
getHttpCallAdapter(operation, workflowContext, taskContext, input);
5850

59-
return CompletableFuture.supplyAsync(
60-
() -> {
61-
HttpCallAdapter httpCallAdapter =
62-
getHttpCallAdapter(operation, workflowContext, taskContext, input);
63-
64-
WorkflowException workflowException = null;
65-
66-
for (var server : operation.getServers()) {
67-
CallHTTP callHTTP = httpCallAdapter.server(server).build();
68-
HttpExecutor executor = new HttpExecutor();
69-
executor.init(callHTTP, definition);
51+
Iterator<String> iter = operation.getServers().iterator();
52+
if (!iter.hasNext()) {
53+
throw new IllegalArgumentException(
54+
"List of servers is empty for operation " + operation.getOperation());
55+
}
56+
CompletableFuture<WorkflowModel> future =
57+
executeServer(iter.next(), httpCallAdapter, workflowContext, taskContext, input);
58+
while (iter.hasNext()) {
59+
future.exceptionallyCompose(
60+
i -> executeServer(iter.next(), httpCallAdapter, workflowContext, taskContext, input));
61+
}
62+
return future;
63+
}
7064

71-
try {
72-
return executor.apply(workflowContext, taskContext, input).get();
73-
} catch (WorkflowException e) {
74-
workflowException = e;
75-
} catch (Exception e) {
76-
throw new RuntimeException(e);
77-
}
78-
}
79-
throw workflowException; // if we there, we failed all servers and ex is not null
80-
},
81-
workflowContext.definition().application().executorService());
65+
private CompletableFuture<WorkflowModel> executeServer(
66+
String server,
67+
HttpCallAdapter callAdapter,
68+
WorkflowContext workflowContext,
69+
TaskContext taskContext,
70+
WorkflowModel input) {
71+
CallHTTP callHTTP = callAdapter.server(server).build();
72+
HttpExecutor executor = new HttpExecutor();
73+
executor.init(callHTTP, workflowContext.definition());
74+
return executor.apply(workflowContext, taskContext, input);
8275
}
8376

8477
@Override
8578
public void init(CallOpenAPI task, WorkflowDefinition definition) {
86-
this.task = task;
87-
this.definition = definition;
88-
this.workflow = definition.workflow();
89-
this.application = definition.application();
90-
this.resourceLoader = definition.resourceLoader();
91-
this.operationDefinitionSupplier = new OperationDefinitionSupplier(application, task);
92-
this.targetSupplier =
93-
getTargetSupplier(
94-
task.getWith().getDocument().getEndpoint(), application.expressionFactory());
79+
with = task.getWith();
80+
operationDefinitionSupplier = new OperationDefinitionSupplier(definition.application(), with);
9581
}
9682

9783
private HttpCallAdapter getHttpCallAdapter(
@@ -102,11 +88,11 @@ private HttpCallAdapter getHttpCallAdapter(
10288
OperationPathResolver pathResolver =
10389
new OperationPathResolver(
10490
operation.getPath(),
105-
application,
106-
task.getWith().getParameters().getAdditionalProperties());
91+
workflowContext.definition().application(),
92+
with.getParameters().getAdditionalProperties());
10793

10894
return new HttpCallAdapter()
109-
.auth(task.getWith().getAuthentication())
95+
.auth(with.getAuthentication())
11096
.body(operation.getBody())
11197
.contentType(operation.getContentType())
11298
.headers(
@@ -118,8 +104,8 @@ private HttpCallAdapter getHttpCallAdapter(
118104
operation.getParameters().stream()
119105
.filter(p -> "query".equals(p.getIn()))
120106
.collect(Collectors.toUnmodifiableSet()))
121-
.redirect(task.getWith().isRedirect())
107+
.redirect(with.isRedirect())
122108
.target(pathResolver.resolve(workflowContext, taskContext, input))
123-
.workflowParams(task.getWith().getParameters().getAdditionalProperties());
109+
.workflowParams(with.getParameters().getAdditionalProperties());
124110
}
125111
}

impl/openapi/src/main/java/io/serverlessworkflow/impl/executors/openapi/OperationDefinitionSupplier.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import static io.serverlessworkflow.impl.executors.http.HttpExecutor.getTargetSupplier;
1919

20-
import io.serverlessworkflow.api.types.CallOpenAPI;
20+
import io.serverlessworkflow.api.types.OpenAPIArguments;
2121
import io.serverlessworkflow.impl.TaskContext;
2222
import io.serverlessworkflow.impl.WorkflowApplication;
2323
import io.serverlessworkflow.impl.WorkflowContext;
@@ -28,20 +28,19 @@
2828
class OperationDefinitionSupplier {
2929

3030
private final WorkflowApplication application;
31-
private final CallOpenAPI task;
31+
private final OpenAPIArguments with;
3232

33-
OperationDefinitionSupplier(WorkflowApplication application, CallOpenAPI task) {
34-
this.task = task;
33+
OperationDefinitionSupplier(WorkflowApplication application, OpenAPIArguments with) {
34+
this.with = with;
3535
this.application = application;
3636
}
3737

3838
OperationDefinition get(
3939
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
4040
TargetSupplier targetSupplier =
41-
getTargetSupplier(
42-
task.getWith().getDocument().getEndpoint(), application.expressionFactory());
41+
getTargetSupplier(with.getDocument().getEndpoint(), application.expressionFactory());
4342

44-
String operationId = task.getWith().getOperationId();
43+
String operationId = with.getOperationId();
4544
WebTarget webTarget = targetSupplier.apply(workflowContext, taskContext, input);
4645
OpenAPIProcessor processor = new OpenAPIProcessor(operationId, webTarget.getUri());
4746
return processor.parse();

0 commit comments

Comments
 (0)