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

Serverless Workflow reference implementation (skeleton) #450

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public class FeaturesTest {
"features/switch.yaml",
"features/try.yaml",
"features/listen.yaml",
"features/callFunction.yaml"
"features/callFunction.yaml",
"features/callCustomFunction.json"
})
public void testSpecFeaturesParsing(String workflowLocation) throws IOException {
Workflow workflow = readWorkflowFromClasspath(workflowLocation);
Expand Down
46 changes: 46 additions & 0 deletions api/src/test/resources/features/callCustomFunction.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"document": {
fjtirado marked this conversation as resolved.
Show resolved Hide resolved
"dsl": "1.0.0-alpha5",
"namespace": "test",
"name": "call-example",
"version": "0.1.0"
},
"schedule": {
"cron": "0 8 * * *"
},
"do": [
{
"getData": {
"call": "http",
"with": {
"method": "get",
"endpoint": "https://api.agify.io?name=meelad"
}
},
"output": {
"as": ".data.reading"
}
},
{
"filterData": {
"for": {
"in": ".data.reading",
"each": "reading"
},
"do": [
{
"log": {
"call": "https://raw.githubusercontent.com/serverlessworkflow/catalog/main/functions/log/1.0.0/function.yaml",
"with": {
"level": "information",
"format": "{TIMESTAMP} [{LEVEL}] ({CONTEXT}): {MESSAGE}",
"message": "Hello, world!",
"timestamp": true
}
}
}
]
}
}
]
}
73 changes: 73 additions & 0 deletions impl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-parent</artifactId>
<version>7.0.0-SNAPSHOT</version>
</parent>
<artifactId>serverlessworkflow-impl</artifactId>
<properties>
<version.org.glassfish.jersey>3.1.9</version.org.glassfish.jersey>
</properties>
<dependencies>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-api</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${version.org.glassfish.jersey}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${version.org.glassfish.jersey}</version>
</dependency>
<dependency>
fjtirado marked this conversation as resolved.
Show resolved Hide resolved
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.spotify.fmt</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<configuration>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<verbose>false</verbose>
<filesNamePattern>.*\.java</filesNamePattern>
<skip>false</skip>
<skipSortingImports>false</skipSortingImports>
<style>google</style>
</configuration>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.impl;

import com.fasterxml.jackson.databind.JsonNode;
import io.serverlessworkflow.api.types.TaskBase;

public abstract class AbstractTaskExecutor<T extends TaskBase> implements TaskExecutor<T> {

protected final T task;

protected AbstractTaskExecutor(T task) {
this.task = task;
}

@Override
public JsonNode apply(JsonNode node) {

// do input filtering
return internalExecute(node);
// do output filtering

}

protected abstract JsonNode internalExecute(JsonNode node);
}
76 changes: 76 additions & 0 deletions impl/src/main/java/io/serverlessworkflow/impl/HttpExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import io.serverlessworkflow.api.types.CallHTTP;
import io.serverlessworkflow.api.types.HTTPArguments;
import io.serverlessworkflow.api.types.WithHTTPHeaders;
import io.serverlessworkflow.api.types.WithHTTPQuery;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation.Builder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import java.util.Map;
import java.util.Map.Entry;

public class HttpExecutor extends AbstractTaskExecutor<CallHTTP> {

private static final Client client = ClientBuilder.newClient();

public HttpExecutor(CallHTTP task) {
super(task);
}

@Override
protected JsonNode internalExecute(JsonNode node) {

HTTPArguments httpArgs = task.getWith();
String uri =
httpArgs
.getEndpoint()
.getEndpointConfiguration()
.getUri()
.getLiteralEndpointURI()
.getLiteralUriTemplate();
WebTarget target = client.target(uri);
WithHTTPQuery query = httpArgs.getQuery();
if (query != null) {
for (Entry<String, Object> entry : query.getAdditionalProperties().entrySet()) {
target = target.queryParam(entry.getKey(), entry.getValue());
}
}
Builder request =
target
.resolveTemplates(
JsonUtils.mapper().convertValue(node, new TypeReference<Map<String, Object>>() {}))
.request(MediaType.APPLICATION_JSON);
WithHTTPHeaders headers = httpArgs.getHeaders();
if (headers != null) {
headers.getAdditionalProperties().forEach(request::header);
}
switch (httpArgs.getMethod().toLowerCase()) {
case "get":
default:
return request.get(JsonNode.class);
case "post":
return request.post(Entity.json(httpArgs.getBody()), JsonNode.class);
}
}
}
Loading
Loading