@@ -78,21 +78,21 @@ impl TwirpClientBuilder {
78
78
}
79
79
}
80
80
81
- pub fn build ( self ) -> Result < TwirpClient > {
82
- TwirpClient :: new ( self . base_url , self . builder , self . middleware )
81
+ pub fn build ( self ) -> Result < HttpTwirpClient > {
82
+ HttpTwirpClient :: new ( self . base_url , self . builder , self . middleware )
83
83
}
84
84
}
85
85
86
86
/// `HttpTwirpClient` is a TwirpClient that uses `reqwest::Client` to make http
87
87
/// requests.
88
88
#[ derive( Clone ) ]
89
- pub struct TwirpClient {
89
+ pub struct HttpTwirpClient {
90
90
pub base_url : Arc < Url > ,
91
91
client : Arc < reqwest:: Client > ,
92
92
middlewares : Vec < Arc < dyn Middleware > > ,
93
93
}
94
94
95
- impl std:: fmt:: Debug for TwirpClient {
95
+ impl std:: fmt:: Debug for HttpTwirpClient {
96
96
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
97
97
f. debug_struct ( "TwirpClient" )
98
98
. field ( "base_url" , & self . base_url )
@@ -102,7 +102,19 @@ impl std::fmt::Debug for TwirpClient {
102
102
}
103
103
}
104
104
105
- impl TwirpClient {
105
+ #[ async_trait]
106
+ pub trait TwirpClient {
107
+ fn with < M > ( & self , middleware : M ) -> Self
108
+ where
109
+ M : Middleware ;
110
+
111
+ async fn request < I , O > ( & self , url : Url , body : I ) -> Result < O >
112
+ where
113
+ I : prost:: Message ,
114
+ O : prost:: Message + Default ;
115
+ }
116
+
117
+ impl HttpTwirpClient {
106
118
/// Creates a TwirpClient with the default `reqwest::ClientBuilder`.
107
119
///
108
120
/// The underlying `reqwest::Client` holds a connection pool internally, so it is advised that
@@ -127,14 +139,17 @@ impl TwirpClient {
127
139
let mut headers: HeaderMap < HeaderValue > = HeaderMap :: default ( ) ;
128
140
headers. insert ( CONTENT_TYPE , CONTENT_TYPE_PROTOBUF . try_into ( ) ?) ;
129
141
let client = b. default_headers ( headers) . build ( ) ?;
130
- Ok ( TwirpClient {
142
+ Ok ( HttpTwirpClient {
131
143
base_url : Arc :: new ( base_url) ,
132
144
client : Arc :: new ( client) ,
133
145
middlewares,
134
146
} )
135
147
}
148
+ }
136
149
137
- pub fn with < M > ( & self , middleware : M ) -> Self
150
+ #[ async_trait]
151
+ impl TwirpClient for HttpTwirpClient {
152
+ fn with < M > ( & self , middleware : M ) -> Self
138
153
where
139
154
M : Middleware ,
140
155
{
@@ -147,7 +162,7 @@ impl TwirpClient {
147
162
}
148
163
}
149
164
150
- pub async fn request < I , O > ( & self , url : Url , body : I ) -> Result < O >
165
+ async fn request < I , O > ( & self , url : Url , body : I ) -> Result < O >
151
166
where
152
167
I : prost:: Message ,
153
168
O : prost:: Message + Default ,
@@ -263,10 +278,10 @@ mod tests {
263
278
#[ tokio:: test]
264
279
async fn test_base_url ( ) {
265
280
let url = Url :: parse ( "http://localhost:3001/twirp/" ) . unwrap ( ) ;
266
- assert ! ( TwirpClient :: default ( url) . is_ok( ) ) ;
281
+ assert ! ( HttpTwirpClient :: default ( url) . is_ok( ) ) ;
267
282
let url = Url :: parse ( "http://localhost:3001/twirp" ) . unwrap ( ) ;
268
283
assert_eq ! (
269
- TwirpClient :: default ( url) . unwrap_err( ) . to_string( ) ,
284
+ HttpTwirpClient :: default ( url) . unwrap_err( ) . to_string( ) ,
270
285
"base_url must end in /, but got: http://localhost:3001/twirp" ,
271
286
) ;
272
287
}
@@ -293,7 +308,7 @@ mod tests {
293
308
async fn test_standard_client ( ) {
294
309
let h = run_test_server ( 3001 ) . await ;
295
310
let base_url = Url :: parse ( "http://localhost:3001/twirp/" ) . unwrap ( ) ;
296
- let client = TwirpClient :: default ( base_url) . unwrap ( ) ;
311
+ let client = HttpTwirpClient :: default ( base_url) . unwrap ( ) ;
297
312
let resp = client
298
313
. ping ( PingRequest {
299
314
name : "hi" . to_string ( ) ,
0 commit comments