@@ -37,7 +37,7 @@ public partial class RestClient : IRestClient {
37
37
/// </summary>
38
38
public string [ ] AcceptedContentTypes { get ; set ; } = null ! ;
39
39
40
- HttpClient HttpClient { get ; }
40
+ internal HttpClient HttpClient { get ; }
41
41
42
42
/// <inheritdoc />>
43
43
public ReadOnlyRestClientOptions Options { get ; }
@@ -55,24 +55,39 @@ public partial class RestClient : IRestClient {
55
55
/// <param name="options">Client options</param>
56
56
/// <param name="configureDefaultHeaders">Delegate to add default headers to the wrapped HttpClient instance</param>
57
57
/// <param name="configureSerialization">Delegate to configure serialization</param>
58
+ /// <param name="useClientFactory">Set to true if you wish to reuse the <seealso cref="HttpClient"/> instance</param>
58
59
public RestClient (
59
60
RestClientOptions options ,
60
61
ConfigureHeaders ? configureDefaultHeaders = null ,
61
- ConfigureSerialization ? configureSerialization = null
62
+ ConfigureSerialization ? configureSerialization = null ,
63
+ bool useClientFactory = false
62
64
) {
63
- Serializers = new RestSerializers ( ConfigureSerializers ( configureSerialization ) ) ;
65
+ if ( useClientFactory && options . BaseUrl == null ) {
66
+ throw new ArgumentException ( "BaseUrl must be set when using a client factory" ) ;
67
+ }
64
68
65
- Options = new ReadOnlyRestClientOptions ( options ) ;
66
- _disposeHttpClient = true ;
69
+ Serializers = new RestSerializers ( ConfigureSerializers ( configureSerialization ) ) ;
70
+ Options = new ReadOnlyRestClientOptions ( options ) ;
67
71
68
- var handler = new HttpClientHandler ( ) ;
69
- ConfigureHttpMessageHandler ( handler , Options ) ;
72
+ if ( useClientFactory ) {
73
+ _disposeHttpClient = false ;
74
+ HttpClient = SimpleClientFactory . GetClient ( options . BaseUrl ! , GetClient ) ;
75
+ }
76
+ else {
77
+ _disposeHttpClient = true ;
78
+ HttpClient = GetClient ( ) ;
79
+ }
70
80
71
- var finalHandler = options . ConfigureMessageHandler ? . Invoke ( handler ) ?? handler ;
81
+ HttpClient GetClient ( ) {
82
+ var handler = new HttpClientHandler ( ) ;
83
+ ConfigureHttpMessageHandler ( handler , Options ) ;
84
+ var finalHandler = options . ConfigureMessageHandler ? . Invoke ( handler ) ?? handler ;
72
85
73
- HttpClient = new HttpClient ( finalHandler ) ;
74
- ConfigureHttpClient ( ) ;
75
- configureDefaultHeaders ? . Invoke ( HttpClient . DefaultRequestHeaders ) ;
86
+ var httpClient = new HttpClient ( finalHandler ) ;
87
+ ConfigureHttpClient ( httpClient , options ) ;
88
+ configureDefaultHeaders ? . Invoke ( httpClient . DefaultRequestHeaders ) ;
89
+ return httpClient ;
90
+ }
76
91
}
77
92
78
93
static RestClientOptions ConfigureOptions ( RestClientOptions options , ConfigureRestClient ? configureRestClient ) {
@@ -86,12 +101,14 @@ static RestClientOptions ConfigureOptions(RestClientOptions options, ConfigureRe
86
101
/// <param name="configureRestClient">Delegate to configure the client options</param>
87
102
/// <param name="configureDefaultHeaders">Delegate to add default headers to the wrapped HttpClient instance</param>
88
103
/// <param name="configureSerialization">Delegate to configure serialization</param>
104
+ /// <param name="useClientFactory">Set to true if you wish to reuse the <seealso cref="HttpClient"/> instance</param>
89
105
public RestClient (
90
106
ConfigureRestClient ? configureRestClient = null ,
91
107
ConfigureHeaders ? configureDefaultHeaders = null ,
92
- ConfigureSerialization ? configureSerialization = null
108
+ ConfigureSerialization ? configureSerialization = null ,
109
+ bool useClientFactory = false
93
110
)
94
- : this ( ConfigureOptions ( new RestClientOptions ( ) , configureRestClient ) , configureDefaultHeaders , configureSerialization ) { }
111
+ : this ( ConfigureOptions ( new RestClientOptions ( ) , configureRestClient ) , configureDefaultHeaders , configureSerialization , useClientFactory ) { }
95
112
96
113
/// <inheritdoc />
97
114
/// <summary>
@@ -101,16 +118,19 @@ public RestClient(
101
118
/// <param name="configureRestClient">Delegate to configure the client options</param>
102
119
/// <param name="configureDefaultHeaders">Delegate to add default headers to the wrapped HttpClient instance</param>
103
120
/// <param name="configureSerialization">Delegate to configure serialization</param>
121
+ /// <param name="useClientFactory">Set to true if you wish to reuse the <seealso cref="HttpClient"/> instance</param>
104
122
public RestClient (
105
123
Uri baseUrl ,
106
124
ConfigureRestClient ? configureRestClient = null ,
107
125
ConfigureHeaders ? configureDefaultHeaders = null ,
108
- ConfigureSerialization ? configureSerialization = null
126
+ ConfigureSerialization ? configureSerialization = null ,
127
+ bool useClientFactory = false
109
128
)
110
129
: this (
111
130
ConfigureOptions ( new RestClientOptions { BaseUrl = baseUrl } , configureRestClient ) ,
112
131
configureDefaultHeaders ,
113
- configureSerialization
132
+ configureSerialization ,
133
+ useClientFactory
114
134
) { }
115
135
116
136
/// <summary>
@@ -153,7 +173,7 @@ public RestClient(
153
173
var opt = options ?? new RestClientOptions ( ) ;
154
174
Options = new ReadOnlyRestClientOptions ( opt ) ;
155
175
156
- if ( options != null ) ConfigureHttpClient ( ) ;
176
+ if ( options != null ) ConfigureHttpClient ( httpClient , options ) ;
157
177
}
158
178
159
179
/// <summary>
@@ -187,14 +207,14 @@ public RestClient(
187
207
)
188
208
: this ( new HttpClient ( handler , disposeHandler ) , true , configureRestClient , configureSerialization ) { }
189
209
190
- void ConfigureHttpClient ( ) {
191
- if ( Options . MaxTimeout > 0 ) HttpClient . Timeout = TimeSpan . FromMilliseconds ( Options . MaxTimeout ) ;
210
+ static void ConfigureHttpClient ( HttpClient httpClient , RestClientOptions options ) {
211
+ if ( options . MaxTimeout > 0 ) httpClient . Timeout = TimeSpan . FromMilliseconds ( options . MaxTimeout ) ;
192
212
193
- if ( Options . UserAgent != null && HttpClient . DefaultRequestHeaders . UserAgent . All ( x => x . Product ? . Name != Options . UserAgent ) ) {
194
- HttpClient . DefaultRequestHeaders . TryAddWithoutValidation ( KnownHeaders . UserAgent , Options . UserAgent ) ;
213
+ if ( options . UserAgent != null && httpClient . DefaultRequestHeaders . UserAgent . All ( x => x . Product ? . Name != options . UserAgent ) ) {
214
+ httpClient . DefaultRequestHeaders . TryAddWithoutValidation ( KnownHeaders . UserAgent , options . UserAgent ) ;
195
215
}
196
216
197
- if ( Options . Expect100Continue != null ) HttpClient . DefaultRequestHeaders . ExpectContinue = Options . Expect100Continue ;
217
+ if ( options . Expect100Continue != null ) httpClient . DefaultRequestHeaders . ExpectContinue = options . Expect100Continue ;
198
218
}
199
219
200
220
static void ConfigureHttpMessageHandler ( HttpClientHandler handler , ReadOnlyRestClientOptions options ) {
0 commit comments