Skip to main content

swf_builders/services/task/
call.rs

1use super::*;
2
3// ============== CallFunctionTaskDefinitionBuilder ==============
4/// Builder for constructing a call task that invokes a function.
5pub struct CallFunctionTaskDefinitionBuilder {
6    call_function: CallFunctionDefinition,
7}
8
9impl CallFunctionTaskDefinitionBuilder {
10    pub fn new(function: &str) -> Self {
11        Self {
12            call_function: CallFunctionDefinition {
13                call: function.to_string(),
14                with: None,
15                common: TaskDefinitionFields::default(),
16            },
17        }
18    }
19
20    /// Adds a single named argument to the function call.
21    pub fn with(&mut self, name: &str, value: Value) -> &mut Self {
22        self.call_function
23            .with
24            .get_or_insert_with(HashMap::new)
25            .insert(name.to_string(), value);
26        self
27    }
28
29    /// Replaces all function call arguments with the provided map.
30    pub fn with_arguments(&mut self, arguments: HashMap<String, Value>) -> &mut Self {
31        self.call_function.with = Some(arguments);
32        self
33    }
34}
35
36impl_task_definition_builder_base!(CallFunctionTaskDefinitionBuilder, call_function, |v| {
37    TaskDefinition::Call(Box::new(CallTaskDefinition::Function(v)))
38});
39
40// ============== CallHTTPDefinitionBuilder ==============
41/// Builder for constructing an HTTP call task.
42pub struct CallHTTPDefinitionBuilder {
43    task: CallHTTPDefinition,
44}
45
46impl CallHTTPDefinitionBuilder {
47    pub fn new(method: &str, endpoint: &str) -> Self {
48        Self {
49            task: CallHTTPDefinition {
50                call: "http".to_string(),
51                with: HTTPArguments {
52                    method: method.to_string(),
53                    endpoint: OneOfEndpointDefinitionOrUri::Uri(endpoint.to_string()),
54                    headers: None,
55                    body: None,
56                    query: None,
57                    output: None,
58                    redirect: None,
59                },
60                common: TaskDefinitionFields::default(),
61            },
62        }
63    }
64
65    /// Adds an HTTP header to the request.
66    pub fn with_header(&mut self, name: &str, value: &str) -> &mut Self {
67        if let OneOfHeadersOrExpression::Map(headers) = self
68            .task
69            .with
70            .headers
71            .get_or_insert_with(|| OneOfHeadersOrExpression::Map(HashMap::new()))
72        {
73            headers.insert(name.to_string(), value.to_string());
74        }
75        self
76    }
77
78    /// Sets the HTTP request body.
79    pub fn with_body(&mut self, body: Value) -> &mut Self {
80        self.task.with.body = Some(body);
81        self
82    }
83
84    /// Adds a query parameter to the request.
85    pub fn with_query(&mut self, name: &str, value: &str) -> &mut Self {
86        if let OneOfQueryOrExpression::Map(query) = self
87            .task
88            .with
89            .query
90            .get_or_insert_with(|| OneOfQueryOrExpression::Map(HashMap::new()))
91        {
92            query.insert(name.to_string(), value.to_string());
93        }
94        self
95    }
96
97    /// Adds multiple query parameters from a map.
98    pub fn with_query_map(&mut self, map: HashMap<String, String>) -> &mut Self {
99        if let OneOfQueryOrExpression::Map(query) = self
100            .task
101            .with
102            .query
103            .get_or_insert_with(|| OneOfQueryOrExpression::Map(HashMap::new()))
104        {
105            query.extend(map);
106        }
107        self
108    }
109
110    /// Sets query parameters from a runtime expression (e.g., `"${ .queryParams }"`).
111    pub fn with_query_expression(&mut self, expr: &str) -> &mut Self {
112        self.task.with.query = Some(OneOfQueryOrExpression::Expression(expr.to_string()));
113        self
114    }
115
116    /// Sets the output format for the HTTP response.
117    pub fn with_output_format(&mut self, output: &str) -> &mut Self {
118        self.task.with.output = Some(output.to_string());
119        self
120    }
121
122    /// Sets whether HTTP redirects should be followed.
123    pub fn with_redirect(&mut self, redirect: bool) -> &mut Self {
124        self.task.with.redirect = Some(redirect);
125        self
126    }
127}
128
129impl_task_definition_builder_base!(CallHTTPDefinitionBuilder, task, |v| TaskDefinition::Call(
130    Box::new(CallTaskDefinition::HTTP(v))
131));
132
133// ============== CallGRPCDefinitionBuilder ==============
134/// Builder for constructing a gRPC call task.
135pub struct CallGRPCDefinitionBuilder {
136    task: CallGRPCDefinition,
137}
138
139impl CallGRPCDefinitionBuilder {
140    pub fn new(proto_url: &str, service_name: &str, method: &str) -> Self {
141        Self {
142            task: CallGRPCDefinition {
143                call: "grpc".to_string(),
144                with: GRPCArguments {
145                    proto: ExternalResourceDefinition {
146                        name: None,
147                        endpoint: OneOfEndpointDefinitionOrUri::Uri(proto_url.to_string()),
148                    },
149                    service: GRPCServiceDefinition {
150                        name: service_name.to_string(),
151                        ..Default::default()
152                    },
153                    method: method.to_string(),
154                    arguments: None,
155                    authentication: None,
156                },
157                common: TaskDefinitionFields::default(),
158            },
159        }
160    }
161
162    /// Sets the gRPC service host.
163    pub fn with_host(&mut self, host: &str) -> &mut Self {
164        self.task.with.service.host = host.to_string();
165        self
166    }
167
168    /// Sets the gRPC service port.
169    pub fn with_port(&mut self, port: u16) -> &mut Self {
170        self.task.with.service.port = Some(port);
171        self
172    }
173
174    /// Adds a single named argument to the gRPC call.
175    pub fn with_argument(&mut self, name: &str, value: Value) -> &mut Self {
176        self.task
177            .with
178            .arguments
179            .get_or_insert_with(HashMap::new)
180            .insert(name.to_string(), value);
181        self
182    }
183
184    /// References a named authentication policy for the gRPC call.
185    pub fn with_authentication_use(&mut self, auth_name: &str) -> &mut Self {
186        self.task.with.authentication = Some(ReferenceableAuthenticationPolicy::Reference(
187            AuthenticationPolicyReference {
188                use_: auth_name.to_string(),
189            },
190        ));
191        self
192    }
193}
194
195impl_task_definition_builder_base!(CallGRPCDefinitionBuilder, task, |v| TaskDefinition::Call(
196    Box::new(CallTaskDefinition::GRPC(Box::new(v)))
197));
198
199// ============== CallOpenAPIDefinitionBuilder ==============
200/// Builder for constructing an OpenAPI call task.
201pub struct CallOpenAPIDefinitionBuilder {
202    task: CallOpenAPIDefinition,
203}
204
205impl CallOpenAPIDefinitionBuilder {
206    pub fn new(document_url: &str, operation_id: &str) -> Self {
207        Self {
208            task: CallOpenAPIDefinition {
209                call: "openapi".to_string(),
210                with: OpenAPIArguments {
211                    document: ExternalResourceDefinition {
212                        name: None,
213                        endpoint: OneOfEndpointDefinitionOrUri::Uri(document_url.to_string()),
214                    },
215                    operation_id: operation_id.to_string(),
216                    parameters: None,
217                    authentication: None,
218                    output: None,
219                    redirect: None,
220                },
221                common: TaskDefinitionFields::default(),
222            },
223        }
224    }
225
226    /// Adds a single named parameter to the OpenAPI call.
227    pub fn with_parameter(&mut self, name: &str, value: Value) -> &mut Self {
228        self.task
229            .with
230            .parameters
231            .get_or_insert_with(HashMap::new)
232            .insert(name.to_string(), value);
233        self
234    }
235
236    /// Sets the output format for the OpenAPI response.
237    pub fn with_output_format(&mut self, output: &str) -> &mut Self {
238        self.task.with.output = Some(output.to_string());
239        self
240    }
241}
242
243impl_task_definition_builder_base!(
244    CallOpenAPIDefinitionBuilder,
245    task,
246    |v| TaskDefinition::Call(Box::new(CallTaskDefinition::OpenAPI(v)))
247);
248
249// ============== CallAsyncAPIDefinitionBuilder ==============
250/// Builder for constructing an AsyncAPI call task.
251pub struct CallAsyncAPIDefinitionBuilder {
252    task: CallAsyncAPIDefinition,
253}
254
255impl CallAsyncAPIDefinitionBuilder {
256    pub fn new(document_url: &str) -> Self {
257        Self {
258            task: CallAsyncAPIDefinition {
259                call: "asyncapi".to_string(),
260                with: AsyncApiArguments {
261                    document: ExternalResourceDefinition {
262                        name: None,
263                        endpoint: OneOfEndpointDefinitionOrUri::Uri(document_url.to_string()),
264                    },
265                    channel: None,
266                    operation: None,
267                    server: None,
268                    protocol: None,
269                    message: None,
270                    subscription: None,
271                    authentication: None,
272                },
273                common: TaskDefinitionFields::default(),
274            },
275        }
276    }
277
278    /// Sets the AsyncAPI channel name.
279    pub fn with_channel(&mut self, channel: &str) -> &mut Self {
280        self.task.with.channel = Some(channel.to_string());
281        self
282    }
283
284    /// Sets the AsyncAPI operation.
285    pub fn with_operation(&mut self, operation: &str) -> &mut Self {
286        self.task.with.operation = Some(operation.to_string());
287        self
288    }
289
290    /// Sets the AsyncAPI protocol (e.g., "kafka", "mqtt").
291    pub fn with_protocol(&mut self, protocol: &str) -> &mut Self {
292        self.task.with.protocol = Some(protocol.to_string());
293        self
294    }
295}
296
297impl_task_definition_builder_base!(CallAsyncAPIDefinitionBuilder, task, |v| {
298    TaskDefinition::Call(Box::new(CallTaskDefinition::AsyncAPI(v)))
299});
300
301// ============== CallA2ADefinitionBuilder ==============
302/// Builder for constructing an Agent-to-Agent (A2A) call task.
303pub struct CallA2ADefinitionBuilder {
304    task: CallA2ADefinition,
305}
306
307impl CallA2ADefinitionBuilder {
308    pub fn new(method: &str) -> Self {
309        Self {
310            task: CallA2ADefinition {
311                call: "a2a".to_string(),
312                with: A2AArguments {
313                    agent_card: None,
314                    server: None,
315                    method: method.to_string(),
316                    parameters: None,
317                },
318                common: TaskDefinitionFields::default(),
319            },
320        }
321    }
322
323    /// Sets the agent card URL for the A2A call.
324    pub fn with_agent_card(&mut self, url: &str) -> &mut Self {
325        self.task.with.agent_card = Some(ExternalResourceDefinition {
326            name: None,
327            endpoint: OneOfEndpointDefinitionOrUri::Uri(url.to_string()),
328        });
329        self
330    }
331
332    /// Adds a single named parameter to the A2A call.
333    pub fn with_parameter(&mut self, name: &str, value: Value) -> &mut Self {
334        if let Some(ref mut params) = self.task.with.parameters {
335            if let OneOfA2AParametersOrExpression::Map(ref mut map) = params {
336                map.insert(name.to_string(), value);
337            }
338        } else {
339            let mut map = HashMap::new();
340            map.insert(name.to_string(), value);
341            self.task.with.parameters = Some(OneOfA2AParametersOrExpression::Map(map));
342        }
343        self
344    }
345}
346
347impl_task_definition_builder_base!(CallA2ADefinitionBuilder, task, |v| TaskDefinition::Call(
348    Box::new(CallTaskDefinition::A2A(v))
349));