@@ -40,7 +40,7 @@ use self::{
40
40
use crate :: {
41
41
context:: { client:: Config , ClientContext } ,
42
42
error:: {
43
- client:: { builder_error, no_address, ClientError } ,
43
+ client:: { builder_error, no_address, ClientError , Result } ,
44
44
BoxError ,
45
45
} ,
46
46
request:: ClientRequest ,
@@ -463,7 +463,7 @@ impl<IL, OL, C, LB> ClientBuilder<IL, OL, C, LB> {
463
463
}
464
464
465
465
/// Insert a header to the request.
466
- pub fn header < K , V > ( & mut self , key : K , value : V ) -> Result < & mut Self , ClientError >
466
+ pub fn header < K , V > ( & mut self , key : K , value : V ) -> Result < & mut Self >
467
467
where
468
468
K : TryInto < HeaderName > ,
469
469
K :: Error : Error + Send + Sync + ' static ,
@@ -681,6 +681,7 @@ impl<IL, OL, C, LB> ClientBuilder<IL, OL, C, LB> {
681
681
} ;
682
682
683
683
let client_inner = ClientInner {
684
+ service,
684
685
caller_name,
685
686
callee_name : self . callee_name ,
686
687
default_target : self . target ,
@@ -690,14 +691,14 @@ impl<IL, OL, C, LB> ClientBuilder<IL, OL, C, LB> {
690
691
headers : self . headers ,
691
692
} ;
692
693
let client = Client {
693
- service,
694
694
inner : Arc :: new ( client_inner) ,
695
695
} ;
696
696
self . mk_client . mk_client ( client)
697
697
}
698
698
}
699
699
700
- struct ClientInner {
700
+ struct ClientInner < S > {
701
+ service : S ,
701
702
caller_name : FastStr ,
702
703
callee_name : FastStr ,
703
704
default_target : Target ,
@@ -718,7 +719,6 @@ struct ClientInner {
718
719
/// let client = Client::builder().build();
719
720
/// let resp = client
720
721
/// .get("http://httpbin.org/get")
721
- /// .expect("invalid uri")
722
722
/// .send()
723
723
/// .await
724
724
/// .expect("failed to send request")
@@ -728,17 +728,23 @@ struct ClientInner {
728
728
/// println!("{resp:?}");
729
729
/// # })
730
730
/// ```
731
- #[ derive( Clone ) ]
732
731
pub struct Client < S > {
733
- service : S ,
734
- inner : Arc < ClientInner > ,
732
+ inner : Arc < ClientInner < S > > ,
733
+ }
734
+
735
+ impl < S > Clone for Client < S > {
736
+ fn clone ( & self ) -> Self {
737
+ Self {
738
+ inner : Arc :: clone ( & self . inner ) ,
739
+ }
740
+ }
735
741
}
736
742
737
743
macro_rules! method_requests {
738
744
( $method: ident) => {
739
745
paste! {
740
746
#[ doc = concat!( "Create a request with `" , stringify!( [ <$method: upper>] ) , "` method and the given `uri`." ) ]
741
- pub fn [ <$method: lower>] <U >( & self , uri: U ) -> Result < RequestBuilder <S > , ClientError >
747
+ pub fn [ <$method: lower>] <U >( & self , uri: U ) -> RequestBuilder <S >
742
748
where
743
749
U : TryInto <Uri >,
744
750
U :: Error : Into <BoxError >,
@@ -759,16 +765,16 @@ impl Client<()> {
759
765
impl < S > Client < S > {
760
766
/// Create a builder for building a request.
761
767
pub fn request_builder ( & self ) -> RequestBuilder < S > {
762
- RequestBuilder :: new ( self )
768
+ RequestBuilder :: new ( self . clone ( ) )
763
769
}
764
770
765
771
/// Create a builder for building a request with the specified method and URI.
766
- pub fn request < U > ( & self , method : Method , uri : U ) -> Result < RequestBuilder < S > , ClientError >
772
+ pub fn request < U > ( & self , method : Method , uri : U ) -> RequestBuilder < S >
767
773
where
768
774
U : TryInto < Uri > ,
769
775
U :: Error : Into < BoxError > ,
770
776
{
771
- RequestBuilder :: new ( self ) . method ( method) . uri ( uri)
777
+ RequestBuilder :: new ( self . clone ( ) ) . method ( method) . uri ( uri)
772
778
}
773
779
774
780
method_requests ! ( options) ;
@@ -907,7 +913,7 @@ where
907
913
908
914
let has_metainfo = METAINFO . try_with ( |_| { } ) . is_ok ( ) ;
909
915
910
- let fut = self . service . call ( cx, req) ;
916
+ let fut = self . inner . service . call ( cx, req) ;
911
917
912
918
if has_metainfo {
913
919
fut. await
@@ -929,12 +935,12 @@ impl<S> MkClient<Client<S>> for DefaultMkClient {
929
935
}
930
936
931
937
/// Create a GET request to the specified URI.
932
- pub async fn get < U > ( uri : U ) -> Result < ClientResponse , ClientError >
938
+ pub async fn get < U > ( uri : U ) -> Result < ClientResponse >
933
939
where
934
940
U : TryInto < Uri > ,
935
941
U :: Error : Into < BoxError > ,
936
942
{
937
- ClientBuilder :: new ( ) . build ( ) . get ( uri) ? . send ( ) . await
943
+ ClientBuilder :: new ( ) . build ( ) . get ( uri) . send ( ) . await
938
944
}
939
945
940
946
// The `httpbin.org` always responses a json data.
@@ -945,7 +951,10 @@ mod client_tests {
945
951
use std:: { collections:: HashMap , future:: Future } ;
946
952
947
953
use http:: { header, StatusCode } ;
948
- use motore:: { layer:: Layer , service:: Service } ;
954
+ use motore:: {
955
+ layer:: { Layer , Stack } ,
956
+ service:: Service ,
957
+ } ;
949
958
use serde:: Deserialize ;
950
959
use volo:: { context:: Endpoint , layer:: Identity } ;
951
960
@@ -974,7 +983,7 @@ mod client_tests {
974
983
const USER_AGENT_KEY : & str = "User-Agent" ;
975
984
const USER_AGENT_VAL : & str = "volo-http-unit-test" ;
976
985
977
- #[ allow ( unused ) ]
986
+ #[ test ]
978
987
fn client_types_check ( ) {
979
988
struct TestLayer ;
980
989
struct TestService < S > {
@@ -1016,6 +1025,10 @@ mod client_tests {
1016
1025
. layer_inner ( TestLayer )
1017
1026
. layer_outer ( TestLayer )
1018
1027
. build ( ) ;
1028
+ let _: DefaultClient < Stack < TestLayer , TestLayer > > = ClientBuilder :: new ( )
1029
+ . layer_inner ( TestLayer )
1030
+ . layer_inner ( TestLayer )
1031
+ . build ( ) ;
1019
1032
}
1020
1033
1021
1034
#[ tokio:: test]
@@ -1038,7 +1051,6 @@ mod client_tests {
1038
1051
1039
1052
let resp = client
1040
1053
. get ( HTTPBIN_GET )
1041
- . unwrap ( )
1042
1054
. send ( )
1043
1055
. await
1044
1056
. unwrap ( )
@@ -1058,7 +1070,6 @@ mod client_tests {
1058
1070
1059
1071
let resp = client
1060
1072
. get ( "/get" )
1061
- . unwrap ( )
1062
1073
. send ( )
1063
1074
. await
1064
1075
. unwrap ( )
@@ -1081,7 +1092,6 @@ mod client_tests {
1081
1092
1082
1093
let resp = client
1083
1094
. get ( "/get" )
1084
- . unwrap ( )
1085
1095
. send ( )
1086
1096
. await
1087
1097
. unwrap ( )
@@ -1101,7 +1111,6 @@ mod client_tests {
1101
1111
1102
1112
let resp = client
1103
1113
. get ( "/get" )
1104
- . unwrap ( )
1105
1114
. send ( )
1106
1115
. await
1107
1116
. unwrap ( )
@@ -1128,7 +1137,6 @@ mod client_tests {
1128
1137
1129
1138
let resp = client
1130
1139
. get ( "/get" )
1131
- . unwrap ( )
1132
1140
. send ( )
1133
1141
. await
1134
1142
. unwrap ( )
@@ -1145,7 +1153,7 @@ mod client_tests {
1145
1153
builder. host ( "httpbin.org" ) . with_port ( 443 ) ;
1146
1154
let client = builder. build ( ) ;
1147
1155
1148
- let resp = client. get ( "/get" ) . unwrap ( ) . send ( ) . await . unwrap ( ) ;
1156
+ let resp = client. get ( "/get" ) . send ( ) . await . unwrap ( ) ;
1149
1157
// Send HTTP request to the HTTPS port (443), `httpbin.org` will response `400 Bad
1150
1158
// Request`.
1151
1159
assert_eq ! ( resp. status( ) , StatusCode :: BAD_REQUEST ) ;
@@ -1161,7 +1169,6 @@ mod client_tests {
1161
1169
"{}" ,
1162
1170
client
1163
1171
. get( "/post" )
1164
- . unwrap( )
1165
1172
. send( )
1166
1173
. await
1167
1174
. expect_err( "GET for httpbin.org/post should fail" )
@@ -1183,7 +1190,6 @@ mod client_tests {
1183
1190
"{}" ,
1184
1191
client
1185
1192
. get( "https://httpbin.org/get" )
1186
- . unwrap( )
1187
1193
. send( )
1188
1194
. await
1189
1195
. expect_err( "HTTPS with disable_tls should fail" )
@@ -1222,7 +1228,7 @@ mod client_tests {
1222
1228
builder. target_parser ( callopt_should_not_inserted) ;
1223
1229
let client = builder. build ( ) ;
1224
1230
1225
- let resp = client. get ( HTTPBIN_GET ) . unwrap ( ) . send ( ) . await ;
1231
+ let resp = client. get ( HTTPBIN_GET ) . send ( ) . await ;
1226
1232
assert ! ( resp. is_ok( ) ) ;
1227
1233
}
1228
1234
@@ -1233,7 +1239,7 @@ mod client_tests {
1233
1239
builder. target_parser ( callopt_should_not_inserted) ;
1234
1240
let client = builder. build ( ) ;
1235
1241
1236
- let resp = client. get ( HTTPBIN_GET ) . unwrap ( ) . send ( ) . await ;
1242
+ let resp = client. get ( HTTPBIN_GET ) . send ( ) . await ;
1237
1243
assert ! ( resp. is_ok( ) ) ;
1238
1244
}
1239
1245
@@ -1245,7 +1251,6 @@ mod client_tests {
1245
1251
1246
1252
let resp = client
1247
1253
. get ( HTTPBIN_GET )
1248
- . unwrap ( )
1249
1254
. with_callopt ( CallOpt :: new ( ) . with ( CallOptInserted ) )
1250
1255
. send ( )
1251
1256
. await ;
@@ -1261,7 +1266,6 @@ mod client_tests {
1261
1266
1262
1267
let resp = client
1263
1268
. get ( HTTPBIN_GET )
1264
- . unwrap ( )
1265
1269
// insert an empty callopt
1266
1270
. with_callopt ( CallOpt :: new ( ) )
1267
1271
. send ( )
@@ -1277,7 +1281,7 @@ mod client_tests {
1277
1281
builder. target_parser ( callopt_should_not_inserted) ;
1278
1282
let client = builder. build ( ) ;
1279
1283
1280
- let resp = client. get ( HTTPBIN_GET ) . unwrap ( ) . send ( ) . await ;
1284
+ let resp = client. get ( HTTPBIN_GET ) . send ( ) . await ;
1281
1285
assert ! ( resp. is_ok( ) ) ;
1282
1286
}
1283
1287
}
0 commit comments