Skip to content

[csharp][netcore-httpclient] Refactor of constructors #9145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ namespace {{packageName}}.Client
{
private readonly String _baseUrl;

private readonly HttpClientHandler _httpClientHandler;
private readonly HttpClient _httpClient;
private readonly bool _disposeClient;
private readonly HttpClientHandler _httpClientHandler;
private readonly HttpClient _httpClient;
private readonly bool _disposeClient;

/// <summary>
/// Specifies the settings on a <see cref="JsonSerializer" /> object.
Expand All @@ -192,88 +192,63 @@ namespace {{packageName}}.Client
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" />, defaulting to the global configurations' base url.
/// </summary>
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
public ApiClient() :
this({{packageName}}.Client.GlobalConfiguration.Instance.BasePath)
{
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" />.
/// </summary>
/// <param name="basePath">The target service's base path in URL format.</param>
/// <exception cref="ArgumentException"></exception>
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
public ApiClient(String basePath)
{
if (string.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty");
{
if (string.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty");

_httpClientHandler = new HttpClientHandler();
_httpClient = new HttpClient(_httpClientHandler, true);
_httpClientHandler = new HttpClientHandler();
_httpClient = new HttpClient(_httpClientHandler, true);
_disposeClient = true;
_baseUrl = basePath;
_baseUrl = basePath;
}
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" />, defaulting to the global configurations' base url.
/// </summary>
/// <param name="client">An instance of HttpClient.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <remarks>
/// Some configuration settings will not be applied without passing an HttpClientHandler.
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
/// </remarks>
public ApiClient(HttpClient client) :
this(client, {{packageName}}.Client.GlobalConfiguration.Instance.BasePath)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" />
/// </summary>
/// <param name="client">An instance of HttpClient.</param>
/// <param name="basePath">The target service's base path in URL format.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <remarks>
/// Some configuration settings will not be applied without passing an HttpClientHandler.
/// <param name="handler">An optional instance of HttpClientHandler that is used by HttpClient.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <remarks>
/// Some configuration settings will not be applied without passing an HttpClientHandler.
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
/// </remarks>
public ApiClient(HttpClient client, String basePath)
{
if (client == null) throw new ArgumentNullException("client cannot be null");
if (string.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty");

_httpClient = client;
_baseUrl = basePath;
/// </remarks>
public ApiClient(HttpClient client, HttpClientHandler handler = null) :
this(client, {{packageName}}.Client.GlobalConfiguration.Instance.BasePath, handler)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" />, defaulting to the global configurations' base url.
/// </summary>
/// <param name="client">An instance of HttpClient.</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient.</param>
/// <exception cref="ArgumentNullException"></exception>
public ApiClient(HttpClient client, HttpClientHandler handler) :
this(client, handler, {{packageName}}.Client.GlobalConfiguration.Instance.BasePath)
{
}

/// <summary>

/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" />.
/// </summary>
/// <param name="client">An instance of HttpClient.</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient.</param>
/// <param name="basePath">The target service's base path in URL format.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public ApiClient(HttpClient client, HttpClientHandler handler, String basePath)
{
if (client == null) throw new ArgumentNullException("client cannot be null");
if (handler == null) throw new ArgumentNullException("handler cannot be null");
if (string.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty");

_httpClientHandler = handler;
_httpClient = client;
_baseUrl = basePath;
/// <param name="basePath">The target service's base path in URL format.</param>
/// <param name="handler">An optional instance of HttpClientHandler that is used by HttpClient.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <remarks>
/// Some configuration settings will not be applied without passing an HttpClientHandler.
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
/// </remarks>
public ApiClient(HttpClient client, String basePath, HttpClientHandler handler = null)
{
if (client == null) throw new ArgumentNullException("client cannot be null");
if (string.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty");

_httpClientHandler = handler;
_httpClient = client;
_baseUrl = basePath;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ namespace {{packageName}}.{{apiPackage}}
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
/// </summary>
/// <returns></returns>
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
public {{classname}}() : this((string)null)
{
}
Expand All @@ -116,8 +117,9 @@ namespace {{packageName}}.{{apiPackage}}
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
/// </summary>
/// <param name="basePath">The target service's base path in URL format.</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <returns></returns>
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
public {{classname}}(String basePath)
{
this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
Expand All @@ -138,6 +140,7 @@ namespace {{packageName}}.{{apiPackage}}
/// <param name="configuration">An instance of Configuration.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
public {{classname}}({{packageName}}.Client.Configuration configuration)
{
if (configuration == null) throw new ArgumentNullException("configuration");
Expand All @@ -158,13 +161,14 @@ namespace {{packageName}}.{{apiPackage}}
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
/// </summary>
/// <param name="client">An instance of HttpClient.</param>
/// <param name="handler">An optional instance of HttpClientHandler that is used by HttpClient.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
/// <remarks>
/// Some configuration settings will not be applied without passing an HttpClientHandler.
/// Some configuration settings will not be applied without passing an HttpClientHandler.
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
/// </remarks>
public {{classname}}(HttpClient client) : this(client, (string)null)
public {{classname}}(HttpClient client, HttpClientHandler handler = null) : this(client, (string)null, handler)
{
}

Expand All @@ -173,22 +177,23 @@ namespace {{packageName}}.{{apiPackage}}
/// </summary>
/// <param name="client">An instance of HttpClient.</param>
/// <param name="basePath">The target service's base path in URL format.</param>
/// <param name="handler">An optional instance of HttpClientHandler that is used by HttpClient.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <returns></returns>
/// <remarks>
/// Some configuration settings will not be applied without passing an HttpClientHandler.
/// <remarks>
/// Some configuration settings will not be applied without passing an HttpClientHandler.
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
/// </remarks>
public {{classname}}(HttpClient client, String basePath)
public {{classname}}(HttpClient client, String basePath, HttpClientHandler handler = null)
{
if (client == null) throw new ArgumentNullException("client");

this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
{{packageName}}.Client.GlobalConfiguration.Instance,
new {{packageName}}.Client.Configuration { BasePath = basePath }
);
this.ApiClient = new {{packageName}}.Client.ApiClient(client, this.Configuration.BasePath);
this.ApiClient = new {{packageName}}.Client.ApiClient(client, this.Configuration.BasePath, handler);
this.Client = this.ApiClient;
{{#supportsAsync}}
this.AsynchronousClient = this.ApiClient;
Expand All @@ -201,85 +206,23 @@ namespace {{packageName}}.{{apiPackage}}
/// </summary>
/// <param name="client">An instance of HttpClient.</param>
/// <param name="configuration">An instance of Configuration.</param>
/// <param name="handler">An optional instance of HttpClientHandler that is used by HttpClient.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
/// <remarks>
/// Some configuration settings will not be applied without passing an HttpClientHandler.
/// Some configuration settings will not be applied without passing an HttpClientHandler.
/// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
/// </remarks>
public {{classname}}(HttpClient client, {{packageName}}.Client.Configuration configuration)
public {{classname}}(HttpClient client, {{packageName}}.Client.Configuration configuration, HttpClientHandler handler = null)
{
if (configuration == null) throw new ArgumentNullException("configuration");
if (client == null) throw new ArgumentNullException("client");

this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
{{packageName}}.Client.GlobalConfiguration.Instance,
configuration
);
this.ApiClient = new {{packageName}}.Client.ApiClient(client, this.Configuration.BasePath);
this.Client = this.ApiClient;
{{#supportsAsync}}
this.AsynchronousClient = this.ApiClient;
{{/supportsAsync}}
ExceptionFactory = {{packageName}}.Client.Configuration.DefaultExceptionFactory;
}

/// <summary>
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
/// </summary>
/// <param name="client">An instance of HttpClient.</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
public {{classname}}(HttpClient client, HttpClientHandler handler) : this(client, handler, (string)null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
/// </summary>
/// <param name="client">An instance of HttpClient.</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient.</param>
/// <param name="basePath">The target service's base path in URL format.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <returns></returns>
public {{classname}}(HttpClient client, HttpClientHandler handler, String basePath)
{
if (client == null) throw new ArgumentNullException("client");
if (handler == null) throw new ArgumentNullException("handler");

this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
{{packageName}}.Client.GlobalConfiguration.Instance,
new {{packageName}}.Client.Configuration { BasePath = basePath }
);
this.ApiClient = new {{packageName}}.Client.ApiClient(client, handler, this.Configuration.BasePath);
this.Client = this.ApiClient;
{{#supportsAsync}}
this.AsynchronousClient = this.ApiClient;
{{/supportsAsync}}
this.ExceptionFactory = {{packageName}}.Client.Configuration.DefaultExceptionFactory;
}

/// <summary>
/// Initializes a new instance of the <see cref="{{classname}}"/> class using Configuration object.
/// </summary>
/// <param name="client">An instance of HttpClient.</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient.</param>
/// <param name="configuration">An instance of Configuration.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
public {{classname}}(HttpClient client, HttpClientHandler handler, {{packageName}}.Client.Configuration configuration)
{
if (configuration == null) throw new ArgumentNullException("configuration");
if (client == null) throw new ArgumentNullException("client");
if (handler == null) throw new ArgumentNullException("handler");

this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
{{packageName}}.Client.GlobalConfiguration.Instance,
configuration
);
this.ApiClient = new {{packageName}}.Client.ApiClient(client, handler, this.Configuration.BasePath);
this.ApiClient = new {{packageName}}.Client.ApiClient(client, this.Configuration.BasePath, handler);
this.Client = this.ApiClient;
{{#supportsAsync}}
this.AsynchronousClient = this.ApiClient;
Expand Down
Loading