@@ -37,7 +37,7 @@ public partial class RestClient : IRestClient {
3737 /// </summary>
3838 public string [ ] AcceptedContentTypes { get ; set ; } = null ! ;
3939
40- HttpClient HttpClient { get ; }
40+ internal HttpClient HttpClient { get ; }
4141
4242 /// <inheritdoc />>
4343 public ReadOnlyRestClientOptions Options { get ; }
@@ -55,24 +55,39 @@ public partial class RestClient : IRestClient {
5555 /// <param name="options">Client options</param>
5656 /// <param name="configureDefaultHeaders">Delegate to add default headers to the wrapped HttpClient instance</param>
5757 /// <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>
5859 public RestClient (
5960 RestClientOptions options ,
6061 ConfigureHeaders ? configureDefaultHeaders = null ,
61- ConfigureSerialization ? configureSerialization = null
62+ ConfigureSerialization ? configureSerialization = null ,
63+ bool useClientFactory = false
6264 ) {
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+ }
6468
65- Options = new ReadOnlyRestClientOptions ( options ) ;
66- _disposeHttpClient = true ;
69+ Serializers = new RestSerializers ( ConfigureSerializers ( configureSerialization ) ) ;
70+ Options = new ReadOnlyRestClientOptions ( options ) ;
6771
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+ }
7080
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 ;
7285
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+ }
7691 }
7792
7893 static RestClientOptions ConfigureOptions ( RestClientOptions options , ConfigureRestClient ? configureRestClient ) {
@@ -86,12 +101,14 @@ static RestClientOptions ConfigureOptions(RestClientOptions options, ConfigureRe
86101 /// <param name="configureRestClient">Delegate to configure the client options</param>
87102 /// <param name="configureDefaultHeaders">Delegate to add default headers to the wrapped HttpClient instance</param>
88103 /// <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>
89105 public RestClient (
90106 ConfigureRestClient ? configureRestClient = null ,
91107 ConfigureHeaders ? configureDefaultHeaders = null ,
92- ConfigureSerialization ? configureSerialization = null
108+ ConfigureSerialization ? configureSerialization = null ,
109+ bool useClientFactory = false
93110 )
94- : this ( ConfigureOptions ( new RestClientOptions ( ) , configureRestClient ) , configureDefaultHeaders , configureSerialization ) { }
111+ : this ( ConfigureOptions ( new RestClientOptions ( ) , configureRestClient ) , configureDefaultHeaders , configureSerialization , useClientFactory ) { }
95112
96113 /// <inheritdoc />
97114 /// <summary>
@@ -101,16 +118,19 @@ public RestClient(
101118 /// <param name="configureRestClient">Delegate to configure the client options</param>
102119 /// <param name="configureDefaultHeaders">Delegate to add default headers to the wrapped HttpClient instance</param>
103120 /// <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>
104122 public RestClient (
105123 Uri baseUrl ,
106124 ConfigureRestClient ? configureRestClient = null ,
107125 ConfigureHeaders ? configureDefaultHeaders = null ,
108- ConfigureSerialization ? configureSerialization = null
126+ ConfigureSerialization ? configureSerialization = null ,
127+ bool useClientFactory = false
109128 )
110129 : this (
111130 ConfigureOptions ( new RestClientOptions { BaseUrl = baseUrl } , configureRestClient ) ,
112131 configureDefaultHeaders ,
113- configureSerialization
132+ configureSerialization ,
133+ useClientFactory
114134 ) { }
115135
116136 /// <summary>
@@ -153,7 +173,7 @@ public RestClient(
153173 var opt = options ?? new RestClientOptions ( ) ;
154174 Options = new ReadOnlyRestClientOptions ( opt ) ;
155175
156- if ( options != null ) ConfigureHttpClient ( ) ;
176+ if ( options != null ) ConfigureHttpClient ( httpClient , options ) ;
157177 }
158178
159179 /// <summary>
@@ -187,14 +207,14 @@ public RestClient(
187207 )
188208 : this ( new HttpClient ( handler , disposeHandler ) , true , configureRestClient , configureSerialization ) { }
189209
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 ) ;
192212
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 ) ;
195215 }
196216
197- if ( Options . Expect100Continue != null ) HttpClient . DefaultRequestHeaders . ExpectContinue = Options . Expect100Continue ;
217+ if ( options . Expect100Continue != null ) httpClient . DefaultRequestHeaders . ExpectContinue = options . Expect100Continue ;
198218 }
199219
200220 static void ConfigureHttpMessageHandler ( HttpClientHandler handler , ReadOnlyRestClientOptions options ) {
0 commit comments