forked from loafle/openapi-generator-original
[csharp] Refactor Configuration usage (#5740)
* [csharp] refactor ApiClient and Configuration usage * # This is a combination of 2 commits. # The first commit's message is: [csharp] refactor ApiClient and Configuration usage [csharp] Regenerate sample # This is the 2nd commit message: [csharp] Simplify setting apikey/prefix/headers * # This is a combination of 2 commits. # The first commit's message is: [csharp] refactor ApiClient and Configuration usage * Simplify setting apikey/prefix/headers * Regenerate sample # This is the 2nd commit message: [csharp] Pass-through configuration timeout to client * [csharp] refactor ApiClient and Configuration usage * Simplify setting apikey/prefix/headers * Regenerate sample * [csharp] Regenerate all client samples * [csharp] regenerate .net standard/core samples * [csharp] Fix Timeout diff between netstandard and non-netstandard * [csharp] Resolve additional netStandard issues after merge * [csharp] Update doc for Configuration usage
This commit is contained in:
@@ -312,6 +312,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
|
||||
clientPackageDir, "ApiResponse.cs"));
|
||||
supportingFiles.add(new SupportingFile("ExceptionFactory.mustache",
|
||||
clientPackageDir, "ExceptionFactory.cs"));
|
||||
|
||||
if(Boolean.FALSE.equals(this.netStandard) && Boolean.FALSE.equals(this.netCoreProjectFileFlag)) {
|
||||
supportingFiles.add(new SupportingFile("compile.mustache", "", "build.bat"));
|
||||
supportingFiles.add(new SupportingFile("compile-mono.sh.mustache", "", "build.sh"));
|
||||
@@ -324,6 +325,11 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
|
||||
supportingFiles.add(new SupportingFile("project.json.mustache", packageFolder + File.separator, "project.json"));
|
||||
}
|
||||
|
||||
supportingFiles.add(new SupportingFile("IReadableConfiguration.mustache",
|
||||
clientPackageDir, "IReadableConfiguration.cs"));
|
||||
supportingFiles.add(new SupportingFile("GlobalConfiguration.mustache",
|
||||
clientPackageDir, "GlobalConfiguration.cs"));
|
||||
|
||||
// Only write out test related files if excludeTests is unset or explicitly set to false (see start of this method)
|
||||
if(Boolean.FALSE.equals(excludeTests)) {
|
||||
// shell script to run the nunit test
|
||||
|
||||
@@ -49,11 +49,11 @@ namespace {{packageName}}.Client
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ApiClient" /> class
|
||||
/// with default configuration and base path ({{{basePath}}}).
|
||||
/// with default configuration.
|
||||
/// </summary>
|
||||
public ApiClient()
|
||||
{
|
||||
Configuration = Configuration.Default;
|
||||
Configuration = {{packageName}}.Client.Configuration.Default;
|
||||
RestClient = new RestClient("{{{basePath}}}");
|
||||
{{#netStandard}}
|
||||
RestClient.IgnoreResponseStatusCode = true;
|
||||
@@ -65,14 +65,11 @@ namespace {{packageName}}.Client
|
||||
/// with default base path ({{{basePath}}}).
|
||||
/// </summary>
|
||||
/// <param name="config">An instance of Configuration.</param>
|
||||
public ApiClient(Configuration config = null)
|
||||
public ApiClient(Configuration config)
|
||||
{
|
||||
if (config == null)
|
||||
Configuration = Configuration.Default;
|
||||
else
|
||||
Configuration = config;
|
||||
Configuration = config ?? {{packageName}}.Client.Configuration.Default;
|
||||
|
||||
RestClient = new RestClient("{{{basePath}}}");
|
||||
RestClient = new RestClient(Configuration.BasePath);
|
||||
{{#netStandard}}
|
||||
RestClient.IgnoreResponseStatusCode = true;
|
||||
{{/netStandard}}
|
||||
@@ -92,7 +89,7 @@ namespace {{packageName}}.Client
|
||||
{{#netStandard}}
|
||||
RestClient.IgnoreResponseStatusCode = true;
|
||||
{{/netStandard}}
|
||||
Configuration = Configuration.Default;
|
||||
Configuration = Client.Configuration.Default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -103,10 +100,15 @@ namespace {{packageName}}.Client
|
||||
public static ApiClient Default;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Configuration.
|
||||
/// Gets or sets an instance of the IReadableConfiguration.
|
||||
/// </summary>
|
||||
/// <value>An instance of the Configuration.</value>
|
||||
public Configuration Configuration { get; set; }
|
||||
/// <value>An instance of the IReadableConfiguration.</value>
|
||||
/// <remarks>
|
||||
/// <see cref="IReadableConfiguration"/> helps us to avoid modifying possibly global
|
||||
/// configuration values from within a given client. It does not gaurantee thread-safety
|
||||
/// of the <see cref="Configuration"/> instance in any way.
|
||||
/// </remarks>
|
||||
public IReadableConfiguration Configuration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the RestClient.
|
||||
@@ -210,7 +212,8 @@ namespace {{packageName}}.Client
|
||||
pathParams, contentType);
|
||||
|
||||
// set timeout
|
||||
RestClient.Timeout = Configuration.Timeout;
|
||||
{{#netStandard}}RestClient.Timeout = TimeSpan.FromMilliseconds(Configuration.Timeout);{{/netStandard}}
|
||||
{{^netStandard}}RestClient.Timeout = Configuration.Timeout;{{/netStandard}}
|
||||
// set user agent
|
||||
RestClient.UserAgent = Configuration.UserAgent;
|
||||
|
||||
@@ -334,6 +337,7 @@ namespace {{packageName}}.Client
|
||||
return response.RawBytes;
|
||||
}
|
||||
|
||||
// TODO: ? if (type.IsAssignableFrom(typeof(Stream)))
|
||||
if (type == typeof(Stream))
|
||||
{
|
||||
if (headers != null)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{{>partial_header}}
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@@ -11,10 +12,147 @@ namespace {{packageName}}.Client
|
||||
/// <summary>
|
||||
/// Represents a set of configuration settings
|
||||
/// </summary>
|
||||
{{>visibility}} class Configuration
|
||||
{{>visibility}} class Configuration : IReadableConfiguration
|
||||
{
|
||||
#region Constants
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the Configuration class with different settings
|
||||
/// Version of the package.
|
||||
/// </summary>
|
||||
/// <value>Version of the package.</value>
|
||||
public const string Version = "{{packageVersion}}";
|
||||
|
||||
/// <summary>
|
||||
/// Identifier for ISO 8601 DateTime Format
|
||||
/// </summary>
|
||||
/// <remarks>See https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 for more information.</remarks>
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public const string ISO8601_DATETIME_FORMAT = "o";
|
||||
|
||||
#endregion Constants
|
||||
|
||||
#region Static Members
|
||||
|
||||
private static readonly object GlobalConfigSync = new { };
|
||||
private static Configuration _globalConfiguration;
|
||||
|
||||
/// <summary>
|
||||
/// Default creation of exceptions for a given method name and response object
|
||||
/// </summary>
|
||||
public static readonly ExceptionFactory DefaultExceptionFactory = (methodName, response) =>
|
||||
{
|
||||
var status = (int)response.StatusCode;
|
||||
if (status >= 400)
|
||||
{
|
||||
return new ApiException(status,
|
||||
string.Format("Error calling {0}: {1}", methodName, response.Content),
|
||||
response.Content);
|
||||
}
|
||||
{{^netStandard}}if (status == 0)
|
||||
{
|
||||
return new ApiException(status,
|
||||
string.Format("Error calling {0}: {1}", methodName, response.ErrorMessage), response.ErrorMessage);
|
||||
}{{/netStandard}}
|
||||
return null;
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the default Configuration.
|
||||
/// </summary>
|
||||
/// <value>Configuration.</value>
|
||||
public static Configuration Default
|
||||
{
|
||||
get { return _globalConfiguration; }
|
||||
set
|
||||
{
|
||||
lock (GlobalConfigSync)
|
||||
{
|
||||
_globalConfiguration = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Static Members
|
||||
|
||||
#region Private Members
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the API key based on the authentication name.
|
||||
/// </summary>
|
||||
/// <value>The API key.</value>
|
||||
private IDictionary<string, string> _apiKey = null;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the prefix (e.g. Token) of the API key based on the authentication name.
|
||||
/// </summary>
|
||||
/// <value>The prefix of the API key.</value>
|
||||
private IDictionary<string, string> _apiKeyPrefix = null;
|
||||
|
||||
private string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
|
||||
private string _tempFolderPath = Path.GetTempPath();
|
||||
|
||||
#endregion Private Members
|
||||
|
||||
#region Constructors
|
||||
|
||||
static Configuration()
|
||||
{
|
||||
_globalConfiguration = new GlobalConfiguration();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Configuration" /> class
|
||||
/// </summary>
|
||||
public Configuration()
|
||||
{
|
||||
UserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{packageVersion}}/csharp{{/httpUserAgent}}";
|
||||
BasePath = "{{{basePath}}}";
|
||||
DefaultHeader = new ConcurrentDictionary<string, string>();
|
||||
ApiKey = new ConcurrentDictionary<string, string>();
|
||||
ApiKeyPrefix = new ConcurrentDictionary<string, string>();
|
||||
|
||||
// Setting Timeout has side effects (forces ApiClient creation).
|
||||
Timeout = 100000;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Configuration" /> class
|
||||
/// </summary>
|
||||
public Configuration(
|
||||
IDictionary<string, string> defaultHeader,
|
||||
IDictionary<string, string> apiKey,
|
||||
IDictionary<string, string> apiKeyPrefix,
|
||||
string basePath = "{{{basePath}}}") : this()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(basePath))
|
||||
throw new ArgumentException("The provided basePath is invalid.", "basePath");
|
||||
if (defaultHeader == null)
|
||||
throw new ArgumentNullException("defaultHeader");
|
||||
if (apiKey == null)
|
||||
throw new ArgumentNullException("apiKey");
|
||||
if (apiKeyPrefix == null)
|
||||
throw new ArgumentNullException("apiKeyPrefix");
|
||||
|
||||
BasePath = basePath;
|
||||
|
||||
foreach (var keyValuePair in defaultHeader)
|
||||
{
|
||||
DefaultHeader.Add(keyValuePair);
|
||||
}
|
||||
|
||||
foreach (var keyValuePair in apiKey)
|
||||
{
|
||||
ApiKey.Add(keyValuePair);
|
||||
}
|
||||
|
||||
foreach (var keyValuePair in apiKeyPrefix)
|
||||
{
|
||||
ApiKeyPrefix.Add(keyValuePair);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Configuration" /> class with different settings
|
||||
/// </summary>
|
||||
/// <param name="apiClient">Api client</param>
|
||||
/// <param name="defaultHeader">Dictionary of default HTTP header</param>
|
||||
@@ -27,131 +165,225 @@ namespace {{packageName}}.Client
|
||||
/// <param name="dateTimeFormat">DateTime format string</param>
|
||||
/// <param name="timeout">HTTP connection timeout (in milliseconds)</param>
|
||||
/// <param name="userAgent">HTTP user agent</param>
|
||||
public Configuration(ApiClient apiClient = null,
|
||||
Dictionary<String, String> defaultHeader = null,
|
||||
string username = null,
|
||||
string password = null,
|
||||
string accessToken = null,
|
||||
Dictionary<String, String> apiKey = null,
|
||||
Dictionary<String, String> apiKeyPrefix = null,
|
||||
string tempFolderPath = null,
|
||||
string dateTimeFormat = null,
|
||||
int timeout = 100000,
|
||||
string userAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{packageVersion}}/csharp{{/httpUserAgent}}"
|
||||
)
|
||||
[Obsolete("Use explicit object construction and setting of properties.", true)]
|
||||
public Configuration(
|
||||
// ReSharper disable UnusedParameter.Local
|
||||
ApiClient apiClient = null,
|
||||
IDictionary<string, string> defaultHeader = null,
|
||||
string username = null,
|
||||
string password = null,
|
||||
string accessToken = null,
|
||||
IDictionary<string, string> apiKey = null,
|
||||
IDictionary<string, string> apiKeyPrefix = null,
|
||||
string tempFolderPath = null,
|
||||
string dateTimeFormat = null,
|
||||
int timeout = 100000,
|
||||
string userAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{packageVersion}}/csharp{{/httpUserAgent}}"
|
||||
// ReSharper restore UnusedParameter.Local
|
||||
)
|
||||
{
|
||||
setApiClientUsingDefault(apiClient);
|
||||
|
||||
Username = username;
|
||||
Password = password;
|
||||
AccessToken = accessToken;
|
||||
UserAgent = userAgent;
|
||||
|
||||
if (defaultHeader != null)
|
||||
DefaultHeader = defaultHeader;
|
||||
if (apiKey != null)
|
||||
ApiKey = apiKey;
|
||||
if (apiKeyPrefix != null)
|
||||
ApiKeyPrefix = apiKeyPrefix;
|
||||
|
||||
TempFolderPath = tempFolderPath;
|
||||
DateTimeFormat = dateTimeFormat;
|
||||
Timeout = {{#netStandard}}TimeSpan.FromMilliseconds({{/netStandard}}timeout{{#netStandard}}){{/netStandard}};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the Configuration class.
|
||||
/// </summary>
|
||||
/// <param name="apiClient">Api client.</param>
|
||||
[Obsolete("This constructor caused unexpected sharing of static data. It is no longer supported.", true)]
|
||||
// ReSharper disable once UnusedParameter.Local
|
||||
public Configuration(ApiClient apiClient)
|
||||
{
|
||||
setApiClientUsingDefault(apiClient);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Version of the package.
|
||||
/// </summary>
|
||||
/// <value>Version of the package.</value>
|
||||
public const string Version = "{{packageVersion}}";
|
||||
#endregion Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the default Configuration.
|
||||
/// </summary>
|
||||
/// <value>Configuration.</value>
|
||||
public static Configuration Default = new Configuration();
|
||||
|
||||
#region Properties
|
||||
|
||||
private ApiClient _apiClient = null;
|
||||
/// <summary>
|
||||
/// Default creation of exceptions for a given method name and response object
|
||||
/// Gets an instance of an ApiClient for this configuration
|
||||
/// </summary>
|
||||
public static readonly ExceptionFactory DefaultExceptionFactory = (methodName, response) =>
|
||||
public virtual ApiClient ApiClient
|
||||
{
|
||||
int status = (int) response.StatusCode;
|
||||
if (status >= 400) return new ApiException(status, String.Format("Error calling {0}: {1}", methodName, response.Content), response.Content);
|
||||
{{^netStandard}}
|
||||
if (status == 0) return new ApiException(status, String.Format("Error calling {0}: {1}", methodName, response.ErrorMessage), response.ErrorMessage);
|
||||
{{/netStandard}}
|
||||
return null;
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds.
|
||||
/// </summary>
|
||||
/// <value>Timeout.</value>
|
||||
public {{^netStandard}}int{{/netStandard}}{{#netStandard}}TimeSpan?{{/netStandard}} Timeout
|
||||
{
|
||||
get { return ApiClient.RestClient.Timeout; }
|
||||
|
||||
set
|
||||
get
|
||||
{
|
||||
if (ApiClient != null)
|
||||
ApiClient.RestClient.Timeout = value;
|
||||
if (_apiClient == null) _apiClient = CreateApiClient();
|
||||
return _apiClient;
|
||||
}
|
||||
}
|
||||
|
||||
private String _basePath = null;
|
||||
/// <summary>
|
||||
/// Gets or sets the default API client for making HTTP calls.
|
||||
/// Gets or sets the base path for API access.
|
||||
/// </summary>
|
||||
/// <value>The API client.</value>
|
||||
public ApiClient ApiClient;
|
||||
|
||||
/// <summary>
|
||||
/// Set the ApiClient using Default or ApiClient instance.
|
||||
/// </summary>
|
||||
/// <param name="apiClient">An instance of ApiClient.</param>
|
||||
/// <returns></returns>
|
||||
public void setApiClientUsingDefault (ApiClient apiClient = null)
|
||||
{
|
||||
if (apiClient == null)
|
||||
{
|
||||
if (Default != null && Default.ApiClient == null)
|
||||
Default.ApiClient = new ApiClient();
|
||||
|
||||
ApiClient = Default != null ? Default.ApiClient : new ApiClient();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Default != null && Default.ApiClient == null)
|
||||
Default.ApiClient = apiClient;
|
||||
|
||||
ApiClient = apiClient;
|
||||
public virtual string BasePath {
|
||||
get { return _basePath; }
|
||||
set {
|
||||
_basePath = value;
|
||||
// pass-through to ApiClient if it's set.
|
||||
if(_apiClient != null) {
|
||||
_apiClient.RestClient.BaseUrl = new Uri(_basePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<String, String> _defaultHeaderMap = new Dictionary<String, String>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the default header.
|
||||
/// </summary>
|
||||
public Dictionary<String, String> DefaultHeader
|
||||
public virtual IDictionary<string, string> DefaultHeader { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds.
|
||||
/// </summary>
|
||||
public virtual int Timeout
|
||||
{
|
||||
get { return _defaultHeaderMap; }
|
||||
{{#netStandard}}get { return (int)ApiClient.RestClient.Timeout.GetValueOrDefault(TimeSpan.FromSeconds(0)).TotalMilliseconds; }
|
||||
set { ApiClient.RestClient.Timeout = TimeSpan.FromMilliseconds(value); }{{/netStandard}}{{^netStandard}}
|
||||
get { return ApiClient.RestClient.Timeout; }
|
||||
set { ApiClient.RestClient.Timeout = value; }{{/netStandard}}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the HTTP user agent.
|
||||
/// </summary>
|
||||
/// <value>Http user agent.</value>
|
||||
public virtual string UserAgent { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the username (HTTP basic authentication).
|
||||
/// </summary>
|
||||
/// <value>The username.</value>
|
||||
public virtual string Username { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the password (HTTP basic authentication).
|
||||
/// </summary>
|
||||
/// <value>The password.</value>
|
||||
public virtual string Password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the access token for OAuth2 authentication.
|
||||
/// </summary>
|
||||
/// <param name="apiKeyIdentifier">API key identifier (authentication scheme).</param>
|
||||
/// <returns>API key with prefix.</returns>
|
||||
public string GetApiKeyWithPrefix (string apiKeyIdentifier)
|
||||
{
|
||||
var apiKeyValue = "";
|
||||
ApiKey.TryGetValue (apiKeyIdentifier, out apiKeyValue);
|
||||
var apiKeyPrefix = "";
|
||||
if (ApiKeyPrefix.TryGetValue (apiKeyIdentifier, out apiKeyPrefix))
|
||||
return apiKeyPrefix + " " + apiKeyValue;
|
||||
else
|
||||
return apiKeyValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the access token for OAuth2 authentication.
|
||||
/// </summary>
|
||||
/// <value>The access token.</value>
|
||||
public virtual string AccessToken { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the temporary folder path to store the files downloaded from the server.
|
||||
/// </summary>
|
||||
/// <value>Folder path.</value>
|
||||
public virtual string TempFolderPath
|
||||
{
|
||||
get { return _tempFolderPath; }
|
||||
|
||||
set
|
||||
{
|
||||
_defaultHeaderMap = value;
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
// Possible breaking change since swagger-codegen 2.2.1, enforce a valid temporary path on set.
|
||||
_tempFolderPath = Path.GetTempPath();
|
||||
return;
|
||||
}
|
||||
|
||||
// create the directory if it does not exist
|
||||
if (!Directory.Exists(value))
|
||||
{
|
||||
Directory.CreateDirectory(value);
|
||||
}
|
||||
|
||||
// check if the path contains directory separator at the end
|
||||
if (value[value.Length - 1] == Path.DirectorySeparatorChar)
|
||||
{
|
||||
_tempFolderPath = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
_tempFolderPath = value + Path.DirectorySeparatorChar;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the the date time format used when serializing in the ApiClient
|
||||
/// By default, it's set to ISO 8601 - "o", for others see:
|
||||
/// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
|
||||
/// and https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
|
||||
/// No validation is done to ensure that the string you're providing is valid
|
||||
/// </summary>
|
||||
/// <value>The DateTimeFormat string</value>
|
||||
public virtual string DateTimeFormat
|
||||
{
|
||||
get { return _dateTimeFormat; }
|
||||
set
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
// Never allow a blank or null string, go back to the default
|
||||
_dateTimeFormat = ISO8601_DATETIME_FORMAT;
|
||||
return;
|
||||
}
|
||||
|
||||
// Caution, no validation when you choose date time format other than ISO 8601
|
||||
// Take a look at the above links
|
||||
_dateTimeFormat = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the prefix (e.g. Token) of the API key based on the authentication name.
|
||||
/// </summary>
|
||||
/// <value>The prefix of the API key.</value>
|
||||
public virtual IDictionary<string, string> ApiKeyPrefix
|
||||
{
|
||||
get { return _apiKeyPrefix; }
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new InvalidOperationException("ApiKeyPrefix collection may not be null.");
|
||||
}
|
||||
_apiKeyPrefix = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the API key based on the authentication name.
|
||||
/// </summary>
|
||||
/// <value>The API key.</value>
|
||||
public virtual IDictionary<string, string> ApiKey
|
||||
{
|
||||
get { return _apiKey; }
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new InvalidOperationException("ApiKey collection may not be null.");
|
||||
}
|
||||
_apiKey = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Properties
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Add default header.
|
||||
/// </summary>
|
||||
@@ -160,7 +392,38 @@ namespace {{packageName}}.Client
|
||||
/// <returns></returns>
|
||||
public void AddDefaultHeader(string key, string value)
|
||||
{
|
||||
_defaultHeaderMap[key] = value;
|
||||
DefaultHeader[key] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ApiClient" /> based on this <see cref="Configuration" /> instance.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public ApiClient CreateApiClient()
|
||||
{
|
||||
return new ApiClient(BasePath) { Configuration = this };
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns a string with essential information for debugging.
|
||||
/// </summary>
|
||||
public static String ToDebugReport()
|
||||
{
|
||||
String report = "C# SDK ({{{packageName}}}) Debug Report:\n";
|
||||
{{^netStandard}}
|
||||
{{^supportsUWP}}
|
||||
report += " OS: " + System.Environment.OSVersion + "\n";
|
||||
report += " .NET Framework Version: " + System.Environment.Version + "\n";
|
||||
{{/supportsUWP}}
|
||||
{{/netStandard}}
|
||||
{{#netStandard}}
|
||||
report += " OS: " + System.Runtime.InteropServices.RuntimeInformation.OSDescription + "\n";
|
||||
{{/netStandard}}
|
||||
report += " Version of the API: {{{version}}}\n";
|
||||
report += " SDK Package Version: {{{packageVersion}}}\n";
|
||||
|
||||
return report;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -184,151 +447,6 @@ namespace {{packageName}}.Client
|
||||
ApiKeyPrefix[key] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the HTTP user agent.
|
||||
/// </summary>
|
||||
/// <value>Http user agent.</value>
|
||||
public String UserAgent { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the username (HTTP basic authentication).
|
||||
/// </summary>
|
||||
/// <value>The username.</value>
|
||||
public String Username { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the password (HTTP basic authentication).
|
||||
/// </summary>
|
||||
/// <value>The password.</value>
|
||||
public String Password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the access token for OAuth2 authentication.
|
||||
/// </summary>
|
||||
/// <value>The access token.</value>
|
||||
public String AccessToken { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the API key based on the authentication name.
|
||||
/// </summary>
|
||||
/// <value>The API key.</value>
|
||||
public Dictionary<String, String> ApiKey = new Dictionary<String, String>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the prefix (e.g. Token) of the API key based on the authentication name.
|
||||
/// </summary>
|
||||
/// <value>The prefix of the API key.</value>
|
||||
public Dictionary<String, String> ApiKeyPrefix = new Dictionary<String, String>();
|
||||
|
||||
/// <summary>
|
||||
/// Get the API key with prefix.
|
||||
/// </summary>
|
||||
/// <param name="apiKeyIdentifier">API key identifier (authentication scheme).</param>
|
||||
/// <returns>API key with prefix.</returns>
|
||||
public string GetApiKeyWithPrefix (string apiKeyIdentifier)
|
||||
{
|
||||
var apiKeyValue = "";
|
||||
ApiKey.TryGetValue (apiKeyIdentifier, out apiKeyValue);
|
||||
var apiKeyPrefix = "";
|
||||
if (ApiKeyPrefix.TryGetValue (apiKeyIdentifier, out apiKeyPrefix))
|
||||
return apiKeyPrefix + " " + apiKeyValue;
|
||||
else
|
||||
return apiKeyValue;
|
||||
}
|
||||
|
||||
private string _tempFolderPath;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the temporary folder path to store the files downloaded from the server.
|
||||
/// </summary>
|
||||
/// <value>Folder path.</value>
|
||||
public String TempFolderPath
|
||||
{
|
||||
get
|
||||
{
|
||||
// default to Path.GetTempPath() if _tempFolderPath is not set
|
||||
if (String.IsNullOrEmpty(_tempFolderPath))
|
||||
{
|
||||
_tempFolderPath = Path.GetTempPath();
|
||||
}
|
||||
return _tempFolderPath;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (String.IsNullOrEmpty(value))
|
||||
{
|
||||
_tempFolderPath = value;
|
||||
return;
|
||||
}
|
||||
|
||||
// create the directory if it does not exist
|
||||
if (!Directory.Exists(value))
|
||||
Directory.CreateDirectory(value);
|
||||
|
||||
// check if the path contains directory separator at the end
|
||||
if (value[value.Length - 1] == Path.DirectorySeparatorChar)
|
||||
_tempFolderPath = value;
|
||||
else
|
||||
_tempFolderPath = value + Path.DirectorySeparatorChar;
|
||||
}
|
||||
}
|
||||
|
||||
private const string ISO8601_DATETIME_FORMAT = "o";
|
||||
|
||||
private string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the the date time format used when serializing in the ApiClient
|
||||
/// By default, it's set to ISO 8601 - "o", for others see:
|
||||
/// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
|
||||
/// and https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
|
||||
/// No validation is done to ensure that the string you're providing is valid
|
||||
/// </summary>
|
||||
/// <value>The DateTimeFormat string</value>
|
||||
public String DateTimeFormat
|
||||
{
|
||||
get
|
||||
{
|
||||
return _dateTimeFormat;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
// Never allow a blank or null string, go back to the default
|
||||
_dateTimeFormat = ISO8601_DATETIME_FORMAT;
|
||||
return;
|
||||
}
|
||||
|
||||
// Caution, no validation when you choose date time format other than ISO 8601
|
||||
// Take a look at the above links
|
||||
_dateTimeFormat = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a string with essential information for debugging.
|
||||
/// </summary>
|
||||
public static String ToDebugReport()
|
||||
{
|
||||
String report = "C# SDK ({{{packageName}}}) Debug Report:\n";
|
||||
{{^netStandard}}
|
||||
{{^supportsUWP}}
|
||||
report += " OS: " + Environment.OSVersion + "\n";
|
||||
report += " .NET Framework Version: " + Assembly
|
||||
.GetExecutingAssembly()
|
||||
.GetReferencedAssemblies()
|
||||
.Where(x => x.Name == "System.Core").First().Version.ToString() + "\n";
|
||||
{{/supportsUWP}}
|
||||
{{/netStandard}}
|
||||
{{#netStandard}}
|
||||
report += " OS: " + System.Runtime.InteropServices.RuntimeInformation.OSDescription + "\n";
|
||||
{{/netStandard}}
|
||||
report += " Version of the API: {{{version}}}\n";
|
||||
report += " SDK Package Version: {{{packageVersion}}}\n";
|
||||
|
||||
return report;
|
||||
}
|
||||
#endregion Methods
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
{{>partial_header}}
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace {{packageName}}.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="GlobalConfiguration"/> provides a compile-time extension point for globally configuring
|
||||
/// API Clients.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A customized implementation via partial class may reside in another file and may
|
||||
/// be excluded from automatic generation via a .swagger-codegen-ignore file.
|
||||
/// </remarks>
|
||||
public partial class GlobalConfiguration : Configuration
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
{{>partial_header}}
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace {{packageName}}.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a readable-only configuration contract.
|
||||
/// </summary>
|
||||
public interface IReadableConfiguration
|
||||
{
|
||||
string AccessToken { get; }
|
||||
IDictionary<string, string> ApiKey { get; }
|
||||
IDictionary<string, string> ApiKeyPrefix { get; }
|
||||
string BasePath { get; }
|
||||
string DateTimeFormat { get; }
|
||||
IDictionary<string, string> DefaultHeader { get; }
|
||||
string Password { get; }
|
||||
string TempFolderPath { get; }
|
||||
int Timeout { get; }
|
||||
string UserAgent { get; }
|
||||
string Username { get; }
|
||||
|
||||
string GetApiKeyWithPrefix(string apiKeyIdentifier);
|
||||
}
|
||||
}
|
||||
@@ -88,15 +88,9 @@ namespace {{packageName}}.{{apiPackage}}
|
||||
/// <returns></returns>
|
||||
public {{classname}}(String basePath)
|
||||
{
|
||||
this.Configuration = new Configuration(new ApiClient(basePath));
|
||||
this.Configuration = new Configuration { BasePath = basePath };
|
||||
|
||||
ExceptionFactory = {{packageName}}.Client.Configuration.DefaultExceptionFactory;
|
||||
|
||||
// ensure API client has configuration ready
|
||||
if (Configuration.ApiClient.Configuration == null)
|
||||
{
|
||||
this.Configuration.ApiClient.Configuration = this.Configuration;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -113,12 +107,6 @@ namespace {{packageName}}.{{apiPackage}}
|
||||
this.Configuration = configuration;
|
||||
|
||||
ExceptionFactory = {{packageName}}.Client.Configuration.DefaultExceptionFactory;
|
||||
|
||||
// ensure API client has configuration ready
|
||||
if (Configuration.ApiClient.Configuration == null)
|
||||
{
|
||||
this.Configuration.ApiClient.Configuration = this.Configuration;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -167,9 +155,9 @@ namespace {{packageName}}.{{apiPackage}}
|
||||
/// </summary>
|
||||
/// <returns>Dictionary of HTTP header</returns>
|
||||
[Obsolete("DefaultHeader is deprecated, please use Configuration.DefaultHeader instead.")]
|
||||
public Dictionary<String, String> DefaultHeader()
|
||||
public IDictionary<String, String> DefaultHeader()
|
||||
{
|
||||
return this.Configuration.DefaultHeader;
|
||||
return new ReadOnlyDictionary<string, string>(this.Configuration.DefaultHeader);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -41,9 +41,9 @@ namespace Example
|
||||
{{/isBasic}}
|
||||
{{#isApiKey}}
|
||||
// Configure API key authorization: {{{name}}}
|
||||
Configuration.Default.ApiKey.Add("{{{keyParamName}}}", "YOUR_API_KEY");
|
||||
Configuration.Default.AddApiKey("{{{keyParamName}}}", "YOUR_API_KEY");
|
||||
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
|
||||
// Configuration.Default.ApiKeyPrefix.Add("{{{keyParamName}}}", "Bearer");
|
||||
// Configuration.Default.AddApiKeyPrefix("{{{keyParamName}}}", "Bearer");
|
||||
{{/isApiKey}}
|
||||
{{#isOAuth}}
|
||||
// Configure OAuth2 access token for authorization: {{{name}}}
|
||||
|
||||
Reference in New Issue
Block a user