[C#][client][csharp-netcore] Fix csharp netcore defaultheaders (#3562)

* [csharp-netcore] Send default HTTP headers with requests

* [csharp-netcore] Add DefaultHeaders in favor of DefaultHeader
This commit is contained in:
Mario De Schaepmeester 2019-08-08 18:24:47 +02:00 committed by William Cheng
parent 752030890f
commit fbb2f1e05a
9 changed files with 132 additions and 36 deletions

View File

@ -270,6 +270,14 @@ namespace {{packageName}}.Client
} }
} }
if (configuration.DefaultHeaders != null)
{
foreach (var headerParam in configuration.DefaultHeaders)
{
request.AddHeader(headerParam.Key, headerParam.Value);
}
}
if (options.HeaderParameters != null) if (options.HeaderParameters != null)
{ {
foreach (var headerParam in options.HeaderParameters) foreach (var headerParam in options.HeaderParameters)

View File

@ -35,7 +35,7 @@ namespace {{packageName}}.Client
#endregion Constants #endregion Constants
#region Static Members #region Static Members
/// <summary> /// <summary>
/// Default creation of exceptions for a given method name and response object /// Default creation of exceptions for a given method name and response object
/// </summary> /// </summary>
@ -65,7 +65,7 @@ namespace {{packageName}}.Client
/// Example: http://localhost:3000/v1/ /// Example: http://localhost:3000/v1/
/// </summary> /// </summary>
private String _basePath; private String _basePath;
/// <summary> /// <summary>
/// Gets or sets the API key based on the authentication name. /// Gets or sets the API key based on the authentication name.
/// This is the key and value comprising the "secret" for acessing an API. /// This is the key and value comprising the "secret" for acessing an API.
@ -94,7 +94,7 @@ namespace {{packageName}}.Client
{ {
UserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{packageVersion}}/csharp{{/httpUserAgent}}"; UserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{packageVersion}}/csharp{{/httpUserAgent}}";
BasePath = "{{{basePath}}}"; BasePath = "{{{basePath}}}";
DefaultHeader = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>(); DefaultHeaders = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>();
ApiKey = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>(); ApiKey = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>();
ApiKeyPrefix = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>(); ApiKeyPrefix = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>();
@ -107,15 +107,15 @@ namespace {{packageName}}.Client
/// </summary> /// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
public Configuration( public Configuration(
IDictionary<string, string> defaultHeader, IDictionary<string, string> defaultHeaders,
IDictionary<string, string> apiKey, IDictionary<string, string> apiKey,
IDictionary<string, string> apiKeyPrefix, IDictionary<string, string> apiKeyPrefix,
string basePath = "{{{basePath}}}") : this() string basePath = "{{{basePath}}}") : this()
{ {
if (string.{{^net35}}IsNullOrWhiteSpace{{/net35}}{{#net35}}IsNullOrEmpty{{/net35}}(basePath)) if (string.{{^net35}}IsNullOrWhiteSpace{{/net35}}{{#net35}}IsNullOrEmpty{{/net35}}(basePath))
throw new ArgumentException("The provided basePath is invalid.", "basePath"); throw new ArgumentException("The provided basePath is invalid.", "basePath");
if (defaultHeader == null) if (defaultHeaders == null)
throw new ArgumentNullException("defaultHeader"); throw new ArgumentNullException("defaultHeaders");
if (apiKey == null) if (apiKey == null)
throw new ArgumentNullException("apiKey"); throw new ArgumentNullException("apiKey");
if (apiKeyPrefix == null) if (apiKeyPrefix == null)
@ -123,9 +123,9 @@ namespace {{packageName}}.Client
BasePath = basePath; BasePath = basePath;
foreach (var keyValuePair in defaultHeader) foreach (var keyValuePair in defaultHeaders)
{ {
DefaultHeader.Add(keyValuePair); DefaultHeaders.Add(keyValuePair);
} }
foreach (var keyValuePair in apiKey) foreach (var keyValuePair in apiKey)
@ -156,7 +156,23 @@ namespace {{packageName}}.Client
/// <summary> /// <summary>
/// Gets or sets the default header. /// Gets or sets the default header.
/// </summary> /// </summary>
public virtual IDictionary<string, string> DefaultHeader { get; set; } [Obsolete("Use DefaultHeaders instead.")]
public virtual IDictionary<string, string> DefaultHeader
{
get
{
return DefaultHeaders;
}
set
{
DefaultHeaders = value;
}
}
/// <summary>
/// Gets or sets the default headers.
/// </summary>
public virtual IDictionary<string, string> DefaultHeaders { get; set; }
/// <summary> /// <summary>
/// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds. /// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds.
@ -374,17 +390,17 @@ namespace {{packageName}}.Client
Dictionary<string, string> apiKey = first.ApiKey.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary<string, string> apiKey = first.ApiKey.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Dictionary<string, string> apiKeyPrefix = first.ApiKeyPrefix.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary<string, string> apiKeyPrefix = first.ApiKeyPrefix.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Dictionary<string, string> defaultHeader = first.DefaultHeader.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary<string, string> defaultHeaders = first.DefaultHeaders.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
foreach (var kvp in second.ApiKey) apiKey[kvp.Key] = kvp.Value; foreach (var kvp in second.ApiKey) apiKey[kvp.Key] = kvp.Value;
foreach (var kvp in second.ApiKeyPrefix) apiKeyPrefix[kvp.Key] = kvp.Value; foreach (var kvp in second.ApiKeyPrefix) apiKeyPrefix[kvp.Key] = kvp.Value;
foreach (var kvp in second.DefaultHeader) defaultHeader[kvp.Key] = kvp.Value; foreach (var kvp in second.DefaultHeaders) defaultHeaders[kvp.Key] = kvp.Value;
var config = new Configuration var config = new Configuration
{ {
ApiKey = apiKey, ApiKey = apiKey,
ApiKeyPrefix = apiKeyPrefix, ApiKeyPrefix = apiKeyPrefix,
DefaultHeader = defaultHeader, DefaultHeader = defaultHeaders,
BasePath = second.BasePath ?? first.BasePath, BasePath = second.BasePath ?? first.BasePath,
Timeout = second.Timeout, Timeout = second.Timeout,
UserAgent = second.UserAgent ?? first.UserAgent, UserAgent = second.UserAgent ?? first.UserAgent,

View File

@ -1,5 +1,6 @@
{{>partial_header}} {{>partial_header}}
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace {{packageName}}.Client namespace {{packageName}}.Client
@ -43,8 +44,15 @@ namespace {{packageName}}.Client
/// Gets the default header. /// Gets the default header.
/// </summary> /// </summary>
/// <value>Default header.</value> /// <value>Default header.</value>
[Obsolete("Use DefaultHeaders instead.")]
IDictionary<string, string> DefaultHeader { get; } IDictionary<string, string> DefaultHeader { get; }
/// <summary>
/// Gets the default headers.
/// </summary>
/// <value>Default headers.</value>
IDictionary<string, string> DefaultHeaders { get; }
/// <summary> /// <summary>
/// Gets the temp folder path. /// Gets the temp folder path.
/// </summary> /// </summary>

View File

@ -273,6 +273,14 @@ namespace Org.OpenAPITools.Client
} }
} }
if (configuration.DefaultHeaders != null)
{
foreach (var headerParam in configuration.DefaultHeaders)
{
request.AddHeader(headerParam.Key, headerParam.Value);
}
}
if (options.HeaderParameters != null) if (options.HeaderParameters != null)
{ {
foreach (var headerParam in options.HeaderParameters) foreach (var headerParam in options.HeaderParameters)

View File

@ -42,7 +42,7 @@ namespace Org.OpenAPITools.Client
#endregion Constants #endregion Constants
#region Static Members #region Static Members
/// <summary> /// <summary>
/// Default creation of exceptions for a given method name and response object /// Default creation of exceptions for a given method name and response object
/// </summary> /// </summary>
@ -68,7 +68,7 @@ namespace Org.OpenAPITools.Client
/// Example: http://localhost:3000/v1/ /// Example: http://localhost:3000/v1/
/// </summary> /// </summary>
private String _basePath; private String _basePath;
/// <summary> /// <summary>
/// Gets or sets the API key based on the authentication name. /// Gets or sets the API key based on the authentication name.
/// This is the key and value comprising the "secret" for acessing an API. /// This is the key and value comprising the "secret" for acessing an API.
@ -97,7 +97,7 @@ namespace Org.OpenAPITools.Client
{ {
UserAgent = "OpenAPI-Generator/1.0.0/csharp"; UserAgent = "OpenAPI-Generator/1.0.0/csharp";
BasePath = "http://petstore.swagger.io:80/v2"; BasePath = "http://petstore.swagger.io:80/v2";
DefaultHeader = new ConcurrentDictionary<string, string>(); DefaultHeaders = new ConcurrentDictionary<string, string>();
ApiKey = new ConcurrentDictionary<string, string>(); ApiKey = new ConcurrentDictionary<string, string>();
ApiKeyPrefix = new ConcurrentDictionary<string, string>(); ApiKeyPrefix = new ConcurrentDictionary<string, string>();
@ -110,15 +110,15 @@ namespace Org.OpenAPITools.Client
/// </summary> /// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
public Configuration( public Configuration(
IDictionary<string, string> defaultHeader, IDictionary<string, string> defaultHeaders,
IDictionary<string, string> apiKey, IDictionary<string, string> apiKey,
IDictionary<string, string> apiKeyPrefix, IDictionary<string, string> apiKeyPrefix,
string basePath = "http://petstore.swagger.io:80/v2") : this() string basePath = "http://petstore.swagger.io:80/v2") : this()
{ {
if (string.IsNullOrWhiteSpace(basePath)) if (string.IsNullOrWhiteSpace(basePath))
throw new ArgumentException("The provided basePath is invalid.", "basePath"); throw new ArgumentException("The provided basePath is invalid.", "basePath");
if (defaultHeader == null) if (defaultHeaders == null)
throw new ArgumentNullException("defaultHeader"); throw new ArgumentNullException("defaultHeaders");
if (apiKey == null) if (apiKey == null)
throw new ArgumentNullException("apiKey"); throw new ArgumentNullException("apiKey");
if (apiKeyPrefix == null) if (apiKeyPrefix == null)
@ -126,9 +126,9 @@ namespace Org.OpenAPITools.Client
BasePath = basePath; BasePath = basePath;
foreach (var keyValuePair in defaultHeader) foreach (var keyValuePair in defaultHeaders)
{ {
DefaultHeader.Add(keyValuePair); DefaultHeaders.Add(keyValuePair);
} }
foreach (var keyValuePair in apiKey) foreach (var keyValuePair in apiKey)
@ -159,7 +159,23 @@ namespace Org.OpenAPITools.Client
/// <summary> /// <summary>
/// Gets or sets the default header. /// Gets or sets the default header.
/// </summary> /// </summary>
public virtual IDictionary<string, string> DefaultHeader { get; set; } [Obsolete("Use DefaultHeaders instead.")]
public virtual IDictionary<string, string> DefaultHeader
{
get
{
return DefaultHeaders;
}
set
{
DefaultHeaders = value;
}
}
/// <summary>
/// Gets or sets the default headers.
/// </summary>
public virtual IDictionary<string, string> DefaultHeaders { get; set; }
/// <summary> /// <summary>
/// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds. /// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds.
@ -369,17 +385,17 @@ namespace Org.OpenAPITools.Client
Dictionary<string, string> apiKey = first.ApiKey.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary<string, string> apiKey = first.ApiKey.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Dictionary<string, string> apiKeyPrefix = first.ApiKeyPrefix.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary<string, string> apiKeyPrefix = first.ApiKeyPrefix.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Dictionary<string, string> defaultHeader = first.DefaultHeader.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary<string, string> defaultHeaders = first.DefaultHeaders.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
foreach (var kvp in second.ApiKey) apiKey[kvp.Key] = kvp.Value; foreach (var kvp in second.ApiKey) apiKey[kvp.Key] = kvp.Value;
foreach (var kvp in second.ApiKeyPrefix) apiKeyPrefix[kvp.Key] = kvp.Value; foreach (var kvp in second.ApiKeyPrefix) apiKeyPrefix[kvp.Key] = kvp.Value;
foreach (var kvp in second.DefaultHeader) defaultHeader[kvp.Key] = kvp.Value; foreach (var kvp in second.DefaultHeaders) defaultHeaders[kvp.Key] = kvp.Value;
var config = new Configuration var config = new Configuration
{ {
ApiKey = apiKey, ApiKey = apiKey,
ApiKeyPrefix = apiKeyPrefix, ApiKeyPrefix = apiKeyPrefix,
DefaultHeader = defaultHeader, DefaultHeader = defaultHeaders,
BasePath = second.BasePath ?? first.BasePath, BasePath = second.BasePath ?? first.BasePath,
Timeout = second.Timeout, Timeout = second.Timeout,
UserAgent = second.UserAgent ?? first.UserAgent, UserAgent = second.UserAgent ?? first.UserAgent,

View File

@ -9,6 +9,7 @@
*/ */
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Org.OpenAPITools.Client namespace Org.OpenAPITools.Client
@ -52,8 +53,15 @@ namespace Org.OpenAPITools.Client
/// Gets the default header. /// Gets the default header.
/// </summary> /// </summary>
/// <value>Default header.</value> /// <value>Default header.</value>
[Obsolete("Use DefaultHeaders instead.")]
IDictionary<string, string> DefaultHeader { get; } IDictionary<string, string> DefaultHeader { get; }
/// <summary>
/// Gets the default headers.
/// </summary>
/// <value>Default headers.</value>
IDictionary<string, string> DefaultHeaders { get; }
/// <summary> /// <summary>
/// Gets the temp folder path. /// Gets the temp folder path.
/// </summary> /// </summary>

View File

@ -274,6 +274,14 @@ namespace Org.OpenAPITools.Client
} }
} }
if (configuration.DefaultHeaders != null)
{
foreach (var headerParam in configuration.DefaultHeaders)
{
request.AddHeader(headerParam.Key, headerParam.Value);
}
}
if (options.HeaderParameters != null) if (options.HeaderParameters != null)
{ {
foreach (var headerParam in options.HeaderParameters) foreach (var headerParam in options.HeaderParameters)

View File

@ -42,7 +42,7 @@ namespace Org.OpenAPITools.Client
#endregion Constants #endregion Constants
#region Static Members #region Static Members
/// <summary> /// <summary>
/// Default creation of exceptions for a given method name and response object /// Default creation of exceptions for a given method name and response object
/// </summary> /// </summary>
@ -72,7 +72,7 @@ namespace Org.OpenAPITools.Client
/// Example: http://localhost:3000/v1/ /// Example: http://localhost:3000/v1/
/// </summary> /// </summary>
private String _basePath; private String _basePath;
/// <summary> /// <summary>
/// Gets or sets the API key based on the authentication name. /// Gets or sets the API key based on the authentication name.
/// This is the key and value comprising the "secret" for acessing an API. /// This is the key and value comprising the "secret" for acessing an API.
@ -101,7 +101,7 @@ namespace Org.OpenAPITools.Client
{ {
UserAgent = "OpenAPI-Generator/1.0.0/csharp"; UserAgent = "OpenAPI-Generator/1.0.0/csharp";
BasePath = "http://petstore.swagger.io:80/v2"; BasePath = "http://petstore.swagger.io:80/v2";
DefaultHeader = new ConcurrentDictionary<string, string>(); DefaultHeaders = new ConcurrentDictionary<string, string>();
ApiKey = new ConcurrentDictionary<string, string>(); ApiKey = new ConcurrentDictionary<string, string>();
ApiKeyPrefix = new ConcurrentDictionary<string, string>(); ApiKeyPrefix = new ConcurrentDictionary<string, string>();
@ -114,15 +114,15 @@ namespace Org.OpenAPITools.Client
/// </summary> /// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
public Configuration( public Configuration(
IDictionary<string, string> defaultHeader, IDictionary<string, string> defaultHeaders,
IDictionary<string, string> apiKey, IDictionary<string, string> apiKey,
IDictionary<string, string> apiKeyPrefix, IDictionary<string, string> apiKeyPrefix,
string basePath = "http://petstore.swagger.io:80/v2") : this() string basePath = "http://petstore.swagger.io:80/v2") : this()
{ {
if (string.IsNullOrWhiteSpace(basePath)) if (string.IsNullOrWhiteSpace(basePath))
throw new ArgumentException("The provided basePath is invalid.", "basePath"); throw new ArgumentException("The provided basePath is invalid.", "basePath");
if (defaultHeader == null) if (defaultHeaders == null)
throw new ArgumentNullException("defaultHeader"); throw new ArgumentNullException("defaultHeaders");
if (apiKey == null) if (apiKey == null)
throw new ArgumentNullException("apiKey"); throw new ArgumentNullException("apiKey");
if (apiKeyPrefix == null) if (apiKeyPrefix == null)
@ -130,9 +130,9 @@ namespace Org.OpenAPITools.Client
BasePath = basePath; BasePath = basePath;
foreach (var keyValuePair in defaultHeader) foreach (var keyValuePair in defaultHeaders)
{ {
DefaultHeader.Add(keyValuePair); DefaultHeaders.Add(keyValuePair);
} }
foreach (var keyValuePair in apiKey) foreach (var keyValuePair in apiKey)
@ -163,7 +163,23 @@ namespace Org.OpenAPITools.Client
/// <summary> /// <summary>
/// Gets or sets the default header. /// Gets or sets the default header.
/// </summary> /// </summary>
public virtual IDictionary<string, string> DefaultHeader { get; set; } [Obsolete("Use DefaultHeaders instead.")]
public virtual IDictionary<string, string> DefaultHeader
{
get
{
return DefaultHeaders;
}
set
{
DefaultHeaders = value;
}
}
/// <summary>
/// Gets or sets the default headers.
/// </summary>
public virtual IDictionary<string, string> DefaultHeaders { get; set; }
/// <summary> /// <summary>
/// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds. /// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds.
@ -374,17 +390,17 @@ namespace Org.OpenAPITools.Client
Dictionary<string, string> apiKey = first.ApiKey.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary<string, string> apiKey = first.ApiKey.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Dictionary<string, string> apiKeyPrefix = first.ApiKeyPrefix.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary<string, string> apiKeyPrefix = first.ApiKeyPrefix.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Dictionary<string, string> defaultHeader = first.DefaultHeader.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary<string, string> defaultHeaders = first.DefaultHeaders.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
foreach (var kvp in second.ApiKey) apiKey[kvp.Key] = kvp.Value; foreach (var kvp in second.ApiKey) apiKey[kvp.Key] = kvp.Value;
foreach (var kvp in second.ApiKeyPrefix) apiKeyPrefix[kvp.Key] = kvp.Value; foreach (var kvp in second.ApiKeyPrefix) apiKeyPrefix[kvp.Key] = kvp.Value;
foreach (var kvp in second.DefaultHeader) defaultHeader[kvp.Key] = kvp.Value; foreach (var kvp in second.DefaultHeaders) defaultHeaders[kvp.Key] = kvp.Value;
var config = new Configuration var config = new Configuration
{ {
ApiKey = apiKey, ApiKey = apiKey,
ApiKeyPrefix = apiKeyPrefix, ApiKeyPrefix = apiKeyPrefix,
DefaultHeader = defaultHeader, DefaultHeader = defaultHeaders,
BasePath = second.BasePath ?? first.BasePath, BasePath = second.BasePath ?? first.BasePath,
Timeout = second.Timeout, Timeout = second.Timeout,
UserAgent = second.UserAgent ?? first.UserAgent, UserAgent = second.UserAgent ?? first.UserAgent,

View File

@ -9,6 +9,7 @@
*/ */
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Org.OpenAPITools.Client namespace Org.OpenAPITools.Client
@ -52,8 +53,15 @@ namespace Org.OpenAPITools.Client
/// Gets the default header. /// Gets the default header.
/// </summary> /// </summary>
/// <value>Default header.</value> /// <value>Default header.</value>
[Obsolete("Use DefaultHeaders instead.")]
IDictionary<string, string> DefaultHeader { get; } IDictionary<string, string> DefaultHeader { get; }
/// <summary>
/// Gets the default headers.
/// </summary>
/// <value>Default headers.</value>
IDictionary<string, string> DefaultHeaders { get; }
/// <summary> /// <summary>
/// Gets the temp folder path. /// Gets the temp folder path.
/// </summary> /// </summary>