Common/IPC/
ProxyCallToSideCar.rs

1//! # ProxyCallToSideCar Effect
2//!
3//! Defines the `ActionEffect` for proxying a generic RPC call to a sidecar
4//! process.
5
6use std::sync::Arc;
7
8use serde_json::Value;
9
10use super::IPCProvider::IPCProvider;
11use crate::{Effect::ActionEffect::ActionEffect, Error::CommonError::CommonError};
12
13/// Creates an effect that proxies an RPC call to a specified target sidecar.
14///
15/// This is a powerful utility for scenarios where the host application
16/// (`Mountain`) needs to act as a router, forwarding a request it received from
17/// one process to another without needing to understand the request's content.
18/// The entire call payload is encapsulated within the `CallData` object.
19///
20/// # Parameters
21///
22/// * `TargetSideCarIdentifier`: The unique ID of the sidecar to which the call
23///   should be proxied.
24/// * `CallData`: A JSON `Value` expected to be an object containing `{"Method":
25///   "...", "Parameters": ...}`.
26///
27/// # Returns
28///
29/// An `ActionEffect` that resolves with the JSON `Value` returned by the
30/// target sidecar.
31pub fn ProxyCallToSideCar(
32	TargetSideCarIdentifier:String,
33
34	CallData:Value,
35) -> ActionEffect<Arc<dyn IPCProvider>, CommonError, Value> {
36	ActionEffect::New(Arc::new(move |Provider:Arc<dyn IPCProvider>| {
37		let TargetIdentifierClone = TargetSideCarIdentifier.clone();
38
39		let CallDataClone = CallData.clone();
40
41		Box::pin(async move {
42			let MethodString = CallDataClone
43				.get("Method")
44				.and_then(Value::as_str)
45				.ok_or_else(|| {
46					CommonError::InvalidArgument {
47						ArgumentName:"CallData.Method".to_string(),
48
49						Reason:"Expected a 'Method' string field in CallData for proxying.".to_string(),
50					}
51				})?
52				.to_string();
53
54			let ParametersValue = CallDataClone.get("Parameters").cloned().unwrap_or(Value::Null);
55
56			// Using a default timeout here; a real implementation might make this
57			// configurable by extracting it from the CallData payload.
58			let DefaultTimeoutMilliseconds = 30000;
59
60			Provider
61				.SendRequestToSideCar(TargetIdentifierClone, MethodString, ParametersValue, DefaultTimeoutMilliseconds)
62				.await
63		})
64	}))
65}