Skip to content

Commit 8b84bd2

Browse files
committed
Add HTTP method shortcuts and output expression support in DSL
Signed-off-by: Matheus André <matheusandr2@gmail.com>
1 parent 046217a commit 8b84bd2

4 files changed

Lines changed: 375 additions & 4 deletions

File tree

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/CallHttpTaskBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,16 @@ protected CallHttpTaskBuilder() {
3131
public CallHttpTaskBuilder self() {
3232
return this;
3333
}
34+
35+
/**
36+
* Sets the output expression for this task (equivalent to {@code output.as} in YAML).
37+
*
38+
* <p>Uses JSONPath expression syntax (e.g., {@code $.field} to select a field from the result).
39+
*
40+
* @param expr JSONPath expression to extract the desired output value from the task result
41+
* @return this builder for chaining
42+
*/
43+
public CallHttpTaskBuilder outputAs(String expr) {
44+
return this.output(b -> b.as(expr));
45+
}
3446
}

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/dsl/BaseCallHttpSpec.java

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,26 @@ public interface BaseCallHttpSpec<SELF extends BaseCallHttpSpec<SELF>> {
3333
*/
3434
List<Consumer<CallHttpTaskFluent<?>>> steps();
3535

36-
default SELF GET() {
36+
default SELF get() {
3737
steps().add(c -> c.method("GET"));
3838
return self();
3939
}
4040

41-
default SELF POST() {
41+
@Deprecated
42+
default SELF GET() {
43+
return get();
44+
}
45+
46+
default SELF post() {
4247
steps().add(c -> c.method("POST"));
4348
return self();
4449
}
4550

51+
@Deprecated
52+
default SELF POST() {
53+
return post();
54+
}
55+
4656
default SELF acceptJSON() {
4757
return header("Accept", "application/json");
4858
}
@@ -122,6 +132,59 @@ default SELF query(String name, String value) {
122132
return self();
123133
}
124134

135+
default SELF put() {
136+
steps().add(c -> c.method("PUT"));
137+
return self();
138+
}
139+
140+
default SELF delete() {
141+
steps().add(c -> c.method("DELETE"));
142+
return self();
143+
}
144+
145+
default SELF patch() {
146+
steps().add(c -> c.method("PATCH"));
147+
return self();
148+
}
149+
150+
default SELF head() {
151+
steps().add(c -> c.method("HEAD"));
152+
return self();
153+
}
154+
155+
default SELF options() {
156+
steps().add(c -> c.method("OPTIONS"));
157+
return self();
158+
}
159+
160+
default SELF acceptXML() {
161+
return header("Accept", "application/xml");
162+
}
163+
164+
default SELF acceptForm() {
165+
return header("Accept", "application/x-www-form-urlencoded");
166+
}
167+
168+
default SELF acceptText() {
169+
return header("Accept", "text/plain");
170+
}
171+
172+
default SELF contentTypeJSON() {
173+
return header("Content-Type", "application/json");
174+
}
175+
176+
default SELF contentTypeXML() {
177+
return header("Content-Type", "application/xml");
178+
}
179+
180+
default SELF contentTypeForm() {
181+
return header("Content-Type", "application/x-www-form-urlencoded");
182+
}
183+
184+
default SELF contentTypeText() {
185+
return header("Content-Type", "text/plain");
186+
}
187+
125188
default SELF redirect(boolean redirect) {
126189
steps().add(c -> c.redirect(redirect));
127190
return self();

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/dsl/CallHttpSpec.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
public final class CallHttpSpec implements BaseCallHttpSpec<CallHttpSpec>, CallHttpConfigurer {
2626

27-
private final List<Consumer<CallHttpTaskFluent<?>>> steps = new ArrayList<>();
27+
private final List<Consumer<CallHttpTaskBuilder>> steps = new ArrayList<>();
2828

2929
public CallHttpSpec() {}
3030

@@ -34,12 +34,32 @@ public CallHttpSpec self() {
3434
}
3535

3636
@Override
37+
@SuppressWarnings("unchecked")
3738
public List<Consumer<CallHttpTaskFluent<?>>> steps() {
38-
return steps;
39+
// Safe cast because we control that all added consumers accept CallHttpTaskBuilder
40+
return (List) steps;
3941
}
4042

4143
@Override
4244
public void accept(CallHttpTaskBuilder builder) {
4345
BaseCallHttpSpec.super.accept(builder);
4446
}
47+
48+
@Override
49+
public void accept(CallHttpTaskFluent<?> fluent) {
50+
if (!(fluent instanceof CallHttpTaskBuilder builder)) {
51+
throw new IllegalArgumentException(
52+
"CallHttpSpec can only be applied to CallHttpTaskBuilder, got: "
53+
+ (fluent == null ? "null" : fluent.getClass().getName()));
54+
}
55+
accept(builder);
56+
}
57+
58+
@Override
59+
public CallHttpSpec andThen(Consumer<? super CallHttpTaskBuilder> after) {
60+
java.util.Objects.requireNonNull(after, "after");
61+
// Convert Consumer<? super CallHttpTaskBuilder> to Consumer<CallHttpTaskBuilder> for storage
62+
steps.add(builder -> after.accept(builder));
63+
return this;
64+
}
4565
}

0 commit comments

Comments
 (0)