mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-05 00:37:04 +02:00
Client streaming working
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use hyper::client::connect::Connect;
|
||||
use hyper::client::HttpConnector;
|
||||
use hyper::Client;
|
||||
use hyper_rustls::HttpsConnector;
|
||||
@@ -8,7 +9,7 @@ pub use prost_reflect::DynamicMessage;
|
||||
use serde_json::Deserializer;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
use tokio_stream::{Stream, StreamExt};
|
||||
use tokio_stream::StreamExt;
|
||||
use tonic::body::BoxBody;
|
||||
use tonic::transport::Uri;
|
||||
use tonic::{IntoRequest, IntoStreamingRequest, Streaming};
|
||||
@@ -88,6 +89,40 @@ impl GrpcConnection {
|
||||
.into_inner())
|
||||
}
|
||||
|
||||
pub async fn client_streaming(
|
||||
&self,
|
||||
service: &str,
|
||||
method: &str,
|
||||
stream: ReceiverStream<String>,
|
||||
) -> Result<DynamicMessage> {
|
||||
let service = self.pool.get_service_by_name(service).unwrap();
|
||||
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 req = {
|
||||
let method = method.clone();
|
||||
stream
|
||||
.map(move |s| {
|
||||
let mut deserializer = Deserializer::from_str(&s);
|
||||
let req_message =
|
||||
DynamicMessage::deserialize(method.input(), &mut deserializer)
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap();
|
||||
deserializer.end().unwrap();
|
||||
req_message
|
||||
})
|
||||
.into_streaming_request()
|
||||
};
|
||||
let path = method_desc_to_path(method);
|
||||
let codec = DynamicCodec::new(method.clone());
|
||||
client.ready().await.unwrap();
|
||||
Ok(client
|
||||
.client_streaming(req, path, codec)
|
||||
.await
|
||||
.map_err(|s| s.to_string())?
|
||||
.into_inner())
|
||||
}
|
||||
|
||||
pub async fn server_streaming(
|
||||
&self,
|
||||
service: &str,
|
||||
@@ -156,6 +191,20 @@ impl GrpcManager {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn client_streaming(
|
||||
&mut self,
|
||||
id: &str,
|
||||
uri: Uri,
|
||||
service: &str,
|
||||
method: &str,
|
||||
stream: ReceiverStream<String>,
|
||||
) -> Result<DynamicMessage> {
|
||||
self.connect(id, uri)
|
||||
.await
|
||||
.client_streaming(service, method, stream)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn streaming(
|
||||
&mut self,
|
||||
id: &str,
|
||||
@@ -169,6 +218,7 @@ impl GrpcManager {
|
||||
.streaming(service, method, stream)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn connect(&mut self, id: &str, uri: Uri) -> GrpcConnection {
|
||||
let (pool, conn) = fill_pool(&uri).await;
|
||||
let connection = GrpcConnection { pool, conn, uri };
|
||||
|
||||
Reference in New Issue
Block a user