1use std::collections::HashMap;
6use std::sync::Arc;
7
8use anyhow::Result;
9use async_trait::async_trait;
10use bytes::Bytes;
11use tokio::sync::mpsc;
12
13use crate::response::ResponseChunk;
14
15#[async_trait]
19pub trait Executor: Send + Sync + 'static {
20 async fn execute(&self, payload: Bytes) -> Result<Bytes> {
23 self.execute_method("dispatch", payload).await
24 }
25
26 async fn execute_method(&self, method: &str, payload: Bytes) -> Result<Bytes>;
28
29 async fn execute_value(
35 &self,
36 method: &str,
37 payload: serde_json::Value,
38 ) -> Result<serde_json::Value> {
39 let bytes = serde_json::to_vec(&payload)?;
40 let result = self.execute_method(method, Bytes::from(bytes)).await?;
41 Ok(serde_json::from_slice(&result)?)
42 }
43
44 async fn execute_value_traced(
51 &self,
52 method: &str,
53 payload: serde_json::Value,
54 ) -> Result<(serde_json::Value, Arc<str>)> {
55 let value = self.execute_value(method, payload).await?;
56 Ok((value, Arc::from("")))
57 }
58
59 async fn execute_streamed(
74 &self,
75 method: &str,
76 payload: serde_json::Value,
77 ) -> Result<(mpsc::Receiver<ResponseChunk>, Arc<str>)> {
78 let (value, id) = self.execute_value_traced(method, payload).await?;
79 let (tx, rx) = mpsc::channel(4);
80 let _ = tx.send(ResponseChunk::Return(value)).await;
81 let _ = tx.send(ResponseChunk::End).await;
82 Ok((rx, id))
83 }
84}
85
86pub async fn value_to_chunks(value: serde_json::Value, tx: &mpsc::Sender<ResponseChunk>) {
91 let status =
92 u16::try_from(value.get("status").and_then(|v| v.as_u64()).unwrap_or(200)).unwrap_or(200);
93
94 let headers: HashMap<String, String> = value
95 .get("headers")
96 .and_then(|v| v.as_object())
97 .map(|obj| {
98 obj.iter()
99 .filter_map(|(k, v)| Some((k.clone(), v.as_str()?.to_string())))
100 .collect()
101 })
102 .unwrap_or_default();
103
104 let _ = tx.send(ResponseChunk::Headers { status, headers }).await;
105
106 let body_str = value
107 .get("body")
108 .and_then(|v| v.as_str())
109 .unwrap_or("")
110 .to_string();
111 let body_encoding = value
112 .get("body_encoding")
113 .and_then(|v| v.as_str())
114 .map(str::to_string);
115
116 let body_bytes: Bytes = if body_encoding.as_deref() == Some("base64") {
117 use base64::Engine;
118 match base64::engine::general_purpose::STANDARD.decode(&body_str) {
119 Ok(b) => Bytes::from(b),
120 Err(e) => {
121 tracing::error!(error = ?e, "value_to_chunks: base64 decode failed");
122 Bytes::new()
123 }
124 }
125 } else {
126 Bytes::from(body_str.into_bytes())
127 };
128
129 if !body_bytes.is_empty() {
130 let _ = tx.send(ResponseChunk::Body(body_bytes)).await;
131 }
132
133 let _ = tx.send(ResponseChunk::End).await;
134}
135
136#[async_trait]
138impl<T: Executor + ?Sized> Executor for Arc<T> {
139 async fn execute(&self, payload: Bytes) -> Result<Bytes> {
140 (**self).execute(payload).await
141 }
142
143 async fn execute_method(&self, method: &str, payload: Bytes) -> Result<Bytes> {
144 (**self).execute_method(method, payload).await
145 }
146
147 async fn execute_value(
148 &self,
149 method: &str,
150 payload: serde_json::Value,
151 ) -> Result<serde_json::Value> {
152 (**self).execute_value(method, payload).await
153 }
154
155 async fn execute_value_traced(
156 &self,
157 method: &str,
158 payload: serde_json::Value,
159 ) -> Result<(serde_json::Value, Arc<str>)> {
160 (**self).execute_value_traced(method, payload).await
161 }
162
163 async fn execute_streamed(
164 &self,
165 method: &str,
166 payload: serde_json::Value,
167 ) -> Result<(mpsc::Receiver<ResponseChunk>, Arc<str>)> {
168 (**self).execute_streamed(method, payload).await
169 }
170}