Merge pull request #1716 from wing328/csharp_config_remove_static

[C#] remove some static methods in configuration
This commit is contained in:
wing328 2015-12-15 19:51:29 +08:00
commit 89f269969d
19 changed files with 196 additions and 56 deletions

View File

@ -19,23 +19,56 @@ namespace {{packageName}}.Client
public class ApiClient public class ApiClient
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ApiClient" /> class. /// Initializes a new instance of the <see cref="ApiClient" /> class
/// with default configuration and base path ({{basePath}}).
/// </summary>
public ApiClient()
{
Configuration = Configuration.Default;
RestClient = new RestClient("{{basePath}}");
}
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" /> class
/// with default base path ({{basePath}}).
/// </summary>
/// <param name="config">An instance of Configuration.</param>
public ApiClient(Configuration config = null)
{
if (config == null)
Configuration = Configuration.Default;
else
Configuration = config;
RestClient = new RestClient("{{basePath}}");
}
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" /> class
/// with default configuration.
/// </summary> /// </summary>
/// <param name="basePath">The base path.</param> /// <param name="basePath">The base path.</param>
public ApiClient(String basePath="{{basePath}}") public ApiClient(String basePath = "{{basePath}}")
{ {
if (String.IsNullOrEmpty(basePath)) if (String.IsNullOrEmpty(basePath))
throw new ArgumentException("basePath cannot be empty"); throw new ArgumentException("basePath cannot be empty");
RestClient = new RestClient(basePath); RestClient = new RestClient(basePath);
Configuration = Configuration.Default;
} }
/// <summary> /// <summary>
/// Gets or sets the default API client for making HTTP calls. /// Gets or sets the default API client for making HTTP calls.
/// </summary> /// </summary>
/// <value>The default API client.</value> /// <value>The default API client.</value>
public static ApiClient Default = new ApiClient(); public static ApiClient Default = new ApiClient(Configuration.Default);
/// <summary>
/// Gets or sets the Configuration.
/// </summary>
/// <value>An instance of the Configuration.</value>
public Configuration Configuration { get; set; }
/// <summary> /// <summary>
/// Gets or sets the RestClient. /// Gets or sets the RestClient.
/// </summary> /// </summary>

View File

@ -12,6 +12,45 @@ namespace {{packageName}}.Client
/// </summary> /// </summary>
public class Configuration public class Configuration
{ {
/// <summary>
/// Initializes a new instance of the Configuration class with different settings
/// </summary>
/// <param name="apiClient">Api client</param>
/// <param name="defaultHeader">Dictionary of default HTTP header</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="accessToken">accessToken</param>
/// <param name="apiKey">Dictionary of API key</param>
/// <param name="apiKeyPrefix">Dictionary of API key prefix</param>
/// <param name="tempFolderPath">Temp folder path</param>
/// <param name="dateTimeFormat">DateTime format string</param>
public Configuration(ApiClient apiClient,
Dictionary<String, String> defaultHeader,
string username,
string password,
string accessToken,
Dictionary<String, String> apiKey,
Dictionary<String, String> apiKeyPrefix,
string tempFolderPath,
string dateTimeFormat
)
{
if (apiClient == null)
ApiClient = ApiClient.Default;
else
ApiClient = apiClient;
Username = username;
Password = password;
AccessToken = accessToken;
ApiKey = apiKey;
ApiKeyPrefix = apiKeyPrefix;
TempFolderPath = tempFolderPath;
DateTimeFormat = dateTimeFormat;
}
/// <summary> /// <summary>
/// Initializes a new instance of the Configuration class. /// Initializes a new instance of the Configuration class.
/// </summary> /// </summary>
@ -109,13 +148,13 @@ namespace {{packageName}}.Client
return apiKeyValue; return apiKeyValue;
} }
private static string _tempFolderPath = Path.GetTempPath(); private string _tempFolderPath = Path.GetTempPath();
/// <summary> /// <summary>
/// Gets or sets the temporary folder path to store the files downloaded from the server. /// Gets or sets the temporary folder path to store the files downloaded from the server.
/// </summary> /// </summary>
/// <value>Folder path.</value> /// <value>Folder path.</value>
public static String TempFolderPath public String TempFolderPath
{ {
get { return _tempFolderPath; } get { return _tempFolderPath; }
@ -141,7 +180,7 @@ namespace {{packageName}}.Client
private const string ISO8601_DATETIME_FORMAT = "o"; private const string ISO8601_DATETIME_FORMAT = "o";
private static string _dateTimeFormat = ISO8601_DATETIME_FORMAT; private string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
/// <summary> /// <summary>
/// Gets or sets the the date time format used when serializing in the ApiClient /// Gets or sets the the date time format used when serializing in the ApiClient
@ -151,7 +190,7 @@ namespace {{packageName}}.Client
/// No validation is done to ensure that the string you're providing is valid /// No validation is done to ensure that the string you're providing is valid
/// </summary> /// </summary>
/// <value>The DateTimeFormat string</value> /// <value>The DateTimeFormat string</value>
public static String DateTimeFormat public String DateTimeFormat
{ {
get get
{ {

View File

@ -6,7 +6,6 @@ using RestSharp;
using IO.Swagger.Client; using IO.Swagger.Client;
using IO.Swagger.Model; using IO.Swagger.Model;
namespace IO.Swagger.Api namespace IO.Swagger.Api
{ {

View File

@ -6,7 +6,6 @@ using RestSharp;
using IO.Swagger.Client; using IO.Swagger.Client;
using IO.Swagger.Model; using IO.Swagger.Model;
namespace IO.Swagger.Api namespace IO.Swagger.Api
{ {

View File

@ -6,7 +6,6 @@ using RestSharp;
using IO.Swagger.Client; using IO.Swagger.Client;
using IO.Swagger.Model; using IO.Swagger.Model;
namespace IO.Swagger.Api namespace IO.Swagger.Api
{ {

View File

@ -19,23 +19,56 @@ namespace IO.Swagger.Client
public class ApiClient public class ApiClient
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ApiClient" /> class. /// Initializes a new instance of the <see cref="ApiClient" /> class
/// with default configuration and base path (http://petstore.swagger.io/v2).
/// </summary>
public ApiClient()
{
Configuration = Configuration.Default;
RestClient = new RestClient("http://petstore.swagger.io/v2");
}
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" /> class
/// with default base path (http://petstore.swagger.io/v2).
/// </summary>
/// <param name="config">An instance of Configuration.</param>
public ApiClient(Configuration config = null)
{
if (config == null)
Configuration = Configuration.Default;
else
Configuration = config;
RestClient = new RestClient("http://petstore.swagger.io/v2");
}
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" /> class
/// with default configuration.
/// </summary> /// </summary>
/// <param name="basePath">The base path.</param> /// <param name="basePath">The base path.</param>
public ApiClient(String basePath="http://petstore.swagger.io/v2") public ApiClient(String basePath = "http://petstore.swagger.io/v2")
{ {
if (String.IsNullOrEmpty(basePath)) if (String.IsNullOrEmpty(basePath))
throw new ArgumentException("basePath cannot be empty"); throw new ArgumentException("basePath cannot be empty");
RestClient = new RestClient(basePath); RestClient = new RestClient(basePath);
Configuration = Configuration.Default;
} }
/// <summary> /// <summary>
/// Gets or sets the default API client for making HTTP calls. /// Gets or sets the default API client for making HTTP calls.
/// </summary> /// </summary>
/// <value>The default API client.</value> /// <value>The default API client.</value>
public static ApiClient Default = new ApiClient(); public static ApiClient Default = new ApiClient(Configuration.Default);
/// <summary>
/// Gets or sets the Configuration.
/// </summary>
/// <value>An instance of the Configuration.</value>
public Configuration Configuration { get; set; }
/// <summary> /// <summary>
/// Gets or sets the RestClient. /// Gets or sets the RestClient.
/// </summary> /// </summary>

View File

@ -12,11 +12,53 @@ namespace IO.Swagger.Client
/// </summary> /// </summary>
public class Configuration public class Configuration
{ {
/// <summary>
/// Initializes a new instance of the Configuration class with different settings
/// </summary>
/// <param name="apiClient">Api client</param>
/// <param name="defaultHeader">Dictionary of default HTTP header</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="accessToken">accessToken</param>
/// <param name="apiKey">Dictionary of API key</param>
/// <param name="apiKeyPrefix">Dictionary of API key prefix</param>
/// <param name="tempFolderPath">Temp folder path</param>
/// <param name="dateTimeFormat">DateTime format string</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
)
{
if (apiClient == null)
ApiClient = ApiClient.Default;
else
ApiClient = apiClient;
Username = username;
Password = password;
AccessToken = accessToken;
if (apiKey != null)
ApiKey = apiKey;
if (apiKeyPrefix != null)
ApiKeyPrefix = apiKeyPrefix;
TempFolderPath = tempFolderPath;
DateTimeFormat = dateTimeFormat;
}
/// <summary> /// <summary>
/// Initializes a new instance of the Configuration class. /// Initializes a new instance of the Configuration class.
/// </summary> /// </summary>
/// <param name="apiClient">Api client.</param> /// <param name="apiClient">Api client.</param>
public Configuration(ApiClient apiClient=null) public Configuration(ApiClient apiClient)
{ {
if (apiClient == null) if (apiClient == null)
ApiClient = ApiClient.Default; ApiClient = ApiClient.Default;
@ -109,13 +151,13 @@ namespace IO.Swagger.Client
return apiKeyValue; return apiKeyValue;
} }
private static string _tempFolderPath = Path.GetTempPath(); private string _tempFolderPath = Path.GetTempPath();
/// <summary> /// <summary>
/// Gets or sets the temporary folder path to store the files downloaded from the server. /// Gets or sets the temporary folder path to store the files downloaded from the server.
/// </summary> /// </summary>
/// <value>Folder path.</value> /// <value>Folder path.</value>
public static String TempFolderPath public String TempFolderPath
{ {
get { return _tempFolderPath; } get { return _tempFolderPath; }
@ -141,7 +183,7 @@ namespace IO.Swagger.Client
private const string ISO8601_DATETIME_FORMAT = "o"; private const string ISO8601_DATETIME_FORMAT = "o";
private static string _dateTimeFormat = ISO8601_DATETIME_FORMAT; private string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
/// <summary> /// <summary>
/// Gets or sets the the date time format used when serializing in the ApiClient /// Gets or sets the the date time format used when serializing in the ApiClient
@ -151,7 +193,7 @@ namespace IO.Swagger.Client
/// No validation is done to ensure that the string you're providing is valid /// No validation is done to ensure that the string you're providing is valid
/// </summary> /// </summary>
/// <value>The DateTimeFormat string</value> /// <value>The DateTimeFormat string</value>
public static String DateTimeFormat public String DateTimeFormat
{ {
get get
{ {

View File

@ -7,8 +7,6 @@ using System.Collections.Generic;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace IO.Swagger.Model namespace IO.Swagger.Model
{ {
@ -124,6 +122,4 @@ namespace IO.Swagger.Model
} }
} }
} }

View File

@ -7,8 +7,6 @@ using System.Collections.Generic;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace IO.Swagger.Model namespace IO.Swagger.Model
{ {
@ -189,6 +187,4 @@ namespace IO.Swagger.Model
} }
} }
} }

View File

@ -7,8 +7,6 @@ using System.Collections.Generic;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace IO.Swagger.Model namespace IO.Swagger.Model
{ {
@ -189,6 +187,4 @@ namespace IO.Swagger.Model
} }
} }
} }

View File

@ -7,8 +7,6 @@ using System.Collections.Generic;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace IO.Swagger.Model namespace IO.Swagger.Model
{ {
@ -124,6 +122,4 @@ namespace IO.Swagger.Model
} }
} }
} }

View File

@ -7,8 +7,6 @@ using System.Collections.Generic;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace IO.Swagger.Model namespace IO.Swagger.Model
{ {
@ -221,6 +219,4 @@ namespace IO.Swagger.Model
} }
} }
} }

View File

@ -11,7 +11,7 @@ namespace SwaggerClientTest.TestApiClient
public void TearDown() public void TearDown()
{ {
// Reset to default, just in case // Reset to default, just in case
Configuration.DateTimeFormat = "o"; Configuration.Default.DateTimeFormat = "o";
} }
[Test ()] [Test ()]
@ -29,28 +29,34 @@ namespace SwaggerClientTest.TestApiClient
} }
[Test ()] [Test ()]
public void TestParameterToString_DateTime () public void TestParameterToStringForDateTime ()
{ {
ApiClient api = new ApiClient(); ApiClient api = new ApiClient ();
// test datetime // test datetime
DateTime dateUtc = DateTime.Parse("2008-04-10T13:30:00.0000000z", null, System.Globalization.DateTimeStyles.RoundtripKind); DateTime dateUtc = DateTime.Parse ("2008-04-10T13:30:00.0000000z", null, System.Globalization.DateTimeStyles.RoundtripKind);
Assert.AreEqual("2008-04-10T13:30:00.0000000Z", api.ParameterToString(dateUtc)); Assert.AreEqual ("2008-04-10T13:30:00.0000000Z", api.ParameterToString (dateUtc));
// test datetime with no timezone // test datetime with no timezone
DateTime dateWithNoTz = DateTime.Parse("2008-04-10T13:30:00.000", null, System.Globalization.DateTimeStyles.RoundtripKind); DateTime dateWithNoTz = DateTime.Parse ("2008-04-10T13:30:00.000", null, System.Globalization.DateTimeStyles.RoundtripKind);
Assert.AreEqual("2008-04-10T13:30:00.0000000", api.ParameterToString(dateWithNoTz)); Assert.AreEqual ("2008-04-10T13:30:00.0000000", api.ParameterToString (dateWithNoTz));
}
// The test below only passes when running at -04:00 timezone
[Ignore ()]
public void TestParameterToStringWithTimeZoneForDateTime ()
{
ApiClient api = new ApiClient ();
// test datetime with a time zone // test datetime with a time zone
DateTime dateWithTz = DateTime.Parse("2008-04-10T13:30:00.0000000-04:00", null, System.Globalization.DateTimeStyles.RoundtripKind); DateTime dateWithTz = DateTime.Parse("2008-04-10T13:30:00.0000000-04:00", null, System.Globalization.DateTimeStyles.RoundtripKind);
Assert.AreEqual("2008-04-10T13:30:00.0000000-04:00", api.ParameterToString(dateWithTz)); Assert.AreEqual("2008-04-10T13:30:00.0000000-04:00", api.ParameterToString(dateWithTz));
} }
[Test ()] [Test ()]
public void TestParameterToString_DateTime_WithUFormat () public void TestParameterToStringForDateTimeWithUFormat ()
{ {
// Setup the DateTimeFormat across all of the calls // Setup the DateTimeFormat across all of the calls
Configuration.DateTimeFormat = "u"; Configuration.Default.DateTimeFormat = "u";
ApiClient api = new ApiClient(); ApiClient api = new ApiClient();
// test datetime // test datetime
@ -59,10 +65,10 @@ namespace SwaggerClientTest.TestApiClient
} }
[Test ()] [Test ()]
public void TestParameterToString_DateTime_WithCustomFormat () public void TestParameterToStringForDateTimeWithCustomFormat ()
{ {
// Setup the DateTimeFormat across all of the calls // Setup the DateTimeFormat across all of the calls
Configuration.DateTimeFormat = "dd/MM/yy HH:mm:ss"; Configuration.Default.DateTimeFormat = "dd/MM/yy HH:mm:ss";
ApiClient api = new ApiClient(); ApiClient api = new ApiClient();
// test datetime // test datetime

View File

@ -13,7 +13,7 @@ namespace SwaggerClientTest.TestConfiguration
public void TearDown () public void TearDown ()
{ {
// Reset to default, just in case // Reset to default, just in case
Configuration.DateTimeFormat = "o"; Configuration.Default.DateTimeFormat = "o";
} }
[Test ()] [Test ()]
@ -44,17 +44,26 @@ namespace SwaggerClientTest.TestConfiguration
{ {
// Should default to the Round-trip Format Specifier - "o" // Should default to the Round-trip Format Specifier - "o"
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
Assert.AreEqual("o", Configuration.DateTimeFormat); Assert.AreEqual("o", Configuration.Default.DateTimeFormat);
} }
[Test ()] [Test ()]
public void TestDateTimeFormat_UType() public void TestDateTimeFormat_UType()
{ {
Configuration.DateTimeFormat = "u"; Configuration.Default.DateTimeFormat = "u";
Assert.AreEqual("u", Configuration.DateTimeFormat); Assert.AreEqual("u", Configuration.Default.DateTimeFormat);
} }
[Test ()]
public void TestConstructor()
{
Configuration c = new Configuration (username: "test username", password: "test password");
Assert.AreEqual (c.Username, "test username");
Assert.AreEqual (c.Password, "test password");
}
[Test ()] [Test ()]
public void TestDefautlConfiguration () public void TestDefautlConfiguration ()
{ {

View File

@ -1,4 +1,5 @@
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.swagger-logo.png
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll /Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll