Merge pull request #1596 from wing328/csharp_auth_update

[C#] better authentication, minor format change
This commit is contained in:
wing328
2015-11-24 16:27:21 +08:00
9 changed files with 82 additions and 38 deletions

View File

@@ -198,10 +198,12 @@ namespace {{packageName}}.Client
{
if (obj is DateTime)
return ((DateTime)obj).ToString ("u");
else if (obj is IList) {
else if (obj is IList)
{
string flattenString = "";
string separator = ",";
foreach (var param in (IList)obj) {
foreach (var param in (IList)obj)
{
flattenString += param.ToString() + separator;
}
return flattenString.Remove(flattenString.Length - 1);;
@@ -318,11 +320,29 @@ namespace {{packageName}}.Client
{
{{#authMethods}}
case "{{name}}":
{{#isApiKey}}{{#isKeyInHeader}}headerParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInHeader}}{{#isKeyInQuery}}queryParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}headerParams["Authorization"] = "Basic " + Base64Encode(Configuration.Username + ":" + Configuration.Password);{{/isBasic}}{{#isOAuth}}headerParams["Authorization"] = "Bearer " + Configuration.AccessToken;{{/isOAuth}}
{{#isApiKey}}{{#isKeyInHeader}}
var apiKeyValue = GetApiKeyWithPrefix("{{keyParamName}}");
if (!String.IsNullOrEmpty(apiKeyValue))
{
headerParams["{{keyParamName}}"] = apiKeyValue;
}{{/isKeyInHeader}}{{#isKeyInQuery}}
var apiKeyValue = GetApiKeyWithPrefix("{{keyParamName}}");
if (!String.IsNullOrEmpty(apiKeyValue))
{
queryParams["{{keyParamName}}"] = apiKeyValue;
}{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}
if (!String.IsNullOrEmpty(Configuration.Username) || !String.IsNullOrEmpty(Configuration.Password))
{
headerParams["Authorization"] = "Basic " + Base64Encode(Configuration.Username + ":" + Configuration.Password);
}{{/isBasic}}{{#isOAuth}}
if (!String.IsNullOrEmpty(Configuration.AccessToken))
{
headerParams["Authorization"] = "Bearer " + Configuration.AccessToken;
}{{/isOAuth}}
break;
{{/authMethods}}
default:
//TODO show warning about security definition not found
//show warning about security definition not found
break;
}
}
@@ -335,11 +355,14 @@ namespace {{packageName}}.Client
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
public String SelectHeaderAccept(String[] accepts) {
public String SelectHeaderAccept(String[] accepts)
{
if (accepts.Length == 0)
return null;
if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";
return String.Join(",", accepts);
}
@@ -350,8 +373,7 @@ namespace {{packageName}}.Client
/// <returns>Encoded string.</returns>
public static string Base64Encode(string text)
{
var textByte = System.Text.Encoding.UTF8.GetBytes(text);
return System.Convert.ToBase64String(textByte);
return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text));
}
/// <summary>
@@ -361,7 +383,8 @@ namespace {{packageName}}.Client
/// <param name="source">Object to be casted</param>
/// <param name="dest">Target type</param>
/// <returns>Casted object</returns>
public static dynamic ConvertType(dynamic source, Type dest) {
public static dynamic ConvertType(dynamic source, Type dest)
{
return Convert.ChangeType(source, dest);
}

View File

@@ -198,10 +198,12 @@ namespace IO.Swagger.Client
{
if (obj is DateTime)
return ((DateTime)obj).ToString ("u");
else if (obj is IList) {
else if (obj is IList)
{
string flattenString = "";
string separator = ",";
foreach (var param in (IList)obj) {
foreach (var param in (IList)obj)
{
flattenString += param.ToString() + separator;
}
return flattenString.Remove(flattenString.Length - 1);;
@@ -318,15 +320,24 @@ namespace IO.Swagger.Client
{
case "api_key":
headerParams["api_key"] = GetApiKeyWithPrefix("api_key");
var apiKeyValue = GetApiKeyWithPrefix("api_key");
if (!String.IsNullOrEmpty(apiKeyValue))
{
headerParams["api_key"] = apiKeyValue;
}
break;
case "petstore_auth":
headerParams["Authorization"] = "Bearer " + Configuration.AccessToken;
if (!String.IsNullOrEmpty(Configuration.AccessToken))
{
headerParams["Authorization"] = "Bearer " + Configuration.AccessToken;
}
break;
default:
//TODO show warning about security definition not found
//show warning about security definition not found
break;
}
}
@@ -339,11 +350,14 @@ namespace IO.Swagger.Client
/// </summary>
/// <param name="accepts">The accepts array to select from.</param>
/// <returns>The Accept header to use.</returns>
public String SelectHeaderAccept(String[] accepts) {
public String SelectHeaderAccept(String[] accepts)
{
if (accepts.Length == 0)
return null;
if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";
return String.Join(",", accepts);
}
@@ -354,8 +368,7 @@ namespace IO.Swagger.Client
/// <returns>Encoded string.</returns>
public static string Base64Encode(string text)
{
var textByte = System.Text.Encoding.UTF8.GetBytes(text);
return System.Convert.ToBase64String(textByte);
return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text));
}
/// <summary>
@@ -365,7 +378,8 @@ namespace IO.Swagger.Client
/// <param name="source">Object to be casted</param>
/// <param name="dest">Target type</param>
/// <returns>Casted object</returns>
public static dynamic ConvertType(dynamic source, Type dest) {
public static dynamic ConvertType(dynamic source, Type dest)
{
return Convert.ChangeType(source, dest);
}

View File

@@ -4,23 +4,6 @@
<Files>
<File FileName="TestPet.cs" Line="8" Column="25" />
</Files>
<Pads>
<Pad Id="MonoDevelop.NUnit.TestPad">
<State name="__root__">
<Node name="SwaggerClientTest" expanded="True">
<Node name="SwaggerClientTest" expanded="True">
<Node name="SwaggerClient" expanded="True">
<Node name="TestPet" expanded="True">
<Node name="TestPet" expanded="True">
<Node name="TestEqual" selected="True" />
</Node>
</Node>
</Node>
</Node>
</Node>
</State>
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />

View File

@@ -49,7 +49,9 @@ namespace SwaggerClient.TestPet
petApi.DeletePet(petId, "test key");
}
/// <summary>
/// Test GetPetByIdAsync
/// </summary>
[Test ()]
public void TestGetPetByIdAsync ()
{
@@ -74,6 +76,9 @@ namespace SwaggerClient.TestPet
}
/// <summary>
/// Test GetPetById
/// </summary>
[Test ()]
public void TestGetPetById ()
{
@@ -97,6 +102,9 @@ namespace SwaggerClient.TestPet
}
/// <summary>
/// Test UpdatePetWithForm
/// </summary>
[Test ()]
public void TestUpdatePetWithForm ()
{
@@ -115,6 +123,9 @@ namespace SwaggerClient.TestPet
Assert.AreEqual (56, response.Category.Id);
}
/// <summary>
/// Test UploadFile
/// </summary>
[Test ()]
public void TestUploadFile ()
{
@@ -129,7 +140,9 @@ namespace SwaggerClient.TestPet
}
/// <summary>
/// Test FindPetByStatus
/// </summary>
[Test ()]
public void TestFindPetByStatus ()
{
@@ -145,6 +158,9 @@ namespace SwaggerClient.TestPet
}
/// <summary>
/// Test Equal
/// </summary>
[Test ()]
public void TestEqual()
{

View File

@@ -1,8 +1,16 @@
/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs
/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb
/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll
/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll
/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb
/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll
/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll
/Users/williamcheng/Code/csharp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll
/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/bin/Debug/SwaggerClientTest.dll.mdb
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb
/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
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll