Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: add client example for tonic-reflection #1601

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ name = "health-server"
path = "src/health/server.rs"
required-features = ["health"]

[[bin]]
name = "reflection-client"
path = "src/reflection/client.rs"
required-features = ["reflection"]

[[bin]]
name = "reflection-server"
path = "src/reflection/server.rs"
Expand Down
44 changes: 44 additions & 0 deletions examples/src/reflection/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use tokio_stream::StreamExt;
use tonic_reflection::pb::{
server_reflection_client::ServerReflectionClient, server_reflection_request::MessageRequest,
server_reflection_response::MessageResponse, ServerReflectionRequest, ServerReflectionResponse,
};

fn parse_response(resp: ServerReflectionResponse) {
let message_response = resp.message_response.expect("message response");

if let MessageResponse::ListServicesResponse(list_response) = message_response {
for svc in list_response.service {
println!("\tfound service: `{}`", svc.name);
}
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let conn = tonic::transport::Endpoint::new("http://[::1]:50052")?
.connect()
.await?;

let mut client = ServerReflectionClient::new(conn);

let list_services_request = ServerReflectionRequest {
host: "host".into(),
message_request: Some(MessageRequest::ListServices("list".into())),
};

let request_stream = tokio_stream::once(list_services_request);
let mut inbound = client
.server_reflection_info(request_stream)
.await?
.into_inner();

while let Some(recv) = inbound.next().await {
match recv {
Ok(resp) => parse_response(resp),
Err(e) => println!("\tdid not receive response due to error: `{}`", e),
}
}

Ok(())
}