Skip to main content

platform_module_remote/
binding.rs

1use crate::config::RemoteModuleConfig;
2use crate::event::{
3    RemoteEventHandler, RemoteEventHostActionRunner, validate_event_handler_name,
4    validate_event_name,
5};
6use crate::runtime::{RemoteRuntimeFunction, validate_function_name};
7use platform_core::{AppResult, EventHandlerRegistry};
8use platform_module::{
9    EventHandlerRegistrationContext, EventSurface, ModuleBinding, RuntimeSurface,
10};
11use platform_runtime::{FunctionDefinition, FunctionRegistry, RetryPolicy};
12use std::sync::Arc;
13use std::time::Duration;
14
15#[derive(Debug, Clone, Default)]
16pub struct RemoteBinding {
17    config: Option<RemoteModuleConfig>,
18    functions: Vec<FunctionDefinition>,
19    event_handlers: Vec<RemoteEventHandlerRegistration>,
20}
21
22#[derive(Debug, Clone)]
23struct RemoteEventHandlerRegistration {
24    name: String,
25    event_name: String,
26}
27
28impl RemoteBinding {
29    pub fn from_surfaces(
30        config: RemoteModuleConfig,
31        runtime: Option<&RuntimeSurface>,
32        events: Option<&EventSurface>,
33    ) -> AppResult<Self> {
34        let functions = runtime
35            .into_iter()
36            .flat_map(|surface| surface.functions.iter())
37            .map(|declaration| {
38                validate_function_name(&declaration.name)?;
39                Ok(FunctionDefinition {
40                    name: declaration.name.clone(),
41                    version: declaration.version,
42                    queue: declaration.queue.clone(),
43                    retry_policy: declaration
44                        .retry_policy
45                        .as_ref()
46                        .map(|policy| {
47                            RetryPolicy::fixed(
48                                policy.max_attempts,
49                                Duration::from_millis(policy.initial_delay_ms),
50                            )
51                        })
52                        .unwrap_or_default(),
53                    handler: Arc::new(RemoteRuntimeFunction::new(
54                        config.clone(),
55                        declaration.name.clone(),
56                    )?),
57                })
58            })
59            .collect::<AppResult<Vec<_>>>()?;
60
61        let event_handlers = events
62            .into_iter()
63            .flat_map(|surface| surface.handlers.iter())
64            .map(|declaration| {
65                validate_event_handler_name(&declaration.name)?;
66                validate_event_name(&declaration.event_name)?;
67                Ok(RemoteEventHandlerRegistration {
68                    name: declaration.name.clone(),
69                    event_name: declaration.event_name.clone(),
70                })
71            })
72            .collect::<AppResult<Vec<_>>>()?;
73
74        Ok(Self {
75            config: Some(config),
76            functions,
77            event_handlers,
78        })
79    }
80}
81
82impl ModuleBinding for RemoteBinding {
83    fn register_functions(&self, registry: &mut FunctionRegistry) {
84        for function in self.functions.iter().cloned() {
85            registry.register(function);
86        }
87    }
88
89    fn register_event_handlers(
90        &self,
91        registry: &mut EventHandlerRegistry,
92        context: &EventHandlerRegistrationContext,
93    ) {
94        let Some(config) = &self.config else {
95            return;
96        };
97        let allowed_function_names = self
98            .functions
99            .iter()
100            .map(|function| function.name.clone())
101            .collect::<Vec<_>>();
102
103        for declaration in &self.event_handlers {
104            let mut handler = RemoteEventHandler::new(
105                config.clone(),
106                declaration.name.clone(),
107                declaration.event_name.clone(),
108            )
109            .expect("remote event handler declaration was validated");
110
111            if let Some(runtime) = context.runtime() {
112                handler = handler.with_host_action_runner(RemoteEventHostActionRunner::new(
113                    runtime.runtime_client.clone(),
114                    runtime.function_registry.clone(),
115                    allowed_function_names.clone(),
116                ));
117            }
118
119            registry.register(std::sync::Arc::new(handler));
120        }
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127    use platform_module::{
128        EventHandlerDeclaration, EventSurface, RuntimeFunctionDeclaration,
129        RuntimeRetryPolicyDeclaration, RuntimeSurface,
130    };
131
132    #[test]
133    fn remote_binding_registers_declared_functions() {
134        let binding = RemoteBinding::from_surfaces(
135            RemoteModuleConfig::new("remote-crm", "http://127.0.0.1:4100/lenso/module/v1"),
136            Some(&RuntimeSurface {
137                functions: vec![RuntimeFunctionDeclaration {
138                    name: "remote_crm.sync_contact.v1".to_owned(),
139                    version: 1,
140                    queue: "remote-crm".to_owned(),
141                    input_schema: Some("remote_crm.sync_contact.v1".to_owned()),
142                    retry_policy: Some(RuntimeRetryPolicyDeclaration {
143                        max_attempts: 3,
144                        initial_delay_ms: 1000,
145                    }),
146                }],
147                schedules: vec![],
148            }),
149            None,
150        )
151        .expect("remote binding should build");
152
153        let mut registry = FunctionRegistry::default();
154        binding.register_functions(&mut registry);
155
156        let definition = registry
157            .get("remote_crm.sync_contact.v1")
158            .expect("remote function should register");
159        assert_eq!(definition.version, 1);
160        assert_eq!(definition.queue, "remote-crm");
161        assert_eq!(definition.retry_policy.max_attempts, 3);
162        assert_eq!(
163            definition.retry_policy.initial_delay,
164            Duration::from_millis(1000)
165        );
166    }
167
168    #[test]
169    fn remote_binding_rejects_invalid_function_name() {
170        let error = RemoteBinding::from_surfaces(
171            RemoteModuleConfig::new("remote-crm", "http://127.0.0.1:4100/lenso/module/v1"),
172            Some(&RuntimeSurface {
173                functions: vec![RuntimeFunctionDeclaration {
174                    name: "remote_crm/sync_contact.v1".to_owned(),
175                    version: 1,
176                    queue: "remote-crm".to_owned(),
177                    input_schema: None,
178                    retry_policy: None,
179                }],
180                schedules: vec![],
181            }),
182            None,
183        )
184        .expect_err("invalid function name should fail");
185
186        assert_eq!(error.code, platform_core::ErrorCode::Validation);
187    }
188
189    #[test]
190    fn remote_binding_registers_declared_event_handlers() {
191        let binding = RemoteBinding::from_surfaces(
192            RemoteModuleConfig::new("remote-crm", "http://127.0.0.1:4100/lenso/module/v1"),
193            None,
194            Some(&EventSurface {
195                handlers: vec![EventHandlerDeclaration {
196                    name: "sync_contact_on_user_registered".to_owned(),
197                    event_name: "identity.user_registered.v1".to_owned(),
198                }],
199            }),
200        )
201        .expect("remote binding should build");
202
203        let mut registry = EventHandlerRegistry::default();
204        binding.register_event_handlers(&mut registry, &EventHandlerRegistrationContext::empty());
205
206        assert_eq!(registry.handler_count("identity.user_registered.v1"), 1);
207    }
208
209    #[test]
210    fn remote_binding_rejects_invalid_event_handler_name() {
211        let error = RemoteBinding::from_surfaces(
212            RemoteModuleConfig::new("remote-crm", "http://127.0.0.1:4100/lenso/module/v1"),
213            None,
214            Some(&EventSurface {
215                handlers: vec![EventHandlerDeclaration {
216                    name: "sync/contact".to_owned(),
217                    event_name: "identity.user_registered.v1".to_owned(),
218                }],
219            }),
220        )
221        .expect_err("invalid event handler name should fail");
222
223        assert_eq!(error.code, platform_core::ErrorCode::Validation);
224    }
225}