xref: /tonic/examples/src/authentication/client.rs (revision df8dd896)
1d9a481baSLucio Franco pub mod pb {
2*df8dd896Stottoto     tonic::include_proto!("grpc.examples.unaryecho");
3d9a481baSLucio Franco }
4d9a481baSLucio Franco 
5d9a481baSLucio Franco use pb::{echo_client::EchoClient, EchoRequest};
6eba7ec7bSLucio Franco use tonic::{metadata::MetadataValue, transport::Channel, Request};
7d9a481baSLucio Franco 
8d9a481baSLucio Franco #[tokio::main]
main() -> Result<(), Box<dyn std::error::Error>>9d9a481baSLucio Franco async fn main() -> Result<(), Box<dyn std::error::Error>> {
10eba7ec7bSLucio Franco     let channel = Channel::from_static("http://[::1]:50051").connect().await?;
11d9a481baSLucio Franco 
12edc5a0d8SAdam Chalmers     let token: MetadataValue<_> = "Bearer some-auth-token".parse()?;
13eba7ec7bSLucio Franco 
14eba7ec7bSLucio Franco     let mut client = EchoClient::with_interceptor(channel, move |mut req: Request<()>| {
15eba7ec7bSLucio Franco         req.metadata_mut().insert("authorization", token.clone());
16eba7ec7bSLucio Franco         Ok(req)
17eba7ec7bSLucio Franco     });
18d9a481baSLucio Franco 
19d9a481baSLucio Franco     let request = tonic::Request::new(EchoRequest {
20d9a481baSLucio Franco         message: "hello".into(),
21d9a481baSLucio Franco     });
22d9a481baSLucio Franco 
23d9a481baSLucio Franco     let response = client.unary_echo(request).await?;
24d9a481baSLucio Franco 
25d9a481baSLucio Franco     println!("RESPONSE={:?}", response);
26d9a481baSLucio Franco 
27d9a481baSLucio Franco     Ok(())
28d9a481baSLucio Franco }
29