[csharp][netcore-httpclient] Reuse HttpClient, Allow use of external HttpClient, Fix Socket Exhaustion, Alternative With Constructor Injection (#9085)

* Do not allow not reusing HttpClient, fix socket exhaustion issues, add Readme on how to add your own client

* Add Readme change

* Alternative version via constructor injection

* Update samples

* Update usage in ExecAsync

* Clean Constructor

* Add Warning to constructor, Update Samples

* Add warning, fix last constructor, update samples

* Change client to private in constructor injection

* Add disposable to ApiClient

* Do not dispose handler if we did not create it

* Update samples

* Add disable switch and update samples and documentation
This commit is contained in:
Blackclaws
2021-03-27 08:20:58 +01:00
committed by GitHub
parent b7f389aacb
commit 45654d28ed
12 changed files with 1058 additions and 120 deletions

View File

@@ -100,6 +100,29 @@ System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/");
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
c.Proxy = webProxy;
```
{{#useHttpClient}}
To use your own HttpClient instances just pass them to the ApiClass constructor.
```csharp
HttpClientHandler yourHandler = new HttpClientHandler();
HttpClient yourHttpClient = new HttpClient(yourHandler);
var api = new YourApiClass(yourHttpClient, yourHandler);
```
If you want to use an HttpClient and don't have access to the handler, for example in a DI context in aspnetcore when
using IHttpClientFactory. You need to disable the features that require handler access:
```csharp
HttpClient yourHttpClient = new HttpClient();
var api = new YourApiClass(yourHttpClient, null, true);
```
The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
You need to either manually handle those in your setup of the HttpClient or they won't be available.
{{/useHttpClient}}
<a name="getting-started"></a>
## Getting Started

View File

@@ -161,13 +161,16 @@ namespace {{packageName}}.Client
/// Provides a default implementation of an Api client (both synchronous and asynchronous implementatios),
/// encapsulating general REST accessor use cases.
/// </summary>
{{>visibility}} partial class ApiClient : ISynchronousClient{{#supportsAsync}}, IAsynchronousClient{{/supportsAsync}}
{{>visibility}} partial class ApiClient : IDisposable, ISynchronousClient{{#supportsAsync}}, IAsynchronousClient{{/supportsAsync}}
{
private readonly String _baseUrl;
{{#reUseHttpClient}}
private readonly HttpClientHandler _httpClientHandler;
private readonly bool _disposeHandler;
private readonly HttpClient _httpClient;
{{/reUseHttpClient}}
private readonly bool _disposeClient;
private readonly bool _disableHandlerFeatures;
/// <summary>
/// Specifies the settings on a <see cref="JsonSerializer" /> object.
@@ -189,30 +192,50 @@ namespace {{packageName}}.Client
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" />, defaulting to the global configurations' base url.
/// </summary>
public ApiClient()
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
public ApiClient(HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false) :
this({{packageName}}.Client.GlobalConfiguration.Instance.BasePath, client, handler, disableHandlerFeatures)
{
_baseUrl = {{packageName}}.Client.GlobalConfiguration.Instance.BasePath;
{{#reUseHttpClient}}
_httpClientHandler = new HttpClientHandler();
_httpClient = new HttpClient(_httpClientHandler);
{{/reUseHttpClient}}
}
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" />
/// </summary>
/// <param name="basePath">The target service's base path in URL format.</param>
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <exception cref="ArgumentException"></exception>
public ApiClient(String basePath)
public ApiClient(String basePath, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
if (string.IsNullOrEmpty(basePath))
throw new ArgumentException("basePath cannot be empty");
_baseUrl = basePath;
{{#reUseHttpClient}}
_httpClientHandler = new HttpClientHandler();
_httpClient = new HttpClient(_httpClientHandler);
{{/reUseHttpClient}}
if((client != null && handler == null) && !disableHandlerFeatures) {
throw new ArgumentException("If providing HttpClient, you also need to provide its handler or disable features requiring the handler, see README.md");
}
_disableHandlerFeatures = disableHandlerFeatures;
_httpClientHandler = handler ?? new HttpClientHandler();
_disposeHandler = handler == null;
_httpClient = client ?? new HttpClient(_httpClientHandler, false);
_disposeClient = client == null;
}
/// <summary>
/// Disposes resources if they were created by us
/// </summary>
public void Dispose()
{
if(_disposeClient) {
_httpClient.Dispose();
}
if(_disposeHandler) {
_httpClientHandler.Dispose();
}
}
/// Prepares multipart/form-data content
@@ -275,6 +298,11 @@ namespace {{packageName}}.Client
HttpRequestMessage request = new HttpRequestMessage(method, builder.GetFullUri());
if (configuration.UserAgent != null)
{
request.Headers.TryAddWithoutValidation("User-Agent", configuration.UserAgent);
}
if (configuration.DefaultHeaders != null)
{
foreach (var headerParam in configuration.DefaultHeaders)
@@ -377,15 +405,18 @@ namespace {{packageName}}.Client
}
}
if (response != null)
if(!_disableHandlerFeatures)
{
try {
foreach (Cookie cookie in handler.CookieContainer.GetCookies(uri))
{
transformed.Cookies.Add(cookie);
if (response != null)
{
try {
foreach (Cookie cookie in handler.CookieContainer.GetCookies(uri))
{
transformed.Cookies.Add(cookie);
}
}
catch (PlatformNotSupportedException) {}
}
catch (PlatformNotSupportedException) {}
}
return transformed;
@@ -400,14 +431,8 @@ namespace {{packageName}}.Client
IReadableConfiguration configuration,
System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
{{^reUseHttpClient}}
var handler = new HttpClientHandler();
var client = new HttpClient();
{{/reUseHttpClient}}
{{#reUseHttpClient}}
var handler = _httpClientHandler;
var client = _httpClient;
{{/reUseHttpClient}}
var deserializer = new CustomJsonCodec(SerializerSettings, configuration);
var finalToken = cancellationToken;
@@ -417,20 +442,16 @@ namespace {{packageName}}.Client
var tokenSource = new CancellationTokenSource(configuration.Timeout);
finalToken = CancellationTokenSource.CreateLinkedTokenSource(finalToken, tokenSource.Token).Token;
}
if(!_disableHandlerFeatures) {
if (configuration.Proxy != null)
{
handler.Proxy = configuration.Proxy;
}
if (configuration.Proxy != null)
{
handler.Proxy = configuration.Proxy;
}
if (configuration.UserAgent != null)
{
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", configuration.UserAgent);
}
if (configuration.ClientCertificates != null)
{
handler.ClientCertificates.AddRange(configuration.ClientCertificates);
if (configuration.ClientCertificates != null)
{
handler.ClientCertificates.AddRange(configuration.ClientCertificates);
}
}
var cookieContainer = req.Properties.ContainsKey("CookieContainer") ? req.Properties["CookieContainer"] as List<Cookie> : null;

View File

@@ -0,0 +1,652 @@
{{>partial_header}}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using {{packageName}}.Client;
{{#hasImport}}using {{packageName}}.{{modelPackage}};
{{/hasImport}}
namespace {{packageName}}.{{apiPackage}}
{
{{#operations}}
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
{{>visibility}} interface {{interfacePrefix}}{{classname}}Sync : IApiAccessor
{
#region Synchronous Operations
{{#operation}}
/// <summary>
/// {{summary}}
/// </summary>
{{#notes}}
/// <remarks>
/// {{notes}}
/// </remarks>
{{/notes}}
/// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
{{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
{{/allParams}}/// <returns>{{#returnType}}{{returnType}}{{/returnType}}</returns>
{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}});
/// <summary>
/// {{summary}}
/// </summary>
/// <remarks>
/// {{notes}}
/// </remarks>
/// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
{{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
{{/allParams}}/// <returns>ApiResponse of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Object(void){{/returnType}}</returns>
ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}> {{operationId}}WithHttpInfo({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}});
{{/operation}}
#endregion Synchronous Operations
}
{{#supportsAsync}}
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
{{>visibility}} interface {{interfacePrefix}}{{classname}}Async : IApiAccessor
{
#region Asynchronous Operations
{{#operation}}
/// <summary>
/// {{summary}}
/// </summary>
/// <remarks>
/// {{notes}}
/// </remarks>
/// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
{{#allParams}}
/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
{{/allParams}}
/// <param name="cancellationToken">Cancellation Token to cancel the request.</param>
/// <returns>Task of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}void{{/returnType}}</returns>
{{#returnType}}System.Threading.Tasks.Task<{{{returnType}}}>{{/returnType}}{{^returnType}}System.Threading.Tasks.Task{{/returnType}} {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
/// <summary>
/// {{summary}}
/// </summary>
/// <remarks>
/// {{notes}}
/// </remarks>
/// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
{{#allParams}}
/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
{{/allParams}}
/// <param name="cancellationToken">Cancellation Token to cancel the request.</param>
/// <returns>Task of ApiResponse{{#returnType}} ({{returnType}}){{/returnType}}</returns>
System.Threading.Tasks.Task<ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}>> {{operationId}}WithHttpInfoAsync({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
{{/operation}}
#endregion Asynchronous Operations
}
{{/supportsAsync}}
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
{{>visibility}} interface {{interfacePrefix}}{{classname}} : {{interfacePrefix}}{{classname}}Sync{{#supportsAsync}}, {{interfacePrefix}}{{classname}}Async{{/supportsAsync}}
{
}
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
{{>visibility}} partial class {{classname}} : IDisposable, {{interfacePrefix}}{{classname}}
{
private {{packageName}}.Client.ExceptionFactory _exceptionFactory = (name, response) => 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public {{classname}}(HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false) : this((string)null, client, handler, disableHandlerFeatures)
{
}
/// <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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public {{classname}}(String basePath, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
{{packageName}}.Client.GlobalConfiguration.Instance,
new {{packageName}}.Client.Configuration { BasePath = basePath }
);
this.ApiClient = new {{packageName}}.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
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="configuration">An instance of Configuration</param>
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public {{classname}}({{packageName}}.Client.Configuration configuration, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
if (configuration == null) throw new ArgumentNullException("configuration");
this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
{{packageName}}.Client.GlobalConfiguration.Instance,
configuration
);
this.ApiClient = new {{packageName}}.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
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
/// using a Configuration object and client instance.
/// </summary>
/// <param name="client">The client interface for synchronous API access.</param>{{#supportsAsync}}
/// <param name="asyncClient">The client interface for asynchronous API access.</param>{{/supportsAsync}}
/// <param name="configuration">The configuration object.</param>
public {{classname}}({{packageName}}.Client.ISynchronousClient client, {{#supportsAsync}}{{packageName}}.Client.IAsynchronousClient asyncClient, {{/supportsAsync}}{{packageName}}.Client.IReadableConfiguration configuration)
{
if (client == null) throw new ArgumentNullException("client");
{{#supportsAsync}}
if (asyncClient == null) throw new ArgumentNullException("asyncClient");
{{/supportsAsync}}
if (configuration == null) throw new ArgumentNullException("configuration");
this.Client = client;
{{#supportsAsync}}
this.AsynchronousClient = asyncClient;
{{/supportsAsync}}
this.Configuration = configuration;
this.ExceptionFactory = {{packageName}}.Client.Configuration.DefaultExceptionFactory;
}
/// <summary>
/// Disposes resources if they were created by us
/// </summary>
public void Dispose()
{
this.ApiClient?.Dispose();
}
/// <summary>
/// Holds the ApiClient if created
/// </summary>
public {{packageName}}.Client.ApiClient ApiClient { get; set; } = null;
{{#supportsAsync}}
/// <summary>
/// The client for accessing this underlying API asynchronously.
/// </summary>
public {{packageName}}.Client.IAsynchronousClient AsynchronousClient { get; set; }
{{/supportsAsync}}
/// <summary>
/// The client for accessing this underlying API synchronously.
/// </summary>
public {{packageName}}.Client.ISynchronousClient Client { get; set; }
/// <summary>
/// Gets the base path of the API client.
/// </summary>
/// <value>The base path</value>
public String GetBasePath()
{
return this.Configuration.BasePath;
}
/// <summary>
/// Gets or sets the configuration object
/// </summary>
/// <value>An instance of the Configuration</value>
public {{packageName}}.Client.IReadableConfiguration Configuration { get; set; }
/// <summary>
/// Provides a factory method hook for the creation of exceptions.
/// </summary>
public {{packageName}}.Client.ExceptionFactory ExceptionFactory
{
get
{
if (_exceptionFactory != null && _exceptionFactory.GetInvocationList().Length > 1)
{
throw new InvalidOperationException("Multicast delegate for ExceptionFactory is unsupported.");
}
return _exceptionFactory;
}
set { _exceptionFactory = value; }
}
{{#operation}}
/// <summary>
/// {{summary}} {{notes}}
/// </summary>
/// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
{{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
{{/allParams}}/// <returns>{{#returnType}}{{returnType}}{{/returnType}}</returns>
public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
{
{{#returnType}}{{packageName}}.Client.ApiResponse<{{{returnType}}}> localVarResponse = {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
return localVarResponse.Data;{{/returnType}}{{^returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});{{/returnType}}
}
/// <summary>
/// {{summary}} {{notes}}
/// </summary>
/// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
{{#allParams}}/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
{{/allParams}}/// <returns>ApiResponse of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Object(void){{/returnType}}</returns>
public {{packageName}}.Client.ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}> {{operationId}}WithHttpInfo({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
{
{{#allParams}}
{{#required}}
{{^vendorExtensions.x-csharp-value-type}}
// verify the required parameter '{{paramName}}' is set
if ({{paramName}} == null)
throw new {{packageName}}.Client.ApiException(400, "Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}");
{{/vendorExtensions.x-csharp-value-type}}
{{/required}}
{{/allParams}}
{{packageName}}.Client.RequestOptions localVarRequestOptions = new {{packageName}}.Client.RequestOptions();
String[] _contentTypes = new String[] {
{{#consumes}}
"{{{mediaType}}}"{{^-last}},{{/-last}}
{{/consumes}}
};
// to determine the Accept header
String[] _accepts = new String[] {
{{#produces}}
"{{{mediaType}}}"{{^-last}},{{/-last}}
{{/produces}}
};
var localVarContentType = {{packageName}}.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
if (localVarContentType != null) localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
var localVarAccept = {{packageName}}.Client.ClientUtils.SelectHeaderAccept(_accepts);
if (localVarAccept != null) localVarRequestOptions.HeaderParameters.Add("Accept", localVarAccept);
{{#pathParams}}
{{#required}}
localVarRequestOptions.PathParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // path parameter
{{/required}}
{{^required}}
if ({{paramName}} != null)
{
localVarRequestOptions.PathParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // path parameter
}
{{/required}}
{{/pathParams}}
{{#queryParams}}
{{#required}}
{{#isDeepObject}}
{{#items.vars}}
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}.{{name}}));
{{/items.vars}}
{{^items}}
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("deepObject", "{{baseName}}", {{paramName}}));
{{/items}}
{{/isDeepObject}}
{{^isDeepObject}}
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
{{/isDeepObject}}
{{/required}}
{{^required}}
if ({{paramName}} != null)
{
{{#isDeepObject}}
{{#items.vars}}
if ({{paramName}}.{{name}} != null)
{
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}.{{name}}));
}
{{/items.vars}}
{{^items}}
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("deepObject", "{{baseName}}", {{paramName}}));
{{/items}}
{{/isDeepObject}}
{{^isDeepObject}}
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
{{/isDeepObject}}
}
{{/required}}
{{/queryParams}}
{{#headerParams}}
{{#required}}
localVarRequestOptions.HeaderParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // header parameter
{{/required}}
{{^required}}
if ({{paramName}} != null)
{
localVarRequestOptions.HeaderParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // header parameter
}
{{/required}}
{{/headerParams}}
{{#formParams}}
{{#required}}
{{#isFile}}
localVarRequestOptions.FileParameters.Add("{{baseName}}", {{paramName}});
{{/isFile}}
{{^isFile}}
localVarRequestOptions.FormParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // form parameter
{{/isFile}}
{{/required}}
{{^required}}
if ({{paramName}} != null)
{
{{#isFile}}
localVarRequestOptions.FileParameters.Add("{{baseName}}", {{paramName}});
{{/isFile}}
{{^isFile}}
localVarRequestOptions.FormParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // form parameter
{{/isFile}}
}
{{/required}}
{{/formParams}}
{{#bodyParam}}
localVarRequestOptions.Data = {{paramName}};
{{/bodyParam}}
{{#authMethods}}
// authentication ({{name}}) required
{{#isApiKey}}
{{#isKeyInCookie}}
// cookie parameter support
if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
{
localVarRequestOptions.Cookies.Add(new Cookie("{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")));
}
{{/isKeyInCookie}}
{{#isKeyInHeader}}
if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
{
localVarRequestOptions.HeaderParameters.Add("{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"));
}
{{/isKeyInHeader}}
{{#isKeyInQuery}}
if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
{
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("", "{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")));
}
{{/isKeyInQuery}}
{{/isApiKey}}
{{#isBasicBasic}}
// http basic authentication required
if (!String.IsNullOrEmpty(this.Configuration.Username) || !String.IsNullOrEmpty(this.Configuration.Password))
{
localVarRequestOptions.HeaderParameters.Add("Authorization", "Basic " + {{packageName}}.Client.ClientUtils.Base64Encode(this.Configuration.Username + ":" + this.Configuration.Password));
}
{{/isBasicBasic}}
{{#isBasicBearer}}
// bearer authentication required
if (!String.IsNullOrEmpty(this.Configuration.AccessToken))
{
localVarRequestOptions.HeaderParameters.Add("Authorization", "Bearer " + this.Configuration.AccessToken);
}
{{/isBasicBearer}}
{{#isOAuth}}
// oauth required
if (!String.IsNullOrEmpty(this.Configuration.AccessToken))
{
localVarRequestOptions.HeaderParameters.Add("Authorization", "Bearer " + this.Configuration.AccessToken);
}
{{/isOAuth}}
{{#isHttpSignature}}
if (this.Configuration.HttpSigningConfiguration != null)
{
var HttpSigningHeaders = this.Configuration.HttpSigningConfiguration.GetHttpSignedHeader(this.Configuration.BasePath, "{{{httpMethod}}}", "{{{path}}}", localVarRequestOptions);
foreach (var headerItem in HttpSigningHeaders)
{
if (localVarRequestOptions.HeaderParameters.ContainsKey(headerItem.Key))
{
localVarRequestOptions.HeaderParameters[headerItem.Key] = new List<string>() { headerItem.Value };
}
else
{
localVarRequestOptions.HeaderParameters.Add(headerItem.Key, headerItem.Value);
}
}
}
{{/isHttpSignature}}
{{/authMethods}}
// make the HTTP request
var localVarResponse = this.Client.{{#lambda.titlecase}}{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}{{/lambda.titlecase}}<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}>("{{{path}}}", localVarRequestOptions, this.Configuration);
if (this.ExceptionFactory != null)
{
Exception _exception = this.ExceptionFactory("{{operationId}}", localVarResponse);
if (_exception != null) throw _exception;
}
return localVarResponse;
}
{{#supportsAsync}}
/// <summary>
/// {{summary}} {{notes}}
/// </summary>
/// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
{{#allParams}}
/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
{{/allParams}}
/// <param name="cancellationToken">Cancellation Token to cancel the request.</param>
/// <returns>Task of {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}void{{/returnType}}</returns>
{{#returnType}}public async System.Threading.Tasks.Task<{{{returnType}}}>{{/returnType}}{{^returnType}}public async System.Threading.Tasks.Task{{/returnType}} {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
{{#returnType}}{{packageName}}.Client.ApiResponse<{{{returnType}}}> localVarResponse = await {{operationId}}WithHttpInfoAsync({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}cancellationToken).ConfigureAwait(false);
return localVarResponse.Data;{{/returnType}}{{^returnType}}await {{operationId}}WithHttpInfoAsync({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}cancellationToken).ConfigureAwait(false);{{/returnType}}
}
/// <summary>
/// {{summary}} {{notes}}
/// </summary>
/// <exception cref="{{packageName}}.Client.ApiException">Thrown when fails to make API call</exception>
{{#allParams}}
/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
{{/allParams}}
/// <param name="cancellationToken">Cancellation Token to cancel the request.</param>
/// <returns>Task of ApiResponse{{#returnType}} ({{returnType}}){{/returnType}}</returns>
public async System.Threading.Tasks.Task<{{packageName}}.Client.ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}>> {{operationId}}WithHttpInfoAsync({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default({{{dataType}}}){{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
{{#allParams}}
{{#required}}
{{^vendorExtensions.x-csharp-value-type}}
// verify the required parameter '{{paramName}}' is set
if ({{paramName}} == null)
throw new {{packageName}}.Client.ApiException(400, "Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}");
{{/vendorExtensions.x-csharp-value-type}}
{{/required}}
{{/allParams}}
{{packageName}}.Client.RequestOptions localVarRequestOptions = new {{packageName}}.Client.RequestOptions();
String[] _contentTypes = new String[] {
{{#consumes}}
"{{{mediaType}}}"{{^-last}}, {{/-last}}
{{/consumes}}
};
// to determine the Accept header
String[] _accepts = new String[] {
{{#produces}}
"{{{mediaType}}}"{{^-last}},{{/-last}}
{{/produces}}
};
var localVarContentType = {{packageName}}.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
if (localVarContentType != null) localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
var localVarAccept = {{packageName}}.Client.ClientUtils.SelectHeaderAccept(_accepts);
if (localVarAccept != null) localVarRequestOptions.HeaderParameters.Add("Accept", localVarAccept);
{{#pathParams}}
{{#required}}
localVarRequestOptions.PathParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // path parameter
{{/required}}
{{^required}}
if ({{paramName}} != null)
{
localVarRequestOptions.PathParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // path parameter
}
{{/required}}
{{/pathParams}}
{{#queryParams}}
{{#required}}
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
{{/required}}
{{^required}}
if ({{paramName}} != null)
{
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
}
{{/required}}
{{/queryParams}}
{{#headerParams}}
{{#required}}
localVarRequestOptions.HeaderParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // header parameter
{{/required}}
{{^required}}
if ({{paramName}} != null)
{
localVarRequestOptions.HeaderParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // header parameter
}
{{/required}}
{{/headerParams}}
{{#formParams}}
{{#required}}
{{#isFile}}
localVarRequestOptions.FileParameters.Add("{{baseName}}", {{paramName}});
{{/isFile}}
{{^isFile}}
localVarRequestOptions.FormParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // form parameter
{{/isFile}}
{{/required}}
{{^required}}
if ({{paramName}} != null)
{
{{#isFile}}
localVarRequestOptions.FileParameters.Add("{{baseName}}", {{paramName}});
{{/isFile}}
{{^isFile}}
localVarRequestOptions.FormParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}})); // form parameter
{{/isFile}}
}
{{/required}}
{{/formParams}}
{{#bodyParam}}
localVarRequestOptions.Data = {{paramName}};
{{/bodyParam}}
{{#authMethods}}
// authentication ({{name}}) required
{{#isApiKey}}
{{#isKeyInCookie}}
// cookie parameter support
if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
{
localVarRequestOptions.Cookies.Add(new Cookie("{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")));
}
{{/isKeyInCookie}}
{{#isKeyInHeader}}
if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
{
localVarRequestOptions.HeaderParameters.Add("{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"));
}
{{/isKeyInHeader}}
{{#isKeyInQuery}}
if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")))
{
localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("", "{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")));
}
{{/isKeyInQuery}}
{{/isApiKey}}
{{#isBasic}}
{{#isBasicBasic}}
// http basic authentication required
if (!String.IsNullOrEmpty(this.Configuration.Username) || !String.IsNullOrEmpty(this.Configuration.Password))
{
localVarRequestOptions.HeaderParameters.Add("Authorization", "Basic " + {{packageName}}.Client.ClientUtils.Base64Encode(this.Configuration.Username + ":" + this.Configuration.Password));
}
{{/isBasicBasic}}
{{#isBasicBearer}}
// bearer authentication required
if (!String.IsNullOrEmpty(this.Configuration.AccessToken))
{
localVarRequestOptions.HeaderParameters.Add("Authorization", "Bearer " + this.Configuration.AccessToken);
}
{{/isBasicBearer}}
{{/isBasic}}
{{#isOAuth}}
// oauth required
if (!String.IsNullOrEmpty(this.Configuration.AccessToken))
{
localVarRequestOptions.HeaderParameters.Add("Authorization", "Bearer " + this.Configuration.AccessToken);
}
{{/isOAuth}}
{{#isHttpSignature}}
if (this.Configuration.HttpSigningConfiguration != null)
{
var HttpSigningHeaders = this.Configuration.HttpSigningConfiguration.GetHttpSignedHeader(this.Configuration.BasePath, "{{{httpMethod}}}", "{{{path}}}", localVarRequestOptions);
foreach (var headerItem in HttpSigningHeaders)
{
if (localVarRequestOptions.HeaderParameters.ContainsKey(headerItem.Key))
{
localVarRequestOptions.HeaderParameters[headerItem.Key] = new List<string>() { headerItem.Value };
}
else
{
localVarRequestOptions.HeaderParameters.Add(headerItem.Key, headerItem.Value);
}
}
}
{{/isHttpSignature}}
{{/authMethods}}
// make the HTTP request
var localVarResponse = await this.AsynchronousClient.{{#lambda.titlecase}}{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}{{/lambda.titlecase}}Async<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}>("{{{path}}}", localVarRequestOptions, this.Configuration, cancellationToken).ConfigureAwait(false);
if (this.ExceptionFactory != null)
{
Exception _exception = this.ExceptionFactory("{{operationId}}", localVarResponse);
if (_exception != null) throw _exception;
}
return localVarResponse;
}
{{/supportsAsync}}
{{/operation}}
}
{{/operations}}
}

View File

@@ -50,6 +50,27 @@ webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
c.Proxy = webProxy;
```
To use your own HttpClient instances just pass them to the ApiClass constructor.
```csharp
HttpClientHandler yourHandler = new HttpClientHandler();
HttpClient yourHttpClient = new HttpClient(yourHandler);
var api = new YourApiClass(yourHttpClient, yourHandler);
```
If you want to use an HttpClient and don't have access to the handler, for example in a DI context in aspnetcore when
using IHttpClientFactory. You need to disable the features that require handler access:
```csharp
HttpClient yourHttpClient = new HttpClient();
var api = new YourApiClass(yourHttpClient, null, true);
```
The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings.
You need to either manually handle those in your setup of the HttpClient or they won't be available.
<a name="getting-started"></a>
## Getting Started

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;
@@ -93,30 +94,38 @@ namespace Org.OpenAPITools.Api
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
public partial class AnotherFakeApi : IAnotherFakeApi
public partial class AnotherFakeApi : IDisposable, IAnotherFakeApi
{
private Org.OpenAPITools.Client.ExceptionFactory _exceptionFactory = (name, response) => null;
/// <summary>
/// Initializes a new instance of the <see cref="AnotherFakeApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public AnotherFakeApi() : this((string)null)
public AnotherFakeApi(HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false) : this((string)null, client, handler, disableHandlerFeatures)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AnotherFakeApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public AnotherFakeApi(String basePath)
public AnotherFakeApi(String basePath, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
this.Configuration = Org.OpenAPITools.Client.Configuration.MergeConfigurations(
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
new Org.OpenAPITools.Client.Configuration { BasePath = basePath }
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -125,8 +134,11 @@ namespace Org.OpenAPITools.Api
/// using Configuration object
/// </summary>
/// <param name="configuration">An instance of Configuration</param>
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public AnotherFakeApi(Org.OpenAPITools.Client.Configuration configuration)
public AnotherFakeApi(Org.OpenAPITools.Client.Configuration configuration, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
if (configuration == null) throw new ArgumentNullException("configuration");
@@ -134,8 +146,9 @@ namespace Org.OpenAPITools.Api
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
configuration
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -158,6 +171,19 @@ namespace Org.OpenAPITools.Api
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
/// <summary>
/// Disposes resources if they were created by us
/// </summary>
public void Dispose()
{
this.ApiClient?.Dispose();
}
/// <summary>
/// Holds the ApiClient if created
/// </summary>
public Org.OpenAPITools.Client.ApiClient ApiClient { get; set; } = null;
/// <summary>
/// The client for accessing this underlying API asynchronously.
/// </summary>

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;
@@ -86,30 +87,38 @@ namespace Org.OpenAPITools.Api
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
public partial class DefaultApi : IDefaultApi
public partial class DefaultApi : IDisposable, IDefaultApi
{
private Org.OpenAPITools.Client.ExceptionFactory _exceptionFactory = (name, response) => null;
/// <summary>
/// Initializes a new instance of the <see cref="DefaultApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public DefaultApi() : this((string)null)
public DefaultApi(HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false) : this((string)null, client, handler, disableHandlerFeatures)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DefaultApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public DefaultApi(String basePath)
public DefaultApi(String basePath, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
this.Configuration = Org.OpenAPITools.Client.Configuration.MergeConfigurations(
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
new Org.OpenAPITools.Client.Configuration { BasePath = basePath }
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -118,8 +127,11 @@ namespace Org.OpenAPITools.Api
/// using Configuration object
/// </summary>
/// <param name="configuration">An instance of Configuration</param>
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public DefaultApi(Org.OpenAPITools.Client.Configuration configuration)
public DefaultApi(Org.OpenAPITools.Client.Configuration configuration, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
if (configuration == null) throw new ArgumentNullException("configuration");
@@ -127,8 +139,9 @@ namespace Org.OpenAPITools.Api
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
configuration
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -151,6 +164,19 @@ namespace Org.OpenAPITools.Api
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
/// <summary>
/// Disposes resources if they were created by us
/// </summary>
public void Dispose()
{
this.ApiClient?.Dispose();
}
/// <summary>
/// Holds the ApiClient if created
/// </summary>
public Org.OpenAPITools.Client.ApiClient ApiClient { get; set; } = null;
/// <summary>
/// The client for accessing this underlying API asynchronously.
/// </summary>

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;
@@ -810,30 +811,38 @@ namespace Org.OpenAPITools.Api
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
public partial class FakeApi : IFakeApi
public partial class FakeApi : IDisposable, IFakeApi
{
private Org.OpenAPITools.Client.ExceptionFactory _exceptionFactory = (name, response) => null;
/// <summary>
/// Initializes a new instance of the <see cref="FakeApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public FakeApi() : this((string)null)
public FakeApi(HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false) : this((string)null, client, handler, disableHandlerFeatures)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FakeApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public FakeApi(String basePath)
public FakeApi(String basePath, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
this.Configuration = Org.OpenAPITools.Client.Configuration.MergeConfigurations(
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
new Org.OpenAPITools.Client.Configuration { BasePath = basePath }
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -842,8 +851,11 @@ namespace Org.OpenAPITools.Api
/// using Configuration object
/// </summary>
/// <param name="configuration">An instance of Configuration</param>
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public FakeApi(Org.OpenAPITools.Client.Configuration configuration)
public FakeApi(Org.OpenAPITools.Client.Configuration configuration, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
if (configuration == null) throw new ArgumentNullException("configuration");
@@ -851,8 +863,9 @@ namespace Org.OpenAPITools.Api
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
configuration
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -875,6 +888,19 @@ namespace Org.OpenAPITools.Api
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
/// <summary>
/// Disposes resources if they were created by us
/// </summary>
public void Dispose()
{
this.ApiClient?.Dispose();
}
/// <summary>
/// Holds the ApiClient if created
/// </summary>
public Org.OpenAPITools.Client.ApiClient ApiClient { get; set; } = null;
/// <summary>
/// The client for accessing this underlying API asynchronously.
/// </summary>

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;
@@ -93,30 +94,38 @@ namespace Org.OpenAPITools.Api
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
public partial class FakeClassnameTags123Api : IFakeClassnameTags123Api
public partial class FakeClassnameTags123Api : IDisposable, IFakeClassnameTags123Api
{
private Org.OpenAPITools.Client.ExceptionFactory _exceptionFactory = (name, response) => null;
/// <summary>
/// Initializes a new instance of the <see cref="FakeClassnameTags123Api"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public FakeClassnameTags123Api() : this((string)null)
public FakeClassnameTags123Api(HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false) : this((string)null, client, handler, disableHandlerFeatures)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FakeClassnameTags123Api"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public FakeClassnameTags123Api(String basePath)
public FakeClassnameTags123Api(String basePath, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
this.Configuration = Org.OpenAPITools.Client.Configuration.MergeConfigurations(
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
new Org.OpenAPITools.Client.Configuration { BasePath = basePath }
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -125,8 +134,11 @@ namespace Org.OpenAPITools.Api
/// using Configuration object
/// </summary>
/// <param name="configuration">An instance of Configuration</param>
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public FakeClassnameTags123Api(Org.OpenAPITools.Client.Configuration configuration)
public FakeClassnameTags123Api(Org.OpenAPITools.Client.Configuration configuration, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
if (configuration == null) throw new ArgumentNullException("configuration");
@@ -134,8 +146,9 @@ namespace Org.OpenAPITools.Api
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
configuration
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -158,6 +171,19 @@ namespace Org.OpenAPITools.Api
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
/// <summary>
/// Disposes resources if they were created by us
/// </summary>
public void Dispose()
{
this.ApiClient?.Dispose();
}
/// <summary>
/// Holds the ApiClient if created
/// </summary>
public Org.OpenAPITools.Client.ApiClient ApiClient { get; set; } = null;
/// <summary>
/// The client for accessing this underlying API asynchronously.
/// </summary>

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;
@@ -455,30 +456,38 @@ namespace Org.OpenAPITools.Api
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
public partial class PetApi : IPetApi
public partial class PetApi : IDisposable, IPetApi
{
private Org.OpenAPITools.Client.ExceptionFactory _exceptionFactory = (name, response) => null;
/// <summary>
/// Initializes a new instance of the <see cref="PetApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public PetApi() : this((string)null)
public PetApi(HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false) : this((string)null, client, handler, disableHandlerFeatures)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PetApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public PetApi(String basePath)
public PetApi(String basePath, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
this.Configuration = Org.OpenAPITools.Client.Configuration.MergeConfigurations(
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
new Org.OpenAPITools.Client.Configuration { BasePath = basePath }
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -487,8 +496,11 @@ namespace Org.OpenAPITools.Api
/// using Configuration object
/// </summary>
/// <param name="configuration">An instance of Configuration</param>
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public PetApi(Org.OpenAPITools.Client.Configuration configuration)
public PetApi(Org.OpenAPITools.Client.Configuration configuration, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
if (configuration == null) throw new ArgumentNullException("configuration");
@@ -496,8 +508,9 @@ namespace Org.OpenAPITools.Api
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
configuration
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -520,6 +533,19 @@ namespace Org.OpenAPITools.Api
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
/// <summary>
/// Disposes resources if they were created by us
/// </summary>
public void Dispose()
{
this.ApiClient?.Dispose();
}
/// <summary>
/// Holds the ApiClient if created
/// </summary>
public Org.OpenAPITools.Client.ApiClient ApiClient { get; set; } = null;
/// <summary>
/// The client for accessing this underlying API asynchronously.
/// </summary>

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;
@@ -218,30 +219,38 @@ namespace Org.OpenAPITools.Api
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
public partial class StoreApi : IStoreApi
public partial class StoreApi : IDisposable, IStoreApi
{
private Org.OpenAPITools.Client.ExceptionFactory _exceptionFactory = (name, response) => null;
/// <summary>
/// Initializes a new instance of the <see cref="StoreApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public StoreApi() : this((string)null)
public StoreApi(HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false) : this((string)null, client, handler, disableHandlerFeatures)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="StoreApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public StoreApi(String basePath)
public StoreApi(String basePath, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
this.Configuration = Org.OpenAPITools.Client.Configuration.MergeConfigurations(
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
new Org.OpenAPITools.Client.Configuration { BasePath = basePath }
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -250,8 +259,11 @@ namespace Org.OpenAPITools.Api
/// using Configuration object
/// </summary>
/// <param name="configuration">An instance of Configuration</param>
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public StoreApi(Org.OpenAPITools.Client.Configuration configuration)
public StoreApi(Org.OpenAPITools.Client.Configuration configuration, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
if (configuration == null) throw new ArgumentNullException("configuration");
@@ -259,8 +271,9 @@ namespace Org.OpenAPITools.Api
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
configuration
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -283,6 +296,19 @@ namespace Org.OpenAPITools.Api
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
/// <summary>
/// Disposes resources if they were created by us
/// </summary>
public void Dispose()
{
this.ApiClient?.Dispose();
}
/// <summary>
/// Holds the ApiClient if created
/// </summary>
public Org.OpenAPITools.Client.ApiClient ApiClient { get; set; } = null;
/// <summary>
/// The client for accessing this underlying API asynchronously.
/// </summary>

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;
@@ -390,30 +391,38 @@ namespace Org.OpenAPITools.Api
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
public partial class UserApi : IUserApi
public partial class UserApi : IDisposable, IUserApi
{
private Org.OpenAPITools.Client.ExceptionFactory _exceptionFactory = (name, response) => null;
/// <summary>
/// Initializes a new instance of the <see cref="UserApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public UserApi() : this((string)null)
public UserApi(HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false) : this((string)null, client, handler, disableHandlerFeatures)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="UserApi"/> 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="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public UserApi(String basePath)
public UserApi(String basePath, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
this.Configuration = Org.OpenAPITools.Client.Configuration.MergeConfigurations(
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
new Org.OpenAPITools.Client.Configuration { BasePath = basePath }
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -422,8 +431,11 @@ namespace Org.OpenAPITools.Api
/// using Configuration object
/// </summary>
/// <param name="configuration">An instance of Configuration</param>
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <returns></returns>
public UserApi(Org.OpenAPITools.Client.Configuration configuration)
public UserApi(Org.OpenAPITools.Client.Configuration configuration, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
if (configuration == null) throw new ArgumentNullException("configuration");
@@ -431,8 +443,9 @@ namespace Org.OpenAPITools.Api
Org.OpenAPITools.Client.GlobalConfiguration.Instance,
configuration
);
this.Client = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.AsynchronousClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath);
this.ApiClient = new Org.OpenAPITools.Client.ApiClient(this.Configuration.BasePath, client, handler, disableHandlerFeatures);
this.Client = this.ApiClient;
this.AsynchronousClient = this.ApiClient;
ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
@@ -455,6 +468,19 @@ namespace Org.OpenAPITools.Api
this.ExceptionFactory = Org.OpenAPITools.Client.Configuration.DefaultExceptionFactory;
}
/// <summary>
/// Disposes resources if they were created by us
/// </summary>
public void Dispose()
{
this.ApiClient?.Dispose();
}
/// <summary>
/// Holds the ApiClient if created
/// </summary>
public Org.OpenAPITools.Client.ApiClient ApiClient { get; set; } = null;
/// <summary>
/// The client for accessing this underlying API asynchronously.
/// </summary>

View File

@@ -161,10 +161,17 @@ namespace Org.OpenAPITools.Client
/// Provides a default implementation of an Api client (both synchronous and asynchronous implementatios),
/// encapsulating general REST accessor use cases.
/// </summary>
public partial class ApiClient : ISynchronousClient, IAsynchronousClient
public partial class ApiClient : IDisposable, ISynchronousClient, IAsynchronousClient
{
private readonly String _baseUrl;
private readonly HttpClientHandler _httpClientHandler;
private readonly bool _disposeHandler;
private readonly HttpClient _httpClient;
private readonly bool _disposeClient;
private readonly bool _disableHandlerFeatures;
/// <summary>
/// Specifies the settings on a <see cref="JsonSerializer" /> object.
/// These settings can be adjusted to accomodate custom serialization rules.
@@ -185,22 +192,50 @@ namespace Org.OpenAPITools.Client
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" />, defaulting to the global configurations' base url.
/// </summary>
public ApiClient()
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
public ApiClient(HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false) :
this(Org.OpenAPITools.Client.GlobalConfiguration.Instance.BasePath, client, handler, disableHandlerFeatures)
{
_baseUrl = Org.OpenAPITools.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>
/// <param name="client">An instance of HttpClient</param>
/// <param name="handler">An instance of HttpClientHandler that is used by HttpClient</param>
/// <param name="disableHandlerFeatures">Disable ApiClient features that require access to the HttpClientHandler</param>
/// <exception cref="ArgumentException"></exception>
public ApiClient(String basePath)
public ApiClient(String basePath, HttpClient client = null, HttpClientHandler handler = null, bool disableHandlerFeatures = false)
{
if (string.IsNullOrEmpty(basePath))
throw new ArgumentException("basePath cannot be empty");
_baseUrl = basePath;
if((client != null && handler == null) && !disableHandlerFeatures) {
throw new ArgumentException("If providing HttpClient, you also need to provide its handler or disable features requiring the handler, see README.md");
}
_disableHandlerFeatures = disableHandlerFeatures;
_httpClientHandler = handler ?? new HttpClientHandler();
_disposeHandler = handler == null;
_httpClient = client ?? new HttpClient(_httpClientHandler, false);
_disposeClient = client == null;
}
/// <summary>
/// Disposes resources if they were created by us
/// </summary>
public void Dispose()
{
if(_disposeClient) {
_httpClient.Dispose();
}
if(_disposeHandler) {
_httpClientHandler.Dispose();
}
}
/// Prepares multipart/form-data content
@@ -262,6 +297,11 @@ namespace Org.OpenAPITools.Client
HttpRequestMessage request = new HttpRequestMessage(method, builder.GetFullUri());
if (configuration.UserAgent != null)
{
request.Headers.TryAddWithoutValidation("User-Agent", configuration.UserAgent);
}
if (configuration.DefaultHeaders != null)
{
foreach (var headerParam in configuration.DefaultHeaders)
@@ -363,15 +403,18 @@ namespace Org.OpenAPITools.Client
}
}
if (response != null)
if(!_disableHandlerFeatures)
{
try {
foreach (Cookie cookie in handler.CookieContainer.GetCookies(uri))
{
transformed.Cookies.Add(cookie);
if (response != null)
{
try {
foreach (Cookie cookie in handler.CookieContainer.GetCookies(uri))
{
transformed.Cookies.Add(cookie);
}
}
catch (PlatformNotSupportedException) {}
}
catch (PlatformNotSupportedException) {}
}
return transformed;
@@ -386,8 +429,8 @@ namespace Org.OpenAPITools.Client
IReadableConfiguration configuration,
System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
var handler = new HttpClientHandler();
var client = new HttpClient();
var handler = _httpClientHandler;
var client = _httpClient;
var deserializer = new CustomJsonCodec(SerializerSettings, configuration);
var finalToken = cancellationToken;
@@ -397,20 +440,16 @@ namespace Org.OpenAPITools.Client
var tokenSource = new CancellationTokenSource(configuration.Timeout);
finalToken = CancellationTokenSource.CreateLinkedTokenSource(finalToken, tokenSource.Token).Token;
}
if(!_disableHandlerFeatures) {
if (configuration.Proxy != null)
{
handler.Proxy = configuration.Proxy;
}
if (configuration.Proxy != null)
{
handler.Proxy = configuration.Proxy;
}
if (configuration.UserAgent != null)
{
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", configuration.UserAgent);
}
if (configuration.ClientCertificates != null)
{
handler.ClientCertificates.AddRange(configuration.ClientCertificates);
if (configuration.ClientCertificates != null)
{
handler.ClientCertificates.AddRange(configuration.ClientCertificates);
}
}
var cookieContainer = req.Properties.ContainsKey("CookieContainer") ? req.Properties["CookieContainer"] as List<Cookie> : null;