Skip to main content

conductor/client/
event_client.rs

1// Copyright {{.Year}} Conductor OSS
2// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3
4use crate::error::Result;
5use crate::http::{ApiClient, ApiPath};
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9/// Client for event operations (queue configurations).
10#[derive(Clone)]
11pub struct EventClient {
12    api: ApiClient,
13}
14
15/// Queue configuration for event handling.
16#[derive(Debug, Clone, Default, Serialize, Deserialize)]
17#[serde(rename_all = "camelCase")]
18pub struct QueueConfiguration {
19    /// Queue name (e.g., topic name for Kafka).
20    pub queue_name: String,
21
22    /// Queue type (e.g., "kafka", "sqs", "amqp").
23    pub queue_type: String,
24
25    /// Queue-specific configuration.
26    #[serde(default)]
27    pub configuration: serde_json::Value,
28}
29
30impl QueueConfiguration {
31    /// Create a new queue configuration.
32    pub fn new(queue_type: impl Into<String>, queue_name: impl Into<String>) -> Self {
33        Self {
34            queue_name: queue_name.into(),
35            queue_type: queue_type.into(),
36            configuration: serde_json::Value::Null,
37        }
38    }
39
40    /// Create a Kafka queue configuration.
41    pub fn kafka(topic: impl Into<String>) -> Self {
42        Self::new("kafka", topic)
43    }
44
45    /// Create an SQS queue configuration.
46    pub fn sqs(queue_url: impl Into<String>) -> Self {
47        Self::new("sqs", queue_url)
48    }
49
50    /// Create an AMQP queue configuration.
51    pub fn amqp(queue_name: impl Into<String>) -> Self {
52        Self::new("amqp_queue", queue_name)
53    }
54
55    /// Set the configuration.
56    #[must_use]
57    pub fn with_configuration(mut self, config: serde_json::Value) -> Self {
58        self.configuration = config;
59        self
60    }
61}
62
63impl EventClient {
64    /// Create a new event client.
65    #[must_use]
66    pub fn new(api: ApiClient) -> Self {
67        Self { api }
68    }
69
70    /// Delete a queue configuration.
71    ///
72    /// # Errors
73    ///
74    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
75    pub async fn delete_queue_configuration(
76        &self,
77        queue_config: &QueueConfiguration,
78    ) -> Result<()> {
79        let path = format!(
80            "/event/queue/config/{}/{}",
81            queue_config.queue_type, queue_config.queue_name
82        );
83        self.api
84            .delete_no_content(ApiPath::templated(
85                &path,
86                "/event/queue/config/{queueType}/{queueName}",
87            ))
88            .await
89    }
90
91    /// Get a Kafka queue configuration by topic.
92    ///
93    /// # Errors
94    ///
95    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
96    pub async fn get_kafka_queue_configuration(
97        &self,
98        queue_topic: &str,
99    ) -> Result<QueueConfiguration> {
100        self.get_queue_configuration("kafka", queue_topic).await
101    }
102
103    /// Get a queue configuration by type and name.
104    ///
105    /// # Errors
106    ///
107    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
108    pub async fn get_queue_configuration(
109        &self,
110        queue_type: &str,
111        queue_name: &str,
112    ) -> Result<QueueConfiguration> {
113        let path = format!("/event/queue/config/{queue_type}/{queue_name}");
114        self.api
115            .get(ApiPath::templated(
116                &path,
117                "/event/queue/config/{queueType}/{queueName}",
118            ))
119            .await
120    }
121
122    /// Create or update a queue configuration.
123    ///
124    /// # Errors
125    ///
126    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
127    pub async fn put_queue_configuration(&self, queue_config: &QueueConfiguration) -> Result<()> {
128        let path = format!(
129            "/event/queue/config/{}/{}",
130            queue_config.queue_type, queue_config.queue_name
131        );
132        self.api
133            .put_no_response(
134                ApiPath::templated(&path, "/event/queue/config/{queueType}/{queueName}"),
135                &queue_config.configuration,
136            )
137            .await
138    }
139
140    /// Get all queue configurations
141    ///
142    /// Unlike `get_queue_configuration`, the server's "get all" endpoint
143    /// (`GET /event/queue/config`) returns a `{queueIdentifier: value}` object
144    /// rather than a list of `QueueConfiguration`. The `HashMap<String, String>`
145    /// here matches the server signature exactly: Orkes declares
146    /// `Map<String, String> getQueueNames()` in `EventResource`.
147    ///
148    /// That method returns a hardcoded `Map.of()` -- queue/broker integrations
149    /// moved to the Integrations API and the sibling `putQueueConfig` is
150    /// `@Deprecated(forRemoval = true)` -- so in practice this is always empty on
151    /// Orkes. Plain OSS Conductor has no queue-config routes at all
152    /// (`rest/.../EventResource.java` maps only the event-handler endpoints), so
153    /// it answers 404.
154    ///
155    /// # Errors
156    ///
157    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
158    pub async fn get_all_queue_configurations(&self) -> Result<HashMap<String, String>> {
159        self.api.get("/event/queue/config").await
160    }
161
162    /// Get event handlers for a specific event.
163    ///
164    /// # Errors
165    ///
166    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
167    pub async fn get_event_handlers(
168        &self,
169        event: &str,
170        active_only: bool,
171    ) -> Result<Vec<serde_json::Value>> {
172        let path = format!(
173            "/event/{}?activeOnly={}",
174            urlencoding::encode(event),
175            active_only
176        );
177        self.api
178            .get(ApiPath::templated(&path, "/event/{event}"))
179            .await
180    }
181
182    /// Get all event handlers.
183    ///
184    /// # Errors
185    ///
186    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
187    pub async fn get_all_event_handlers(&self) -> Result<Vec<serde_json::Value>> {
188        self.api.get("/event").await
189    }
190
191    /// Register an event handler.
192    ///
193    /// # Errors
194    ///
195    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
196    pub async fn register_event_handler(&self, event_handler: &serde_json::Value) -> Result<()> {
197        self.api.post_no_response("/event", event_handler).await
198    }
199
200    /// Update an event handler.
201    ///
202    /// # Errors
203    ///
204    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
205    pub async fn update_event_handler(&self, event_handler: &serde_json::Value) -> Result<()> {
206        self.api.put_no_response("/event", event_handler).await
207    }
208
209    /// Remove an event handler.
210    ///
211    /// # Errors
212    ///
213    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
214    pub async fn remove_event_handler(&self, name: &str) -> Result<()> {
215        let path = format!("/event/{name}");
216        self.api
217            .delete_no_content(ApiPath::templated(&path, "/event/{event}"))
218            .await
219    }
220}
221
222#[cfg(test)]
223mod tests {
224    use super::*;
225    use crate::Configuration;
226
227    #[test]
228    fn test_event_client_creation() {
229        let config = Configuration::new("http://localhost:8080/api");
230        let api = ApiClient::new(config).unwrap();
231        let _client = EventClient::new(api);
232    }
233
234    #[test]
235    fn test_queue_configuration() {
236        let kafka_config = QueueConfiguration::kafka("my-topic");
237        assert_eq!(kafka_config.queue_type, "kafka");
238        assert_eq!(kafka_config.queue_name, "my-topic");
239
240        let sqs_config =
241            QueueConfiguration::sqs("https://sqs.us-east-1.amazonaws.com/123/my-queue");
242        assert_eq!(sqs_config.queue_type, "sqs");
243    }
244}