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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
pub mod client_streaming;
pub mod server_streaming;
pub mod streaming;
pub mod unary;
use crate::body::BoxBody;
use crate::generic::client::{GrpcService, IntoService};
use futures::{stream, Future, Poll, Stream};
use http::{uri, Uri};
use prost::Message;
#[derive(Debug, Clone)]
pub struct Grpc<T> {
inner: T,
}
pub trait Encodable<T> {
fn into_encode(self) -> T;
}
impl<T> Grpc<T> {
pub fn new(inner: T) -> Self {
Grpc { inner }
}
pub fn poll_ready<R>(&mut self) -> Poll<(), crate::Status>
where
T: GrpcService<R>,
{
self.inner
.poll_ready()
.map_err(|err| crate::Status::from_error(&*(err.into())))
}
pub fn ready<R>(self) -> impl Future<Item = Self, Error = crate::Status>
where
T: GrpcService<R>,
{
use tower_util::Ready;
Ready::new(self.inner.into_service())
.map(|IntoService(inner)| Grpc { inner })
.map_err(|err| crate::Status::from_error(&*(err.into())))
}
pub fn unary<M1, M2, R>(
&mut self,
request: crate::Request<M1>,
path: uri::PathAndQuery,
) -> unary::ResponseFuture<M2, T::Future, T::ResponseBody>
where
T: GrpcService<R>,
unary::Once<M1>: Encodable<R>,
{
let request = request.map(|v| stream::once(Ok(v)));
let response = self.client_streaming(request, path);
unary::ResponseFuture::new(response)
}
pub fn client_streaming<B, M, R>(
&mut self,
request: crate::Request<B>,
path: uri::PathAndQuery,
) -> client_streaming::ResponseFuture<M, T::Future, T::ResponseBody>
where
T: GrpcService<R>,
B: Encodable<R>,
{
let response = self.streaming(request, path);
client_streaming::ResponseFuture::new(response)
}
pub fn server_streaming<M1, M2, R>(
&mut self,
request: crate::Request<M1>,
path: uri::PathAndQuery,
) -> server_streaming::ResponseFuture<M2, T::Future>
where
T: GrpcService<R>,
unary::Once<M1>: Encodable<R>,
{
let request = request.map(|v| stream::once(Ok(v)));
let response = self.streaming(request, path);
server_streaming::ResponseFuture::new(response)
}
pub fn streaming<B, M, R>(
&mut self,
request: crate::Request<B>,
path: uri::PathAndQuery,
) -> streaming::ResponseFuture<M, T::Future>
where
T: GrpcService<R>,
B: Encodable<R>,
{
use http::header::{self, HeaderValue};
let mut parts = uri::Parts::default();
parts.path_and_query = Some(path);
let uri = Uri::from_parts(parts).expect("path_and_query only is valid Uri");
let request = request.map(Encodable::into_encode);
let mut request = request.into_http(uri);
request
.headers_mut()
.insert(header::TE, HeaderValue::from_static("trailers"));
let content_type = "application/grpc+proto";
request
.headers_mut()
.insert(header::CONTENT_TYPE, HeaderValue::from_static(content_type));
let response = self.inner.call(request);
streaming::ResponseFuture::new(response)
}
}
impl<T, U> Encodable<BoxBody> for T
where
T: Stream<Item = U, Error = crate::Status> + Send + 'static,
U: Message + 'static,
{
fn into_encode(self) -> BoxBody {
use crate::codec::Encoder;
use crate::generic::Encode;
let encode = Encode::request(Encoder::new(), self);
BoxBody::new(Box::new(encode))
}
}