-
Notifications
You must be signed in to change notification settings - Fork 933
/
client.rs
49 lines (37 loc) · 1.55 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! IMPORTANT: warp is not compatible with hyper-1.0, this example will not
//! compile until warp is updated to support hyper-1.0.
//! To hit the gRPC endpoint you must run this client via:
//! `cargo run --bin hyper-warp-client
//! To hit the warp server you can run this command:
//! `curl localhost:50051/hello`
use hello_world::greeter_client::GreeterClient;
use hello_world::HelloRequest;
use hyper::{Client, Uri};
pub mod hello_world {
tonic::include_proto!("helloworld");
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::builder().http2_only(true).build_http();
let uri = Uri::from_static("http://[::1]:50051");
// Hyper's client requires that requests contain full Uris include a scheme and
// an authority. Tonic's transport will handle this for you but when using the client
// manually you need ensure the uri's are set correctly.
let add_origin = tower::service_fn(|mut req: hyper::Request<tonic::body::BoxBody>| {
let uri = Uri::builder()
.scheme(uri.scheme().unwrap().clone())
.authority(uri.authority().unwrap().clone())
.path_and_query(req.uri().path_and_query().unwrap().clone())
.build()
.unwrap();
*req.uri_mut() = uri;
client.request(req)
});
let mut client = GreeterClient::new(add_origin);
let request = tonic::Request::new(HelloRequest {
name: "Tonic".into(),
});
let response = client.say_hello(request).await?;
println!("RESPONSE={:?}", response);
Ok(())
}