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(
72 &self,
73 method: &str,
74 payload: serde_json::Value,
75 ) -> Result<(mpsc::Receiver<ResponseChunk>, Arc<str>)> {
76 let (value, id) = self.execute_value_traced(method, payload).await?;
77 let (tx, rx) = mpsc::channel(4);
78 value_to_chunks(value, &tx).await;
79 Ok((rx, id))
80 }
81}
82
83pub async fn value_to_chunks(value: serde_json::Value, tx: &mpsc::Sender<ResponseChunk>) {
88 let status =
89 u16::try_from(value.get("status").and_then(|v| v.as_u64()).unwrap_or(200)).unwrap_or(200);
90
91 let headers: HashMap<String, String> = value
92 .get("headers")
93 .and_then(|v| v.as_object())
94 .map(|obj| {
95 obj.iter()
96 .filter_map(|(k, v)| Some((k.clone(), v.as_str()?.to_string())))
97 .collect()
98 })
99 .unwrap_or_default();
100
101 let _ = tx.send(ResponseChunk::Headers { status, headers }).await;
102
103 let body_str = value
104 .get("body")
105 .and_then(|v| v.as_str())
106 .unwrap_or("")
107 .to_string();
108 let body_encoding = value
109 .get("body_encoding")
110 .and_then(|v| v.as_str())
111 .map(str::to_string);
112
113 let body_bytes: Bytes = if body_encoding.as_deref() == Some("base64") {
114 use base64::Engine;
115 match base64::engine::general_purpose::STANDARD.decode(&body_str) {
116 Ok(b) => Bytes::from(b),
117 Err(e) => {
118 tracing::error!(error = ?e, "value_to_chunks: base64 decode failed");
119 Bytes::new()
120 }
121 }
122 } else {
123 Bytes::from(body_str.into_bytes())
124 };
125
126 if !body_bytes.is_empty() {
127 let _ = tx.send(ResponseChunk::Body(body_bytes)).await;
128 }
129
130 let _ = tx.send(ResponseChunk::End).await;
131}
132
133#[async_trait]
135impl<T: Executor + ?Sized> Executor for Arc<T> {
136 async fn execute(&self, payload: Bytes) -> Result<Bytes> {
137 (**self).execute(payload).await
138 }
139
140 async fn execute_method(&self, method: &str, payload: Bytes) -> Result<Bytes> {
141 (**self).execute_method(method, payload).await
142 }
143
144 async fn execute_value(
145 &self,
146 method: &str,
147 payload: serde_json::Value,
148 ) -> Result<serde_json::Value> {
149 (**self).execute_value(method, payload).await
150 }
151
152 async fn execute_value_traced(
153 &self,
154 method: &str,
155 payload: serde_json::Value,
156 ) -> Result<(serde_json::Value, Arc<str>)> {
157 (**self).execute_value_traced(method, payload).await
158 }
159
160 async fn execute_streamed(
161 &self,
162 method: &str,
163 payload: serde_json::Value,
164 ) -> Result<(mpsc::Receiver<ResponseChunk>, Arc<str>)> {
165 (**self).execute_streamed(method, payload).await
166 }
167}