Skip to content

Commit 3929aff

Browse files
[csharp][netcore-httpclient] Refactor of constructors (#9145)
* Refactor on constructors Updated samples Fixed a bug of previous commit Refactor on constructors * Fixed indentation in source code * Updated samples * Marked constructors with obsolete * Updated obsolete constructors messages * Updated samples
1 parent 515d4a0 commit 3929aff

File tree

10 files changed

+214
-692
lines changed

10 files changed

+214
-692
lines changed

modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ namespace {{packageName}}.Client
168168
{
169169
private readonly String _baseUrl;
170170
171-
private readonly HttpClientHandler _httpClientHandler;
172-
private readonly HttpClient _httpClient;
173-
private readonly bool _disposeClient;
171+
private readonly HttpClientHandler _httpClientHandler;
172+
private readonly HttpClient _httpClient;
173+
private readonly bool _disposeClient;
174174
175175
/// <summary>
176176
/// Specifies the settings on a <see cref="JsonSerializer" /> object.
@@ -192,88 +192,63 @@ namespace {{packageName}}.Client
192192
/// <summary>
193193
/// Initializes a new instance of the <see cref="ApiClient" />, defaulting to the global configurations' base url.
194194
/// </summary>
195+
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
195196
public ApiClient() :
196197
this({{packageName}}.Client.GlobalConfiguration.Instance.BasePath)
197-
{
198+
{
198199
}
199-
200+
200201
/// <summary>
201202
/// Initializes a new instance of the <see cref="ApiClient" />.
202203
/// </summary>
203204
/// <param name="basePath">The target service's base path in URL format.</param>
204205
/// <exception cref="ArgumentException"></exception>
206+
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
205207
public ApiClient(String basePath)
206-
{
207-
if (string.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty");
208+
{
209+
if (string.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty");
208210
209-
_httpClientHandler = new HttpClientHandler();
210-
_httpClient = new HttpClient(_httpClientHandler, true);
211+
_httpClientHandler = new HttpClientHandler();
212+
_httpClient = new HttpClient(_httpClientHandler, true);
211213
_disposeClient = true;
212-
_baseUrl = basePath;
214+
_baseUrl = basePath;
213215
}
214-
216+
215217
/// <summary>
216218
/// Initializes a new instance of the <see cref="ApiClient" />, defaulting to the global configurations' base url.
217219
/// </summary>
218220
/// <param name="client">An instance of HttpClient.</param>
219-
/// <exception cref="ArgumentNullException"></exception>
220-
/// <remarks>
221-
/// Some configuration settings will not be applied without passing an HttpClientHandler.
222-
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
223-
/// </remarks>
224-
public ApiClient(HttpClient client) :
225-
this(client, {{packageName}}.Client.GlobalConfiguration.Instance.BasePath)
226-
{
227-
}
228-
229-
/// <summary>
230-
/// Initializes a new instance of the <see cref="ApiClient" />
231-
/// </summary>
232-
/// <param name="client">An instance of HttpClient.</param>
233-
/// <param name="basePath">The target service's base path in URL format.</param>
234-
/// <exception cref="ArgumentNullException"></exception>
235-
/// <exception cref="ArgumentException"></exception>
236-
/// <remarks>
237-
/// Some configuration settings will not be applied without passing an HttpClientHandler.
221+
/// <param name="handler">An optional instance of HttpClientHandler that is used by HttpClient.</param>
222+
/// <exception cref="ArgumentNullException"></exception>
223+
/// <remarks>
224+
/// Some configuration settings will not be applied without passing an HttpClientHandler.
238225
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
239-
/// </remarks>
240-
public ApiClient(HttpClient client, String basePath)
241-
{
242-
if (client == null) throw new ArgumentNullException("client cannot be null");
243-
if (string.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty");
244-
245-
_httpClient = client;
246-
_baseUrl = basePath;
226+
/// </remarks>
227+
public ApiClient(HttpClient client, HttpClientHandler handler = null) :
228+
this(client, {{packageName}}.Client.GlobalConfiguration.Instance.BasePath, handler)
229+
{
247230
}
248-
249-
/// <summary>
250-
/// Initializes a new instance of the <see cref="ApiClient" />, defaulting to the global configurations' base url.
251-
/// </summary>
252-
/// <param name="client">An instance of HttpClient.</param>
253-
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient.</param>
254-
/// <exception cref="ArgumentNullException"></exception>
255-
public ApiClient(HttpClient client, HttpClientHandler handler) :
256-
this(client, handler, {{packageName}}.Client.GlobalConfiguration.Instance.BasePath)
257-
{
258-
}
259-
260-
/// <summary>
231+
232+
/// <summary>
261233
/// Initializes a new instance of the <see cref="ApiClient" />.
262234
/// </summary>
263235
/// <param name="client">An instance of HttpClient.</param>
264-
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient.</param>
265-
/// <param name="basePath">The target service's base path in URL format.</param>
266-
/// <exception cref="ArgumentNullException"></exception>
267-
/// <exception cref="ArgumentException"></exception>
268-
public ApiClient(HttpClient client, HttpClientHandler handler, String basePath)
269-
{
270-
if (client == null) throw new ArgumentNullException("client cannot be null");
271-
if (handler == null) throw new ArgumentNullException("handler cannot be null");
272-
if (string.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty");
273-
274-
_httpClientHandler = handler;
275-
_httpClient = client;
276-
_baseUrl = basePath;
236+
/// <param name="basePath">The target service's base path in URL format.</param>
237+
/// <param name="handler">An optional instance of HttpClientHandler that is used by HttpClient.</param>
238+
/// <exception cref="ArgumentNullException"></exception>
239+
/// <exception cref="ArgumentException"></exception>
240+
/// <remarks>
241+
/// Some configuration settings will not be applied without passing an HttpClientHandler.
242+
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
243+
/// </remarks>
244+
public ApiClient(HttpClient client, String basePath, HttpClientHandler handler = null)
245+
{
246+
if (client == null) throw new ArgumentNullException("client cannot be null");
247+
if (string.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty");
248+
249+
_httpClientHandler = handler;
250+
_httpClient = client;
251+
_baseUrl = basePath;
277252
}
278253

279254
/// <summary>

modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/api.mustache

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ namespace {{packageName}}.{{apiPackage}}
108108
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
109109
/// </summary>
110110
/// <returns></returns>
111+
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
111112
public {{classname}}() : this((string)null)
112113
{
113114
}
@@ -116,8 +117,9 @@ namespace {{packageName}}.{{apiPackage}}
116117
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
117118
/// </summary>
118119
/// <param name="basePath">The target service's base path in URL format.</param>
119-
/// <exception cref="ArgumentException"></exception>
120+
/// <exception cref="ArgumentException"></exception>
120121
/// <returns></returns>
122+
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
121123
public {{classname}}(String basePath)
122124
{
123125
this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
@@ -138,6 +140,7 @@ namespace {{packageName}}.{{apiPackage}}
138140
/// <param name="configuration">An instance of Configuration.</param>
139141
/// <exception cref="ArgumentNullException"></exception>
140142
/// <returns></returns>
143+
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
141144
public {{classname}}({{packageName}}.Client.Configuration configuration)
142145
{
143146
if (configuration == null) throw new ArgumentNullException("configuration");
@@ -158,13 +161,14 @@ namespace {{packageName}}.{{apiPackage}}
158161
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
159162
/// </summary>
160163
/// <param name="client">An instance of HttpClient.</param>
164+
/// <param name="handler">An optional instance of HttpClientHandler that is used by HttpClient.</param>
161165
/// <exception cref="ArgumentNullException"></exception>
162166
/// <returns></returns>
163167
/// <remarks>
164-
/// Some configuration settings will not be applied without passing an HttpClientHandler.
168+
/// Some configuration settings will not be applied without passing an HttpClientHandler.
165169
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
166170
/// </remarks>
167-
public {{classname}}(HttpClient client) : this(client, (string)null)
171+
public {{classname}}(HttpClient client, HttpClientHandler handler = null) : this(client, (string)null, handler)
168172
{
169173
}
170174

@@ -173,22 +177,23 @@ namespace {{packageName}}.{{apiPackage}}
173177
/// </summary>
174178
/// <param name="client">An instance of HttpClient.</param>
175179
/// <param name="basePath">The target service's base path in URL format.</param>
180+
/// <param name="handler">An optional instance of HttpClientHandler that is used by HttpClient.</param>
176181
/// <exception cref="ArgumentNullException"></exception>
177-
/// <exception cref="ArgumentException"></exception>
182+
/// <exception cref="ArgumentException"></exception>
178183
/// <returns></returns>
179-
/// <remarks>
180-
/// Some configuration settings will not be applied without passing an HttpClientHandler.
184+
/// <remarks>
185+
/// Some configuration settings will not be applied without passing an HttpClientHandler.
181186
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
182187
/// </remarks>
183-
public {{classname}}(HttpClient client, String basePath)
188+
public {{classname}}(HttpClient client, String basePath, HttpClientHandler handler = null)
184189
{
185190
if (client == null) throw new ArgumentNullException("client");
186191
187192
this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
188193
{{packageName}}.Client.GlobalConfiguration.Instance,
189194
new {{packageName}}.Client.Configuration { BasePath = basePath }
190195
);
191-
this.ApiClient = new {{packageName}}.Client.ApiClient(client, this.Configuration.BasePath);
196+
this.ApiClient = new {{packageName}}.Client.ApiClient(client, this.Configuration.BasePath, handler);
192197
this.Client = this.ApiClient;
193198
{{#supportsAsync}}
194199
this.AsynchronousClient = this.ApiClient;
@@ -201,85 +206,23 @@ namespace {{packageName}}.{{apiPackage}}
201206
/// </summary>
202207
/// <param name="client">An instance of HttpClient.</param>
203208
/// <param name="configuration">An instance of Configuration.</param>
209+
/// <param name="handler">An optional instance of HttpClientHandler that is used by HttpClient.</param>
204210
/// <exception cref="ArgumentNullException"></exception>
205211
/// <returns></returns>
206212
/// <remarks>
207-
/// Some configuration settings will not be applied without passing an HttpClientHandler.
213+
/// Some configuration settings will not be applied without passing an HttpClientHandler.
208214
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
209215
/// </remarks>
210-
public {{classname}}(HttpClient client, {{packageName}}.Client.Configuration configuration)
216+
public {{classname}}(HttpClient client, {{packageName}}.Client.Configuration configuration, HttpClientHandler handler = null)
211217
{
212218
if (configuration == null) throw new ArgumentNullException("configuration");
213-
if (client == null) throw new ArgumentNullException("client");
214-
215-
this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
216-
{{packageName}}.Client.GlobalConfiguration.Instance,
217-
configuration
218-
);
219-
this.ApiClient = new {{packageName}}.Client.ApiClient(client, this.Configuration.BasePath);
220-
this.Client = this.ApiClient;
221-
{{#supportsAsync}}
222-
this.AsynchronousClient = this.ApiClient;
223-
{{/supportsAsync}}
224-
ExceptionFactory = {{packageName}}.Client.Configuration.DefaultExceptionFactory;
225-
}
226-
227-
/// <summary>
228-
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
229-
/// </summary>
230-
/// <param name="client">An instance of HttpClient.</param>
231-
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient.</param>
232-
/// <exception cref="ArgumentNullException"></exception>
233-
/// <returns></returns>
234-
public {{classname}}(HttpClient client, HttpClientHandler handler) : this(client, handler, (string)null)
235-
{
236-
}
237-
238-
/// <summary>
239-
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
240-
/// </summary>
241-
/// <param name="client">An instance of HttpClient.</param>
242-
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient.</param>
243-
/// <param name="basePath">The target service's base path in URL format.</param>
244-
/// <exception cref="ArgumentNullException"></exception>
245-
/// <exception cref="ArgumentException"></exception>
246-
/// <returns></returns>
247-
public {{classname}}(HttpClient client, HttpClientHandler handler, String basePath)
248-
{
249219
if (client == null) throw new ArgumentNullException("client");
250-
if (handler == null) throw new ArgumentNullException("handler");
251-
252-
this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
253-
{{packageName}}.Client.GlobalConfiguration.Instance,
254-
new {{packageName}}.Client.Configuration { BasePath = basePath }
255-
);
256-
this.ApiClient = new {{packageName}}.Client.ApiClient(client, handler, this.Configuration.BasePath);
257-
this.Client = this.ApiClient;
258-
{{#supportsAsync}}
259-
this.AsynchronousClient = this.ApiClient;
260-
{{/supportsAsync}}
261-
this.ExceptionFactory = {{packageName}}.Client.Configuration.DefaultExceptionFactory;
262-
}
263-
264-
/// <summary>
265-
/// Initializes a new instance of the <see cref="{{classname}}"/> class using Configuration object.
266-
/// </summary>
267-
/// <param name="client">An instance of HttpClient.</param>
268-
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient.</param>
269-
/// <param name="configuration">An instance of Configuration.</param>
270-
/// <exception cref="ArgumentNullException"></exception>
271-
/// <returns></returns>
272-
public {{classname}}(HttpClient client, HttpClientHandler handler, {{packageName}}.Client.Configuration configuration)
273-
{
274-
if (configuration == null) throw new ArgumentNullException("configuration");
275-
if (client == null) throw new ArgumentNullException("client");
276-
if (handler == null) throw new ArgumentNullException("handler");
277220
278221
this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
279222
{{packageName}}.Client.GlobalConfiguration.Instance,
280223
configuration
281224
);
282-
this.ApiClient = new {{packageName}}.Client.ApiClient(client, handler, this.Configuration.BasePath);
225+
this.ApiClient = new {{packageName}}.Client.ApiClient(client, this.Configuration.BasePath, handler);
283226
this.Client = this.ApiClient;
284227
{{#supportsAsync}}
285228
this.AsynchronousClient = this.ApiClient;

0 commit comments

Comments
 (0)