mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-22 16:48:30 +02:00
Combine grpc handlers, fix duplicate
This commit is contained in:
@@ -4,8 +4,8 @@ use std::path::PathBuf;
|
|||||||
use hyper::client::HttpConnector;
|
use hyper::client::HttpConnector;
|
||||||
use hyper::Client;
|
use hyper::Client;
|
||||||
use hyper_rustls::HttpsConnector;
|
use hyper_rustls::HttpsConnector;
|
||||||
use prost_reflect::DescriptorPool;
|
|
||||||
pub use prost_reflect::DynamicMessage;
|
pub use prost_reflect::DynamicMessage;
|
||||||
|
use prost_reflect::{DescriptorPool, MethodDescriptor, ServiceDescriptor};
|
||||||
use serde_json::Deserializer;
|
use serde_json::Deserializer;
|
||||||
use tokio_stream::wrappers::ReceiverStream;
|
use tokio_stream::wrappers::ReceiverStream;
|
||||||
use tokio_stream::StreamExt;
|
use tokio_stream::StreamExt;
|
||||||
@@ -25,14 +25,30 @@ pub struct GrpcConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GrpcConnection {
|
impl GrpcConnection {
|
||||||
|
pub fn service(&self, service: &str) -> Result<ServiceDescriptor, String> {
|
||||||
|
let service = self
|
||||||
|
.pool
|
||||||
|
.get_service_by_name(service)
|
||||||
|
.ok_or("Failed to find service")?;
|
||||||
|
Ok(service)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn method(&self, service: &str, method: &str) -> Result<MethodDescriptor, String> {
|
||||||
|
let service = self.service(service)?;
|
||||||
|
let method = service
|
||||||
|
.methods()
|
||||||
|
.find(|m| m.name() == method)
|
||||||
|
.ok_or("Failed to find method")?;
|
||||||
|
Ok(method)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn unary(
|
pub async fn unary(
|
||||||
&self,
|
&self,
|
||||||
service: &str,
|
service: &str,
|
||||||
method: &str,
|
method: &str,
|
||||||
message: &str,
|
message: &str,
|
||||||
) -> Result<DynamicMessage, String> {
|
) -> Result<DynamicMessage, String> {
|
||||||
let service = self.pool.get_service_by_name(service).unwrap();
|
let method = &self.method(&service, &method)?;
|
||||||
let method = &service.methods().find(|m| m.name() == method).unwrap();
|
|
||||||
let input_message = method.input();
|
let input_message = method.input();
|
||||||
|
|
||||||
let mut deserializer = Deserializer::from_str(message);
|
let mut deserializer = Deserializer::from_str(message);
|
||||||
@@ -60,9 +76,7 @@ impl GrpcConnection {
|
|||||||
method: &str,
|
method: &str,
|
||||||
stream: ReceiverStream<String>,
|
stream: ReceiverStream<String>,
|
||||||
) -> Result<Streaming<DynamicMessage>, String> {
|
) -> Result<Streaming<DynamicMessage>, String> {
|
||||||
let service = self.pool.get_service_by_name(service).unwrap();
|
let method = &self.method(&service, &method)?;
|
||||||
let method = &service.methods().find(|m| m.name() == method).unwrap();
|
|
||||||
|
|
||||||
let mut client = tonic::client::Grpc::with_origin(self.conn.clone(), self.uri.clone());
|
let mut client = tonic::client::Grpc::with_origin(self.conn.clone(), self.uri.clone());
|
||||||
|
|
||||||
let method2 = method.clone();
|
let method2 = method.clone();
|
||||||
@@ -92,8 +106,7 @@ impl GrpcConnection {
|
|||||||
method: &str,
|
method: &str,
|
||||||
stream: ReceiverStream<String>,
|
stream: ReceiverStream<String>,
|
||||||
) -> Result<DynamicMessage, String> {
|
) -> Result<DynamicMessage, String> {
|
||||||
let service = self.pool.get_service_by_name(service).unwrap();
|
let method = &self.method(&service, &method)?;
|
||||||
let method = &service.methods().find(|m| m.name() == method).unwrap();
|
|
||||||
let mut client = tonic::client::Grpc::with_origin(self.conn.clone(), self.uri.clone());
|
let mut client = tonic::client::Grpc::with_origin(self.conn.clone(), self.uri.clone());
|
||||||
|
|
||||||
let req = {
|
let req = {
|
||||||
@@ -126,8 +139,7 @@ impl GrpcConnection {
|
|||||||
method: &str,
|
method: &str,
|
||||||
message: &str,
|
message: &str,
|
||||||
) -> Result<Streaming<DynamicMessage>, String> {
|
) -> Result<Streaming<DynamicMessage>, String> {
|
||||||
let service = self.pool.get_service_by_name(service).unwrap();
|
let method = &self.method(&service, &method)?;
|
||||||
let method = &service.methods().find(|m| m.name() == method).unwrap();
|
|
||||||
let input_message = method.input();
|
let input_message = method.input();
|
||||||
|
|
||||||
let mut deserializer = Deserializer::from_str(message);
|
let mut deserializer = Deserializer::from_str(message);
|
||||||
|
|||||||
@@ -127,100 +127,11 @@ async fn cmd_grpc_reflect(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
async fn cmd_grpc_call_unary(
|
async fn cmd_grpc_go(
|
||||||
request_id: &str,
|
request_id: &str,
|
||||||
w: Window,
|
w: Window,
|
||||||
grpc_handle: State<'_, Mutex<GrpcHandle>>,
|
grpc_handle: State<'_, Mutex<GrpcHandle>>,
|
||||||
) -> Result<GrpcMessage, String> {
|
) -> Result<String, String> {
|
||||||
let req = get_grpc_request(&w, request_id)
|
|
||||||
.await
|
|
||||||
.map_err(|e| e.to_string())?;
|
|
||||||
let conn = {
|
|
||||||
let req = req.clone();
|
|
||||||
upsert_grpc_connection(
|
|
||||||
&w,
|
|
||||||
&GrpcConnection {
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|e| e.to_string())?
|
|
||||||
};
|
|
||||||
|
|
||||||
{
|
|
||||||
let req = req.clone();
|
|
||||||
let conn = conn.clone();
|
|
||||||
upsert_grpc_message(
|
|
||||||
&w,
|
|
||||||
&GrpcMessage {
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
connection_id: conn.id,
|
|
||||||
is_info: true,
|
|
||||||
message: format!("Initiating connection to {}", req.url),
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|e| e.to_string())?;
|
|
||||||
};
|
|
||||||
|
|
||||||
let uri = safe_uri(&req.url).map_err(|e| e.to_string())?;
|
|
||||||
let start = std::time::Instant::now();
|
|
||||||
let msg = match grpc_handle
|
|
||||||
.lock()
|
|
||||||
.await
|
|
||||||
.connect(
|
|
||||||
&req.clone().id,
|
|
||||||
uri,
|
|
||||||
req.proto_files
|
|
||||||
.0
|
|
||||||
.iter()
|
|
||||||
.map(|p| PathBuf::from_str(p).unwrap())
|
|
||||||
.collect(),
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
.unary(
|
|
||||||
&req.service.unwrap_or_default(),
|
|
||||||
&req.method.unwrap_or_default(),
|
|
||||||
&req.message,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(msg) => {
|
|
||||||
upsert_grpc_message(
|
|
||||||
&w,
|
|
||||||
&GrpcMessage {
|
|
||||||
message: serialize_message(&msg)?,
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
connection_id: conn.clone().id,
|
|
||||||
is_server: true,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
Err(e) => return Err(e.to_string()),
|
|
||||||
};
|
|
||||||
|
|
||||||
upsert_grpc_connection(
|
|
||||||
&w,
|
|
||||||
&GrpcConnection {
|
|
||||||
elapsed: start.elapsed().as_millis() as i64,
|
|
||||||
..conn
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|e| e.to_string())?;
|
|
||||||
|
|
||||||
msg.map_err(|e| e.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
async fn cmd_grpc_client_streaming(request_id: &str, w: Window) -> Result<GrpcConnection, String> {
|
|
||||||
let req = get_grpc_request(&w, request_id)
|
let req = get_grpc_request(&w, request_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
@@ -253,7 +164,7 @@ async fn cmd_grpc_client_streaming(request_id: &str, w: Window) -> Result<GrpcCo
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.expect("Failed to upsert message");
|
||||||
};
|
};
|
||||||
|
|
||||||
let (in_msg_tx, in_msg_rx) = tauri::async_runtime::channel::<String>(16);
|
let (in_msg_tx, in_msg_rx) = tauri::async_runtime::channel::<String>(16);
|
||||||
@@ -272,11 +183,30 @@ async fn cmd_grpc_client_streaming(request_id: &str, w: Window) -> Result<GrpcCo
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let start = std::time::Instant::now();
|
||||||
|
let connection = grpc_handle
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.connect(
|
||||||
|
&req.clone().id,
|
||||||
|
uri,
|
||||||
|
req.proto_files
|
||||||
|
.0
|
||||||
|
.iter()
|
||||||
|
.map(|p| PathBuf::from_str(p).unwrap())
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let method_desc = connection
|
||||||
|
.method(&service, &method)
|
||||||
|
.expect("Service not found");
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
enum IncomingMsg {
|
enum IncomingMsg {
|
||||||
Message(String),
|
Message(String),
|
||||||
Commit,
|
|
||||||
Cancel,
|
Cancel,
|
||||||
|
Commit,
|
||||||
}
|
}
|
||||||
|
|
||||||
let cb = {
|
let cb = {
|
||||||
@@ -320,6 +250,7 @@ async fn cmd_grpc_client_streaming(request_id: &str, w: Window) -> Result<GrpcCo
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
.map_err(|e| e.to_string())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -337,237 +268,76 @@ async fn cmd_grpc_client_streaming(request_id: &str, w: Window) -> Result<GrpcCo
|
|||||||
};
|
};
|
||||||
let event_handler = w.listen_global(format!("grpc_client_msg_{}", conn.id).as_str(), cb);
|
let event_handler = w.listen_global(format!("grpc_client_msg_{}", conn.id).as_str(), cb);
|
||||||
|
|
||||||
let start = std::time::Instant::now();
|
|
||||||
let grpc_listen = {
|
let grpc_listen = {
|
||||||
let w = w.clone();
|
let w = w.clone();
|
||||||
let conn = conn.clone();
|
let conn = conn.clone();
|
||||||
let req = req.clone();
|
let req = req.clone();
|
||||||
async move {
|
async move {
|
||||||
let grpc_handle = w.state::<Mutex<GrpcHandle>>();
|
let (maybe_stream, maybe_msg) = match (
|
||||||
let msg = grpc_handle
|
method_desc.is_client_streaming(),
|
||||||
.lock()
|
method_desc.is_server_streaming(),
|
||||||
.await
|
) {
|
||||||
.connect(
|
(true, true) => (
|
||||||
&req.clone().id,
|
Some(
|
||||||
uri,
|
connection
|
||||||
req.proto_files
|
.streaming(&service, &method, in_msg_stream)
|
||||||
.0
|
.await
|
||||||
.iter()
|
.unwrap(),
|
||||||
.map(|p| PathBuf::from_str(p).unwrap())
|
),
|
||||||
.collect(),
|
None,
|
||||||
|
),
|
||||||
|
(true, false) => (
|
||||||
|
None,
|
||||||
|
Some(
|
||||||
|
connection
|
||||||
|
.client_streaming(&service, &method, in_msg_stream)
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.to_string())
|
||||||
|
.unwrap(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(false, true) => (
|
||||||
|
Some(
|
||||||
|
connection
|
||||||
|
.server_streaming(&service, &method, &req.message)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
(false, false) => (
|
||||||
|
None,
|
||||||
|
Some(
|
||||||
|
connection
|
||||||
|
.unary(&service, &method, &req.message)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(msg) = maybe_msg {
|
||||||
|
let req = req.clone();
|
||||||
|
let conn = conn.clone();
|
||||||
|
upsert_grpc_message(
|
||||||
|
&w,
|
||||||
|
&GrpcMessage {
|
||||||
|
message: serialize_message(&msg).unwrap(),
|
||||||
|
workspace_id: req.workspace_id,
|
||||||
|
request_id: req.id,
|
||||||
|
connection_id: conn.id,
|
||||||
|
is_server: true,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.map_err(|e| e.to_string())
|
||||||
.client_streaming(&service, &method, in_msg_stream)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let message = serialize_message(&msg).unwrap();
|
|
||||||
upsert_grpc_message(
|
|
||||||
&w,
|
|
||||||
&GrpcMessage {
|
|
||||||
message,
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
connection_id: conn.id,
|
|
||||||
is_server: true,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
{
|
|
||||||
let conn = conn.clone();
|
|
||||||
let w = w.clone();
|
|
||||||
tauri::async_runtime::spawn(async move {
|
|
||||||
tokio::select! {
|
|
||||||
_ = grpc_listen => {
|
|
||||||
upsert_grpc_message(
|
|
||||||
&w,
|
|
||||||
&GrpcMessage {
|
|
||||||
message: "Connection completed".to_string(),
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
connection_id: conn.clone().id,
|
|
||||||
is_info: true,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await.unwrap();
|
|
||||||
upsert_grpc_connection(
|
|
||||||
&w,
|
|
||||||
&GrpcConnection {
|
|
||||||
elapsed: start.elapsed().as_millis() as i64,
|
|
||||||
..conn
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
},
|
|
||||||
_ = cancelled_rx.changed() => {
|
|
||||||
upsert_grpc_message(
|
|
||||||
&w,
|
|
||||||
&GrpcMessage {
|
|
||||||
message: "Connection cancelled".to_string(),
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
connection_id: conn.clone().id,
|
|
||||||
is_info: true,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await.unwrap();
|
|
||||||
|
|
||||||
upsert_grpc_connection(
|
|
||||||
&w,
|
|
||||||
&GrpcConnection {
|
|
||||||
elapsed: start.elapsed().as_millis() as i64,
|
|
||||||
..conn
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
w.unlisten(event_handler);
|
let mut stream = match maybe_stream {
|
||||||
});
|
Some(s) => s,
|
||||||
};
|
None => return,
|
||||||
|
};
|
||||||
Ok(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
async fn cmd_grpc_streaming(
|
|
||||||
request_id: &str,
|
|
||||||
w: Window,
|
|
||||||
grpc_handle: State<'_, Mutex<GrpcHandle>>,
|
|
||||||
) -> Result<String, String> {
|
|
||||||
let req = get_grpc_request(&w, request_id)
|
|
||||||
.await
|
|
||||||
.map_err(|e| e.to_string())?;
|
|
||||||
let conn = {
|
|
||||||
let req = req.clone();
|
|
||||||
upsert_grpc_connection(
|
|
||||||
&w,
|
|
||||||
&GrpcConnection {
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|e| e.to_string())?
|
|
||||||
};
|
|
||||||
|
|
||||||
{
|
|
||||||
let conn = conn.clone();
|
|
||||||
let req = req.clone();
|
|
||||||
upsert_grpc_message(
|
|
||||||
&w,
|
|
||||||
&GrpcMessage {
|
|
||||||
message: "Initiating connection".to_string(),
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
connection_id: conn.id,
|
|
||||||
is_info: true,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.expect("Failed to upsert message");
|
|
||||||
};
|
|
||||||
|
|
||||||
let (in_msg_tx, in_msg_rx) = tauri::async_runtime::channel::<String>(16);
|
|
||||||
let (cancelled_tx, mut cancelled_rx) = tokio::sync::watch::channel(false);
|
|
||||||
|
|
||||||
let uri = safe_uri(&req.url).map_err(|e| e.to_string())?;
|
|
||||||
|
|
||||||
let in_msg_stream = tokio_stream::wrappers::ReceiverStream::new(in_msg_rx);
|
|
||||||
|
|
||||||
let (service, method) = {
|
|
||||||
let req = req.clone();
|
|
||||||
match (req.service, req.method) {
|
|
||||||
(Some(service), Some(method)) => (service, method),
|
|
||||||
_ => return Err("Service and method are required".to_string()),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let start = std::time::Instant::now();
|
|
||||||
let mut stream = grpc_handle
|
|
||||||
.lock()
|
|
||||||
.await
|
|
||||||
.connect(
|
|
||||||
&req.clone().id,
|
|
||||||
uri,
|
|
||||||
req.proto_files
|
|
||||||
.0
|
|
||||||
.iter()
|
|
||||||
.map(|p| PathBuf::from_str(p).unwrap())
|
|
||||||
.collect(),
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
.streaming(&service, &method, in_msg_stream)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
|
||||||
enum IncomingMsg {
|
|
||||||
Message(String),
|
|
||||||
Cancel,
|
|
||||||
}
|
|
||||||
|
|
||||||
let cb = {
|
|
||||||
let cancelled_rx = cancelled_rx.clone();
|
|
||||||
let w = w.clone();
|
|
||||||
let conn = conn.clone();
|
|
||||||
let req = req.clone();
|
|
||||||
|
|
||||||
move |ev: tauri::Event| {
|
|
||||||
if *cancelled_rx.borrow() {
|
|
||||||
// Stream is cancelled
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
match serde_json::from_str::<IncomingMsg>(ev.payload().unwrap()) {
|
|
||||||
Ok(IncomingMsg::Message(msg)) => {
|
|
||||||
in_msg_tx.try_send(msg.clone()).unwrap();
|
|
||||||
let w = w.clone();
|
|
||||||
let req = req.clone();
|
|
||||||
let conn = conn.clone();
|
|
||||||
tauri::async_runtime::spawn(async move {
|
|
||||||
upsert_grpc_message(
|
|
||||||
&w,
|
|
||||||
&GrpcMessage {
|
|
||||||
message: msg,
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
connection_id: conn.id,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|e| e.to_string())
|
|
||||||
.unwrap();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Ok(IncomingMsg::Cancel) => {
|
|
||||||
cancelled_tx.send_replace(true);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("Failed to parse gRPC message: {:?}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let event_handler = w.listen_global(format!("grpc_client_msg_{}", conn.id).as_str(), cb);
|
|
||||||
|
|
||||||
let grpc_listen = {
|
|
||||||
let w = w.clone();
|
|
||||||
let conn = conn.clone();
|
|
||||||
let req = req.clone();
|
|
||||||
async move {
|
|
||||||
loop {
|
loop {
|
||||||
match stream.next().await {
|
match stream.next().await {
|
||||||
Some(Ok(item)) => {
|
Some(Ok(item)) => {
|
||||||
@@ -605,6 +375,7 @@ async fn cmd_grpc_streaming(
|
|||||||
let conn = conn.clone();
|
let conn = conn.clone();
|
||||||
tauri::async_runtime::spawn(async move {
|
tauri::async_runtime::spawn(async move {
|
||||||
let w = w.clone();
|
let w = w.clone();
|
||||||
|
let req = req.clone();
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = grpc_listen => {
|
_ = grpc_listen => {
|
||||||
upsert_grpc_message(
|
upsert_grpc_message(
|
||||||
@@ -656,196 +427,6 @@ async fn cmd_grpc_streaming(
|
|||||||
Ok(conn.id)
|
Ok(conn.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
|
||||||
async fn cmd_grpc_server_streaming(
|
|
||||||
request_id: &str,
|
|
||||||
w: Window,
|
|
||||||
grpc_handle: State<'_, Mutex<GrpcHandle>>,
|
|
||||||
) -> Result<GrpcConnection, String> {
|
|
||||||
let req = get_grpc_request(&w, request_id)
|
|
||||||
.await
|
|
||||||
.map_err(|e| e.to_string())?;
|
|
||||||
|
|
||||||
let conn = {
|
|
||||||
let req = req.clone();
|
|
||||||
upsert_grpc_connection(
|
|
||||||
&w,
|
|
||||||
&GrpcConnection {
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|e| e.to_string())?
|
|
||||||
};
|
|
||||||
|
|
||||||
{
|
|
||||||
let req = req.clone();
|
|
||||||
let conn = conn.clone();
|
|
||||||
upsert_grpc_message(
|
|
||||||
&w,
|
|
||||||
&GrpcMessage {
|
|
||||||
message: "Initiating connection".to_string(),
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
connection_id: conn.id,
|
|
||||||
is_info: true,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
let (cancelled_tx, mut cancelled_rx) = tokio::sync::watch::channel(false);
|
|
||||||
|
|
||||||
let (service, method) = match (&req.service, &req.method) {
|
|
||||||
(Some(service), Some(method)) => (service, method),
|
|
||||||
_ => return Err("Service and method are required".to_string()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let uri = safe_uri(&req.url).map_err(|e| e.to_string())?;
|
|
||||||
let mut stream = grpc_handle
|
|
||||||
.lock()
|
|
||||||
.await
|
|
||||||
.connect(
|
|
||||||
&req.clone().id,
|
|
||||||
uri,
|
|
||||||
req.proto_files
|
|
||||||
.0
|
|
||||||
.iter()
|
|
||||||
.map(|p| PathBuf::from_str(p).unwrap())
|
|
||||||
.collect(),
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
.server_streaming(&service, &method, &req.message)
|
|
||||||
.await
|
|
||||||
.expect("FAILED");
|
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
|
||||||
enum IncomingMsg {
|
|
||||||
Cancel,
|
|
||||||
}
|
|
||||||
|
|
||||||
let cb = {
|
|
||||||
let cancelled_rx = cancelled_rx.clone();
|
|
||||||
|
|
||||||
move |ev: tauri::Event| {
|
|
||||||
if *cancelled_rx.borrow() {
|
|
||||||
// Stream is cancelled
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
match serde_json::from_str::<IncomingMsg>(ev.payload().unwrap()) {
|
|
||||||
Ok(IncomingMsg::Cancel) => {
|
|
||||||
cancelled_tx.send_replace(true);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("Failed to parse gRPC message: {:?}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let event_handler = w.listen_global(format!("grpc_client_msg_{}", conn.id).as_str(), cb);
|
|
||||||
|
|
||||||
let start = std::time::Instant::now();
|
|
||||||
let grpc_listen = {
|
|
||||||
let conn_id = conn.clone().id;
|
|
||||||
let w = w.clone();
|
|
||||||
let req = req.clone();
|
|
||||||
async move {
|
|
||||||
loop {
|
|
||||||
let req = req.clone();
|
|
||||||
let conn_id = conn_id.clone();
|
|
||||||
let w = w.clone();
|
|
||||||
match stream.next().await {
|
|
||||||
Some(Ok(item)) => {
|
|
||||||
let item = serialize_message(&item).unwrap();
|
|
||||||
upsert_grpc_message(
|
|
||||||
&w,
|
|
||||||
&GrpcMessage {
|
|
||||||
message: item,
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
connection_id: conn_id,
|
|
||||||
is_server: true,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|e| e.to_string())
|
|
||||||
.expect("Failed to upsert message");
|
|
||||||
}
|
|
||||||
Some(Err(e)) => {
|
|
||||||
error!("gRPC stream error: {:?}", e);
|
|
||||||
// TODO: Handle error
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
info!("gRPC stream closed by sender");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
{
|
|
||||||
let conn = conn.clone();
|
|
||||||
let req = req.clone();
|
|
||||||
let w = w.clone();
|
|
||||||
tauri::async_runtime::spawn(async move {
|
|
||||||
tokio::select! {
|
|
||||||
_ = grpc_listen => {
|
|
||||||
upsert_grpc_message(
|
|
||||||
&w,
|
|
||||||
&GrpcMessage {
|
|
||||||
message: "Connection completed".to_string(),
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
connection_id: conn.clone().id,
|
|
||||||
is_info: true,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await.unwrap();
|
|
||||||
upsert_grpc_connection(
|
|
||||||
&w,
|
|
||||||
&GrpcConnection{
|
|
||||||
elapsed: start.elapsed().as_millis() as i64,
|
|
||||||
..conn
|
|
||||||
},
|
|
||||||
).await.unwrap();
|
|
||||||
},
|
|
||||||
_ = cancelled_rx.changed() => {
|
|
||||||
upsert_grpc_message(
|
|
||||||
&w,
|
|
||||||
&GrpcMessage {
|
|
||||||
message: "Connection cancelled".to_string(),
|
|
||||||
workspace_id: req.workspace_id,
|
|
||||||
request_id: req.id,
|
|
||||||
connection_id: conn.clone().id,
|
|
||||||
is_info: true,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await.unwrap();
|
|
||||||
upsert_grpc_connection(
|
|
||||||
&w,
|
|
||||||
&GrpcConnection{
|
|
||||||
elapsed: start.elapsed().as_millis() as i64,
|
|
||||||
..conn
|
|
||||||
},
|
|
||||||
).await.unwrap();
|
|
||||||
},
|
|
||||||
}
|
|
||||||
w.unlisten(event_handler);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
async fn cmd_send_ephemeral_request(
|
async fn cmd_send_ephemeral_request(
|
||||||
mut request: HttpRequest,
|
mut request: HttpRequest,
|
||||||
@@ -1637,10 +1218,7 @@ fn main() {
|
|||||||
cmd_get_grpc_request,
|
cmd_get_grpc_request,
|
||||||
cmd_get_settings,
|
cmd_get_settings,
|
||||||
cmd_get_workspace,
|
cmd_get_workspace,
|
||||||
cmd_grpc_call_unary,
|
cmd_grpc_go,
|
||||||
cmd_grpc_client_streaming,
|
|
||||||
cmd_grpc_server_streaming,
|
|
||||||
cmd_grpc_streaming,
|
|
||||||
cmd_grpc_reflect,
|
cmd_grpc_reflect,
|
||||||
cmd_import_data,
|
cmd_import_data,
|
||||||
cmd_list_cookie_jars,
|
cmd_list_cookie_jars,
|
||||||
|
|||||||
@@ -85,8 +85,10 @@ export function GlobalHooks() {
|
|||||||
queryClient.setQueryData<Model[]>(queryKey, (values = []) => {
|
queryClient.setQueryData<Model[]>(queryKey, (values = []) => {
|
||||||
const index = values.findIndex((v) => modelsEq(v, payload)) ?? -1;
|
const index = values.findIndex((v) => modelsEq(v, payload)) ?? -1;
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
|
// console.log('UPDATED', payload);
|
||||||
return [...values.slice(0, index), payload, ...values.slice(index + 1)];
|
return [...values.slice(0, index), payload, ...values.slice(index + 1)];
|
||||||
} else {
|
} else {
|
||||||
|
// console.log('CREATED', payload);
|
||||||
return pushToFront ? [payload, ...(values ?? [])] : [...(values ?? []), payload];
|
return pushToFront ? [payload, ...(values ?? [])] : [...(values ?? []), payload];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -36,10 +36,18 @@ export function GrpcEditor({
|
|||||||
|
|
||||||
// Find the schema for the selected service and method and update the editor
|
// Find the schema for the selected service and method and update the editor
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (editorViewRef.current == null || services === null) return;
|
if (
|
||||||
|
editorViewRef.current == null ||
|
||||||
|
services === null ||
|
||||||
|
request.service === null ||
|
||||||
|
request.method === null
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const s = services.find((s) => s.name === request.service);
|
const s = services.find((s) => s.name === request.service);
|
||||||
if (request.service != null && s == null) {
|
if (s == null) {
|
||||||
|
console.log('Failed to find service', { service: request.service, services });
|
||||||
alert({
|
alert({
|
||||||
id: 'grpc-find-service-error',
|
id: 'grpc-find-service-error',
|
||||||
title: "Couldn't Find Service",
|
title: "Couldn't Find Service",
|
||||||
@@ -52,8 +60,9 @@ export function GrpcEditor({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const schema = s?.methods.find((m) => m.name === request.method)?.schema;
|
const schema = s.methods.find((m) => m.name === request.method)?.schema;
|
||||||
if (request.method != null && schema == null) {
|
if (request.method != null && schema == null) {
|
||||||
|
console.log('Failed to find method', { method: request.method, methods: s?.methods });
|
||||||
alert({
|
alert({
|
||||||
id: 'grpc-find-schema-error',
|
id: 'grpc-find-schema-error',
|
||||||
title: "Couldn't Find Method",
|
title: "Couldn't Find Method",
|
||||||
|
|||||||
@@ -493,7 +493,7 @@ function SidebarItems({
|
|||||||
child.item.model === 'http_request' ? (
|
child.item.model === 'http_request' ? (
|
||||||
<HttpMethodTag className="opacity-50">{child.item.method}</HttpMethodTag>
|
<HttpMethodTag className="opacity-50">{child.item.method}</HttpMethodTag>
|
||||||
) : child.item.model === 'grpc_request' ? (
|
) : child.item.model === 'grpc_request' ? (
|
||||||
<HttpMethodTag className="opacity-50">gRPC</HttpMethodTag>
|
<HttpMethodTag className="opacity-50">GRPC</HttpMethodTag>
|
||||||
) : null
|
) : null
|
||||||
}
|
}
|
||||||
onMove={handleMove}
|
onMove={handleMove}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ const methodMap: Record<string, string> = {
|
|||||||
delete: 'DELETE',
|
delete: 'DELETE',
|
||||||
options: 'OPTIONS',
|
options: 'OPTIONS',
|
||||||
head: 'HEAD',
|
head: 'HEAD',
|
||||||
grpc: 'gRPC',
|
grpc: 'GRPC',
|
||||||
};
|
};
|
||||||
|
|
||||||
export function HttpMethodTag({ children: method, className }: Props) {
|
export function HttpMethodTag({ children: method, className }: Props) {
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ export const JsonAttributeTree = ({ depth = 0, attrKey, attrValue, attrKeyJsonPa
|
|||||||
.sort((a, b) => a.localeCompare(b))
|
.sort((a, b) => a.localeCompare(b))
|
||||||
.flatMap((k) => (
|
.flatMap((k) => (
|
||||||
<JsonAttributeTree
|
<JsonAttributeTree
|
||||||
|
key={k}
|
||||||
depth={depth + 1}
|
depth={depth + 1}
|
||||||
attrValue={attrValue[k]}
|
attrValue={attrValue[k]}
|
||||||
attrKey={k}
|
attrKey={k}
|
||||||
@@ -48,6 +49,7 @@ export const JsonAttributeTree = ({ depth = 0, attrKey, attrValue, attrKeyJsonPa
|
|||||||
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
attrValue.flatMap((v: any, i: number) => (
|
attrValue.flatMap((v: any, i: number) => (
|
||||||
<JsonAttributeTree
|
<JsonAttributeTree
|
||||||
|
key={i}
|
||||||
depth={depth + 1}
|
depth={depth + 1}
|
||||||
attrValue={v}
|
attrValue={v}
|
||||||
attrKey={i}
|
attrKey={i}
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
import { useMutation } from '@tanstack/react-query';
|
||||||
import { invoke } from '@tauri-apps/api';
|
import { invoke } from '@tauri-apps/api';
|
||||||
import { trackEvent } from '../lib/analytics';
|
import { trackEvent } from '../lib/analytics';
|
||||||
import type { GrpcRequest } from '../lib/models';
|
import type { GrpcRequest } from '../lib/models';
|
||||||
import { useActiveEnvironmentId } from './useActiveEnvironmentId';
|
import { useActiveEnvironmentId } from './useActiveEnvironmentId';
|
||||||
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
||||||
import { useAppRoutes } from './useAppRoutes';
|
import { useAppRoutes } from './useAppRoutes';
|
||||||
import { grpcRequestsQueryKey } from './useGrpcRequests';
|
|
||||||
|
|
||||||
export function useDuplicateGrpcRequest({
|
export function useDuplicateGrpcRequest({
|
||||||
id,
|
id,
|
||||||
@@ -17,7 +16,6 @@ export function useDuplicateGrpcRequest({
|
|||||||
const activeWorkspaceId = useActiveWorkspaceId();
|
const activeWorkspaceId = useActiveWorkspaceId();
|
||||||
const activeEnvironmentId = useActiveEnvironmentId();
|
const activeEnvironmentId = useActiveEnvironmentId();
|
||||||
const routes = useAppRoutes();
|
const routes = useAppRoutes();
|
||||||
const queryClient = useQueryClient();
|
|
||||||
return useMutation<GrpcRequest, string>({
|
return useMutation<GrpcRequest, string>({
|
||||||
mutationFn: async () => {
|
mutationFn: async () => {
|
||||||
if (id === null) throw new Error("Can't duplicate a null grpc request");
|
if (id === null) throw new Error("Can't duplicate a null grpc request");
|
||||||
@@ -25,10 +23,6 @@ export function useDuplicateGrpcRequest({
|
|||||||
},
|
},
|
||||||
onSettled: () => trackEvent('GrpcRequest', 'Duplicate'),
|
onSettled: () => trackEvent('GrpcRequest', 'Duplicate'),
|
||||||
onSuccess: async (request) => {
|
onSuccess: async (request) => {
|
||||||
queryClient.setQueryData<GrpcRequest[]>(
|
|
||||||
grpcRequestsQueryKey({ workspaceId: request.workspaceId }),
|
|
||||||
(requests) => [...(requests ?? []), request],
|
|
||||||
);
|
|
||||||
if (navigateAfter && activeWorkspaceId !== null) {
|
if (navigateAfter && activeWorkspaceId !== null) {
|
||||||
routes.navigate('request', {
|
routes.navigate('request', {
|
||||||
workspaceId: activeWorkspaceId,
|
workspaceId: activeWorkspaceId,
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
import { useMutation } from '@tanstack/react-query';
|
||||||
import { invoke } from '@tauri-apps/api';
|
import { invoke } from '@tauri-apps/api';
|
||||||
import { trackEvent } from '../lib/analytics';
|
import { trackEvent } from '../lib/analytics';
|
||||||
import type { HttpRequest } from '../lib/models';
|
import type { HttpRequest } from '../lib/models';
|
||||||
import { useActiveEnvironmentId } from './useActiveEnvironmentId';
|
import { useActiveEnvironmentId } from './useActiveEnvironmentId';
|
||||||
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
||||||
import { useAppRoutes } from './useAppRoutes';
|
import { useAppRoutes } from './useAppRoutes';
|
||||||
import { httpRequestsQueryKey } from './useHttpRequests';
|
|
||||||
|
|
||||||
export function useDuplicateHttpRequest({
|
export function useDuplicateHttpRequest({
|
||||||
id,
|
id,
|
||||||
@@ -17,7 +16,6 @@ export function useDuplicateHttpRequest({
|
|||||||
const activeWorkspaceId = useActiveWorkspaceId();
|
const activeWorkspaceId = useActiveWorkspaceId();
|
||||||
const activeEnvironmentId = useActiveEnvironmentId();
|
const activeEnvironmentId = useActiveEnvironmentId();
|
||||||
const routes = useAppRoutes();
|
const routes = useAppRoutes();
|
||||||
const queryClient = useQueryClient();
|
|
||||||
return useMutation<HttpRequest, string>({
|
return useMutation<HttpRequest, string>({
|
||||||
mutationFn: async () => {
|
mutationFn: async () => {
|
||||||
if (id === null) throw new Error("Can't duplicate a null request");
|
if (id === null) throw new Error("Can't duplicate a null request");
|
||||||
@@ -25,10 +23,6 @@ export function useDuplicateHttpRequest({
|
|||||||
},
|
},
|
||||||
onSettled: () => trackEvent('HttpRequest', 'Duplicate'),
|
onSettled: () => trackEvent('HttpRequest', 'Duplicate'),
|
||||||
onSuccess: async (request) => {
|
onSuccess: async (request) => {
|
||||||
queryClient.setQueryData<HttpRequest[]>(
|
|
||||||
httpRequestsQueryKey({ workspaceId: request.workspaceId }),
|
|
||||||
(requests) => [...(requests ?? []), request],
|
|
||||||
);
|
|
||||||
if (navigateAfter && activeWorkspaceId !== null) {
|
if (navigateAfter && activeWorkspaceId !== null) {
|
||||||
routes.navigate('request', {
|
routes.navigate('request', {
|
||||||
workspaceId: activeWorkspaceId,
|
workspaceId: activeWorkspaceId,
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { useMutation, useQuery } from '@tanstack/react-query';
|
|||||||
import { invoke } from '@tauri-apps/api';
|
import { invoke } from '@tauri-apps/api';
|
||||||
import { emit } from '@tauri-apps/api/event';
|
import { emit } from '@tauri-apps/api/event';
|
||||||
import { minPromiseMillis } from '../lib/minPromiseMillis';
|
import { minPromiseMillis } from '../lib/minPromiseMillis';
|
||||||
import type { GrpcConnection, GrpcMessage, GrpcRequest } from '../lib/models';
|
import type { GrpcConnection, GrpcRequest } from '../lib/models';
|
||||||
import { useDebouncedValue } from './useDebouncedValue';
|
import { useDebouncedValue } from './useDebouncedValue';
|
||||||
|
|
||||||
export interface ReflectResponseService {
|
export interface ReflectResponseService {
|
||||||
@@ -13,27 +13,20 @@ export interface ReflectResponseService {
|
|||||||
export function useGrpc(req: GrpcRequest | null, conn: GrpcConnection | null) {
|
export function useGrpc(req: GrpcRequest | null, conn: GrpcConnection | null) {
|
||||||
const requestId = req?.id ?? 'n/a';
|
const requestId = req?.id ?? 'n/a';
|
||||||
|
|
||||||
const unary = useMutation<GrpcMessage, string>({
|
const unary = useMutation<void, string>({
|
||||||
mutationKey: ['grpc_unary', conn?.id ?? 'n/a'],
|
mutationFn: async () => await invoke('cmd_grpc_go', { requestId }),
|
||||||
mutationFn: async () =>
|
|
||||||
(await invoke('cmd_grpc_call_unary', {
|
|
||||||
requestId,
|
|
||||||
})) as GrpcMessage,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const clientStreaming = useMutation<void, string>({
|
const clientStreaming = useMutation<void, string>({
|
||||||
mutationKey: ['grpc_client_streaming', conn?.id ?? 'n/a'],
|
mutationFn: async () => await invoke('cmd_grpc_go', { requestId }),
|
||||||
mutationFn: async () => await invoke('cmd_grpc_client_streaming', { requestId }),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const serverStreaming = useMutation<void, string>({
|
const serverStreaming = useMutation<void, string>({
|
||||||
mutationKey: ['grpc_server_streaming', conn?.id ?? 'n/a'],
|
mutationFn: async () => await invoke('cmd_grpc_go', { requestId }),
|
||||||
mutationFn: async () => await invoke('cmd_grpc_server_streaming', { requestId }),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const streaming = useMutation<void, string>({
|
const streaming = useMutation<void, string>({
|
||||||
mutationKey: ['grpc_streaming', conn?.id ?? 'n/a'],
|
mutationFn: async () => await invoke('cmd_grpc_go', { requestId }),
|
||||||
mutationFn: async () => await invoke('cmd_grpc_streaming', { requestId }),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const send = useMutation({
|
const send = useMutation({
|
||||||
|
|||||||
Reference in New Issue
Block a user