drasi_reaction_http/
lib.rs1#![allow(unexpected_cfgs)]
2pub mod config;
45pub mod descriptor;
46pub mod http;
47
48pub use config::{CallSpec, HttpReactionConfig, QueryConfig};
49pub use http::HttpReaction;
50
51use std::collections::HashMap;
52
53pub struct HttpReactionBuilder {
69 id: String,
70 queries: Vec<String>,
71 base_url: String,
72 token: Option<String>,
73 timeout_ms: u64,
74 routes: HashMap<String, QueryConfig>,
75 priority_queue_capacity: Option<usize>,
76 auto_start: bool,
77}
78
79impl HttpReactionBuilder {
80 pub fn new(id: impl Into<String>) -> Self {
82 Self {
83 id: id.into(),
84 queries: Vec::new(),
85 base_url: "http://localhost".to_string(),
86 token: None,
87 timeout_ms: 5000,
88 routes: HashMap::new(),
89 priority_queue_capacity: None,
90 auto_start: true,
91 }
92 }
93
94 pub fn with_queries(mut self, queries: Vec<String>) -> Self {
96 self.queries = queries;
97 self
98 }
99
100 pub fn with_query(mut self, query_id: impl Into<String>) -> Self {
102 self.queries.push(query_id.into());
103 self
104 }
105
106 pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
108 self.base_url = base_url.into();
109 self
110 }
111
112 pub fn with_token(mut self, token: impl Into<String>) -> Self {
114 self.token = Some(token.into());
115 self
116 }
117
118 pub fn with_timeout_ms(mut self, timeout_ms: u64) -> Self {
120 self.timeout_ms = timeout_ms;
121 self
122 }
123
124 pub fn with_route(mut self, query_id: impl Into<String>, config: QueryConfig) -> Self {
126 self.routes.insert(query_id.into(), config);
127 self
128 }
129
130 pub fn with_priority_queue_capacity(mut self, capacity: usize) -> Self {
132 self.priority_queue_capacity = Some(capacity);
133 self
134 }
135
136 pub fn with_auto_start(mut self, auto_start: bool) -> Self {
138 self.auto_start = auto_start;
139 self
140 }
141
142 pub fn with_config(mut self, config: HttpReactionConfig) -> Self {
144 self.base_url = config.base_url;
145 self.token = config.token;
146 self.timeout_ms = config.timeout_ms;
147 self.routes = config.routes;
148 self
149 }
150
151 pub fn build(self) -> anyhow::Result<HttpReaction> {
153 let config = HttpReactionConfig {
154 base_url: self.base_url,
155 token: self.token,
156 timeout_ms: self.timeout_ms,
157 routes: self.routes,
158 };
159
160 Ok(HttpReaction::from_builder(
161 self.id,
162 self.queries,
163 config,
164 self.priority_queue_capacity,
165 self.auto_start,
166 ))
167 }
168}
169
170#[cfg(test)]
171mod tests {
172 use super::*;
173 use drasi_lib::Reaction;
174
175 #[test]
176 fn test_http_builder_defaults() {
177 let reaction = HttpReactionBuilder::new("test-reaction").build().unwrap();
178 assert_eq!(reaction.id(), "test-reaction");
179 let props = reaction.properties();
180 assert_eq!(
181 props.get("baseUrl"),
182 Some(&serde_json::Value::String("http://localhost".to_string()))
183 );
184 assert_eq!(
185 props.get("timeoutMs"),
186 Some(&serde_json::Value::Number(5000.into()))
187 );
188 }
189
190 #[test]
191 fn test_http_builder_custom_values() {
192 let reaction = HttpReaction::builder("test-reaction")
193 .with_base_url("http://api.example.com") .with_token("secret-token")
195 .with_timeout_ms(10000)
196 .with_queries(vec!["query1".to_string()])
197 .build()
198 .unwrap();
199
200 assert_eq!(reaction.id(), "test-reaction");
201 let props = reaction.properties();
202 assert_eq!(
203 props.get("baseUrl"),
204 Some(&serde_json::Value::String(
205 "http://api.example.com".to_string() ))
207 );
208 assert_eq!(
209 props.get("timeoutMs"),
210 Some(&serde_json::Value::Number(10000.into()))
211 );
212 assert_eq!(reaction.query_ids(), vec!["query1".to_string()]);
213 }
214
215 #[test]
216 fn test_http_builder_with_query() {
217 let reaction = HttpReaction::builder("test-reaction")
218 .with_query("query1")
219 .with_query("query2")
220 .build()
221 .unwrap();
222
223 assert_eq!(reaction.query_ids(), vec!["query1", "query2"]);
224 }
225
226 #[test]
227 fn test_http_new_constructor() {
228 let config = HttpReactionConfig {
229 base_url: "http://test.example.com".to_string(), token: Some("test-token".to_string()),
231 timeout_ms: 3000,
232 routes: Default::default(),
233 };
234
235 let reaction = HttpReaction::new("test-reaction", vec!["query1".to_string()], config);
236
237 assert_eq!(reaction.id(), "test-reaction");
238 assert_eq!(reaction.query_ids(), vec!["query1".to_string()]);
239 }
240}
241
242#[cfg(feature = "dynamic-plugin")]
246drasi_plugin_sdk::export_plugin!(
247 plugin_id = "http-reaction",
248 core_version = env!("CARGO_PKG_VERSION"),
249 lib_version = env!("CARGO_PKG_VERSION"),
250 plugin_version = env!("CARGO_PKG_VERSION"),
251 source_descriptors = [],
252 reaction_descriptors = [descriptor::HttpReactionDescriptor],
253 bootstrap_descriptors = [],
254);