xref: /tonic/examples/src/interceptor/client.rs (revision e683ffef)
1 use hello_world::greeter_client::GreeterClient;
2 use hello_world::HelloRequest;
3 use tonic::{transport::Endpoint, Request, Status};
4 
5 pub mod hello_world {
6     tonic::include_proto!("helloworld");
7 }
8 
9 #[tokio::main]
10 async fn main() -> Result<(), Box<dyn std::error::Error>> {
11     let channel = Endpoint::from_static("http://[::1]:50051")
12         .connect()
13         .await?;
14 
15     let mut client = GreeterClient::with_interceptor(channel, intercept);
16 
17     let request = tonic::Request::new(HelloRequest {
18         name: "Tonic".into(),
19     });
20 
21     let response = client.say_hello(request).await?;
22 
23     println!("RESPONSE={:?}", response);
24 
25     Ok(())
26 }
27 
28 /// This function will get called on each outbound request. Returning a
29 /// `Status` here will cancel the request and have that status returned to
30 /// the caller.
31 fn intercept(req: Request<()>) -> Result<Request<()>, Status> {
32     println!("Intercepting request: {:?}", req);
33     Ok(req)
34 }
35