forked from hyperium/tonic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: add client example for
tonic-reflection
This adds an example usage of the `ServerReflectionClient` using a `ListServices` request. Fixes: hyperium#1600
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |