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,7 +19,33 @@ namespace {{packageName}}.Client
public class ApiClient
{
/// <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>
/// <param name="basePath">The base path.</param>
public ApiClient(String basePath = "{{basePath}}")
@ -28,13 +54,20 @@ namespace {{packageName}}.Client
throw new ArgumentException("basePath cannot be empty");
RestClient = new RestClient(basePath);
Configuration = Configuration.Default;
}
/// <summary>
/// Gets or sets the default API client for making HTTP calls.
/// </summary>
/// <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>
/// Gets or sets the RestClient.

View File

@ -12,6 +12,45 @@ namespace {{packageName}}.Client
/// </summary>
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>
/// Initializes a new instance of the Configuration class.
/// </summary>
@ -109,13 +148,13 @@ namespace {{packageName}}.Client
return apiKeyValue;
}
private static string _tempFolderPath = Path.GetTempPath();
private string _tempFolderPath = Path.GetTempPath();
/// <summary>
/// Gets or sets the temporary folder path to store the files downloaded from the server.
/// </summary>
/// <value>Folder path.</value>
public static String TempFolderPath
public String TempFolderPath
{
get { return _tempFolderPath; }
@ -141,7 +180,7 @@ namespace {{packageName}}.Client
private const string ISO8601_DATETIME_FORMAT = "o";
private static string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
private string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
/// <summary>
/// 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
/// </summary>
/// <value>The DateTimeFormat string</value>
public static String DateTimeFormat
public String DateTimeFormat
{
get
{

View File

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

View File

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

View File

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

View File

@ -19,7 +19,33 @@ namespace IO.Swagger.Client
public class ApiClient
{
/// <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>
/// <param name="basePath">The base path.</param>
public ApiClient(String basePath = "http://petstore.swagger.io/v2")
@ -28,13 +54,20 @@ namespace IO.Swagger.Client
throw new ArgumentException("basePath cannot be empty");
RestClient = new RestClient(basePath);
Configuration = Configuration.Default;
}
/// <summary>
/// Gets or sets the default API client for making HTTP calls.
/// </summary>
/// <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>
/// Gets or sets the RestClient.

View File

@ -12,11 +12,53 @@ namespace IO.Swagger.Client
/// </summary>
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>
/// Initializes a new instance of the Configuration class.
/// </summary>
/// <param name="apiClient">Api client.</param>
public Configuration(ApiClient apiClient=null)
public Configuration(ApiClient apiClient)
{
if (apiClient == null)
ApiClient = ApiClient.Default;
@ -109,13 +151,13 @@ namespace IO.Swagger.Client
return apiKeyValue;
}
private static string _tempFolderPath = Path.GetTempPath();
private string _tempFolderPath = Path.GetTempPath();
/// <summary>
/// Gets or sets the temporary folder path to store the files downloaded from the server.
/// </summary>
/// <value>Folder path.</value>
public static String TempFolderPath
public String TempFolderPath
{
get { return _tempFolderPath; }
@ -141,7 +183,7 @@ namespace IO.Swagger.Client
private const string ISO8601_DATETIME_FORMAT = "o";
private static string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
private string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
/// <summary>
/// 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
/// </summary>
/// <value>The DateTimeFormat string</value>
public static String DateTimeFormat
public String DateTimeFormat
{
get
{

View File

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

View File

@ -11,7 +11,7 @@ namespace SwaggerClientTest.TestApiClient
public void TearDown()
{
// Reset to default, just in case
Configuration.DateTimeFormat = "o";
Configuration.Default.DateTimeFormat = "o";
}
[Test ()]
@ -29,7 +29,7 @@ namespace SwaggerClientTest.TestApiClient
}
[Test ()]
public void TestParameterToString_DateTime ()
public void TestParameterToStringForDateTime ()
{
ApiClient api = new ApiClient ();
@ -40,17 +40,23 @@ namespace SwaggerClientTest.TestApiClient
// test datetime with no timezone
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));
}
// 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
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));
}
[Test ()]
public void TestParameterToString_DateTime_WithUFormat ()
public void TestParameterToStringForDateTimeWithUFormat ()
{
// Setup the DateTimeFormat across all of the calls
Configuration.DateTimeFormat = "u";
Configuration.Default.DateTimeFormat = "u";
ApiClient api = new ApiClient();
// test datetime
@ -59,10 +65,10 @@ namespace SwaggerClientTest.TestApiClient
}
[Test ()]
public void TestParameterToString_DateTime_WithCustomFormat ()
public void TestParameterToStringForDateTimeWithCustomFormat ()
{
// 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();
// test datetime

View File

@ -13,7 +13,7 @@ namespace SwaggerClientTest.TestConfiguration
public void TearDown ()
{
// Reset to default, just in case
Configuration.DateTimeFormat = "o";
Configuration.Default.DateTimeFormat = "o";
}
[Test ()]
@ -44,15 +44,24 @@ namespace SwaggerClientTest.TestConfiguration
{
// Should default to the Round-trip Format Specifier - "o"
// 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 ()]
public void TestDateTimeFormat_UType()
{
Configuration.DateTimeFormat = "u";
Configuration.Default.DateTimeFormat = "u";
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");
Assert.AreEqual("u", Configuration.DateTimeFormat);
}
[Test ()]

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/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/nunit.framework.dll
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll