fix file upload issue. add error handling for status code 0

This commit is contained in:
wing328 2015-07-03 17:39:27 +08:00
parent 55827fe516
commit 987a61640b
20 changed files with 175 additions and 56 deletions

View File

@ -41,11 +41,38 @@ namespace {{packageName}}.Client {
public Object CallApi(String path, RestSharp.Method method, Dictionary<String, String> queryParams, String postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, String[] authSettings) {
var response = Task.Run(async () => {
var resp = await CallApiAsync(path, method, queryParams, postBody, headerParams, formParams, fileParams, authSettings);
return resp;
});
return response.Result;
var request = new RestRequest(path, method);
UpdateParamsForAuth(queryParams, headerParams, authSettings);
// add default header, if any
foreach(KeyValuePair<string, string> defaultHeader in this.DefaultHeaderMap)
request.AddHeader(defaultHeader.Key, defaultHeader.Value);
// add header parameter, if any
foreach(KeyValuePair<string, string> param in headerParams)
request.AddHeader(param.Key, param.Value);
// add query parameter, if any
foreach(KeyValuePair<string, string> param in queryParams)
request.AddQueryParameter(param.Key, param.Value);
// add form parameter, if any
foreach(KeyValuePair<string, string> param in formParams)
request.AddParameter(param.Key, param.Value);
// add file parameter, if any
foreach(KeyValuePair<string, FileParameter> param in fileParams)
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
if (postBody != null) {
request.AddParameter("application/json", postBody, ParameterType.RequestBody); // http body (model) parameter
}
return (Object)RestClient.Execute(request);
}
public async Task<Object> CallApiAsync(String path, RestSharp.Method method, Dictionary<String, String> queryParams, String postBody,
@ -120,9 +147,9 @@ namespace {{packageName}}.Client {
public FileParameter ParameterToFile(string name, Stream stream)
{
if (stream is FileStream) {
return FileParameter.Create(name, StreamToByteArray(stream), ((FileStream)stream).Name);
return FileParameter.Create(name, StreamToByteArray(stream), Path.GetFileName(((FileStream)stream).Name));
} else {
return FileParameter.Create(name, StreamToByteArray(stream), "temp_name_here");
return FileParameter.Create(name, StreamToByteArray(stream), "no_file_name_provided");
}
}

View File

@ -117,6 +117,8 @@ namespace {{packageName}}.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.ErrorMessage, response.ErrorMessage);
}
{{#returnType}}return ({{{returnType}}}) ApiClient.Deserialize(response.Content, typeof({{{returnType}}}), response.Headers);{{/returnType}}{{^returnType}}return;{{/returnType}}

View File

@ -217,6 +217,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePet: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePet: " + response.ErrorMessage, response.ErrorMessage);
}
return;
@ -291,6 +293,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling AddPet: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling AddPet: " + response.ErrorMessage, response.ErrorMessage);
}
return;
@ -365,6 +369,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByStatus: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByStatus: " + response.ErrorMessage, response.ErrorMessage);
}
return (List<Pet>) ApiClient.Deserialize(response.Content, typeof(List<Pet>), response.Headers);
@ -438,6 +444,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByTags: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByTags: " + response.ErrorMessage, response.ErrorMessage);
}
return (List<Pet>) ApiClient.Deserialize(response.Content, typeof(List<Pet>), response.Headers);
@ -514,6 +522,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling GetPetById: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling GetPetById: " + response.ErrorMessage, response.ErrorMessage);
}
return (Pet) ApiClient.Deserialize(response.Content, typeof(Pet), response.Headers);
@ -596,6 +606,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePetWithForm: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePetWithForm: " + response.ErrorMessage, response.ErrorMessage);
}
return;
@ -681,6 +693,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling DeletePet: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling DeletePet: " + response.ErrorMessage, response.ErrorMessage);
}
return;
@ -766,6 +780,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling UploadFile: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling UploadFile: " + response.ErrorMessage, response.ErrorMessage);
}
return;

View File

@ -147,6 +147,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling GetInventory: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling GetInventory: " + response.ErrorMessage, response.ErrorMessage);
}
return (Dictionary<String, int?>) ApiClient.Deserialize(response.Content, typeof(Dictionary<String, int?>), response.Headers);
@ -218,6 +220,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling PlaceOrder: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling PlaceOrder: " + response.ErrorMessage, response.ErrorMessage);
}
return (Order) ApiClient.Deserialize(response.Content, typeof(Order), response.Headers);
@ -294,6 +298,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling GetOrderById: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling GetOrderById: " + response.ErrorMessage, response.ErrorMessage);
}
return (Order) ApiClient.Deserialize(response.Content, typeof(Order), response.Headers);
@ -372,6 +378,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling DeleteOrder: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling DeleteOrder: " + response.ErrorMessage, response.ErrorMessage);
}
return;

View File

@ -209,6 +209,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling CreateUser: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling CreateUser: " + response.ErrorMessage, response.ErrorMessage);
}
return;
@ -283,6 +285,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling CreateUsersWithArrayInput: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling CreateUsersWithArrayInput: " + response.ErrorMessage, response.ErrorMessage);
}
return;
@ -357,6 +361,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling CreateUsersWithListInput: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling CreateUsersWithListInput: " + response.ErrorMessage, response.ErrorMessage);
}
return;
@ -433,6 +439,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling LoginUser: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling LoginUser: " + response.ErrorMessage, response.ErrorMessage);
}
return (string) ApiClient.Deserialize(response.Content, typeof(string), response.Headers);
@ -506,6 +514,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling LogoutUser: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling LogoutUser: " + response.ErrorMessage, response.ErrorMessage);
}
return;
@ -581,6 +591,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling GetUserByName: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling GetUserByName: " + response.ErrorMessage, response.ErrorMessage);
}
return (User) ApiClient.Deserialize(response.Content, typeof(User), response.Headers);
@ -661,6 +673,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling UpdateUser: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling UpdateUser: " + response.ErrorMessage, response.ErrorMessage);
}
return;
@ -742,6 +756,8 @@ namespace IO.Swagger.Api {
if (((int)response.StatusCode) >= 400) {
throw new ApiException ((int)response.StatusCode, "Error calling DeleteUser: " + response.Content, response.Content);
} else if (((int)response.StatusCode) == 0) {
throw new ApiException ((int)response.StatusCode, "Error calling DeleteUser: " + response.ErrorMessage, response.ErrorMessage);
}
return;

View File

@ -41,11 +41,38 @@ namespace IO.Swagger.Client {
public Object CallApi(String path, RestSharp.Method method, Dictionary<String, String> queryParams, String postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, String[] authSettings) {
var response = Task.Run(async () => {
var resp = await CallApiAsync(path, method, queryParams, postBody, headerParams, formParams, fileParams, authSettings);
return resp;
});
return response.Result;
var request = new RestRequest(path, method);
UpdateParamsForAuth(queryParams, headerParams, authSettings);
// add default header, if any
foreach(KeyValuePair<string, string> defaultHeader in this.DefaultHeaderMap)
request.AddHeader(defaultHeader.Key, defaultHeader.Value);
// add header parameter, if any
foreach(KeyValuePair<string, string> param in headerParams)
request.AddHeader(param.Key, param.Value);
// add query parameter, if any
foreach(KeyValuePair<string, string> param in queryParams)
request.AddQueryParameter(param.Key, param.Value);
// add form parameter, if any
foreach(KeyValuePair<string, string> param in formParams)
request.AddParameter(param.Key, param.Value);
// add file parameter, if any
foreach(KeyValuePair<string, FileParameter> param in fileParams)
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
if (postBody != null) {
request.AddParameter("application/json", postBody, ParameterType.RequestBody); // http body (model) parameter
}
return (Object)RestClient.Execute(request);
}
public async Task<Object> CallApiAsync(String path, RestSharp.Method method, Dictionary<String, String> queryParams, String postBody,
@ -120,9 +147,9 @@ namespace IO.Swagger.Client {
public FileParameter ParameterToFile(string name, Stream stream)
{
if (stream is FileStream) {
return FileParameter.Create(name, StreamToByteArray(stream), ((FileStream)stream).Name);
return FileParameter.Create(name, StreamToByteArray(stream), Path.GetFileName(((FileStream)stream).Name));
} else {
return FileParameter.Create(name, StreamToByteArray(stream), "temp_name_here");
return FileParameter.Create(name, StreamToByteArray(stream), "no_file_name_provided");
}
}

View File

@ -29,9 +29,6 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="nunit.framework">
<HintPath>packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>Lib\SwaggerClient\bin\Newtonsoft.Json.dll</HintPath>
</Reference>
@ -40,6 +37,9 @@
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="nunit.framework">
<HintPath>packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Lib\SwaggerClient\src\main\csharp\IO\Swagger\Api\PetApi.cs" />
@ -57,10 +57,10 @@
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<None Include="packages.config" />
<None Include="Lib\SwaggerClient\compile.bat" />
<None Include="Lib\SwaggerClient\bin\Newtonsoft.Json.dll" />
<None Include="Lib\SwaggerClient\bin\RestSharp.dll" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Lib\" />

View File

@ -1,12 +1,11 @@
<Properties StartupItem="SwaggerClientTest.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs">
<MonoDevelop.Ide.Workbench ActiveDocument="TestPet.cs">
<Files>
<File FileName="TestPet.cs" Line="123" Column="16" />
<File FileName="TestPet.cs" Line="118" Column="3" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs" Line="1" Column="1" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs" Line="1" Column="1" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs" Line="1" Column="1" />
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs" Line="1" Column="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.3" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" />
</packages>

View File

@ -3581,49 +3581,49 @@
</member>
<member name="M:NUnit.Framework.CollectionAssert.IsNotSubsetOf(System.Collections.IEnumerable,System.Collections.IEnumerable)">
<summary>
Asserts that superset is not a subject of subset.
Asserts that the superset does not contain the subset
</summary>
<param name="subset">The IEnumerable superset to be considered</param>
<param name="superset">The IEnumerable subset to be considered</param>
<param name="subset">The IEnumerable subset to be considered</param>
<param name="superset">The IEnumerable superset to be considered</param>
</member>
<member name="M:NUnit.Framework.CollectionAssert.IsNotSubsetOf(System.Collections.IEnumerable,System.Collections.IEnumerable,System.String)">
<summary>
Asserts that superset is not a subject of subset.
Asserts that the superset does not contain the subset
</summary>
<param name="subset">The IEnumerable superset to be considered</param>
<param name="superset">The IEnumerable subset to be considered</param>
<param name="subset">The IEnumerable subset to be considered</param>
<param name="superset">The IEnumerable superset to be considered</param>
<param name="message">The message that will be displayed on failure</param>
</member>
<member name="M:NUnit.Framework.CollectionAssert.IsNotSubsetOf(System.Collections.IEnumerable,System.Collections.IEnumerable,System.String,System.Object[])">
<summary>
Asserts that superset is not a subject of subset.
Asserts that the superset does not contain the subset
</summary>
<param name="subset">The IEnumerable superset to be considered</param>
<param name="superset">The IEnumerable subset to be considered</param>
<param name="subset">The IEnumerable subset to be considered</param>
<param name="superset">The IEnumerable superset to be considered</param>
<param name="message">The message that will be displayed on failure</param>
<param name="args">Arguments to be used in formatting the message</param>
</member>
<member name="M:NUnit.Framework.CollectionAssert.IsSubsetOf(System.Collections.IEnumerable,System.Collections.IEnumerable)">
<summary>
Asserts that superset is a subset of subset.
Asserts that the superset contains the subset.
</summary>
<param name="subset">The IEnumerable superset to be considered</param>
<param name="superset">The IEnumerable subset to be considered</param>
<param name="subset">The IEnumerable subset to be considered</param>
<param name="superset">The IEnumerable superset to be considered</param>
</member>
<member name="M:NUnit.Framework.CollectionAssert.IsSubsetOf(System.Collections.IEnumerable,System.Collections.IEnumerable,System.String)">
<summary>
Asserts that superset is a subset of subset.
Asserts that the superset contains the subset.
</summary>
<param name="subset">The IEnumerable superset to be considered</param>
<param name="superset">The IEnumerable subset to be considered</param>
<param name="subset">The IEnumerable subset to be considered</param>
<param name="superset">The IEnumerable superset to be considered</param>
<param name="message">The message that will be displayed on failure</param>
</member>
<member name="M:NUnit.Framework.CollectionAssert.IsSubsetOf(System.Collections.IEnumerable,System.Collections.IEnumerable,System.String,System.Object[])">
<summary>
Asserts that superset is a subset of subset.
Asserts that the superset contains the subset.
</summary>
<param name="subset">The IEnumerable superset to be considered</param>
<param name="superset">The IEnumerable subset to be considered</param>
<param name="subset">The IEnumerable subset to be considered</param>
<param name="superset">The IEnumerable superset to be considered</param>
<param name="message">The message that will be displayed on failure</param>
<param name="args">Arguments to be used in formatting the message</param>
</member>
@ -6551,6 +6551,23 @@
</summary>
<returns>The target for the action attribute</returns>
</member>
<member name="M:NUnit.Framework.TestActionAttribute.BeforeTest(NUnit.Framework.TestDetails)">
<summary>
Method called before each test
</summary>
<param name="testDetails">Info about the test to be run</param>
</member>
<member name="M:NUnit.Framework.TestActionAttribute.AfterTest(NUnit.Framework.TestDetails)">
<summary>
Method called after each test
</summary>
<param name="testDetails">Info about the test that was just run</param>
</member>
<member name="P:NUnit.Framework.TestActionAttribute.Targets">
<summary>
Gets or sets the ActionTargets for this attribute
</summary>
</member>
<member name="T:NUnit.Framework.TestAttribute">
<summary>
Adding this attribute to a method within a <seealso cref="T:NUnit.Framework.TestFixtureAttribute"/>
@ -8066,7 +8083,7 @@
presence of a particular attribute on an object.
</summary>
</member>
<member name="M:NUnit.Framework.Constraints.ConstraintExpression.Matches(NUnit.Framework.Constraints.Constraint)">
<member name="M:NUnit.Framework.Constraints.ConstraintExpression.Matches(NUnit.Framework.Constraints.IResolveConstraint)">
<summary>
Returns the constraint provided as an argument - used to allow custom
custom constraints to easily participate in the syntax.
@ -10352,6 +10369,13 @@
<param name="actual">The value to be tested</param>
<returns>True if no exception is thrown, otherwise false</returns>
</member>
<member name="M:NUnit.Framework.Constraints.ThrowsNothingConstraint.Matches``1(NUnit.Framework.Constraints.ActualValueDelegate{``0})">
<summary>
Test whether the constraint is satisfied by a given delegate
</summary>
<param name="del">Delegate returning the value to be tested</param>
<returns>True if no exception is thrown, otherwise false</returns>
</member>
<member name="M:NUnit.Framework.Constraints.ThrowsNothingConstraint.WriteDescriptionTo(NUnit.Framework.Constraints.MessageWriter)">
<summary>
Write the constraint description to a MessageWriter

View File

@ -1,15 +1,15 @@
Copyright © 2002-2013 Charlie Poole
Copyright © 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov
Copyright © 2000-2002 Philip A. Craig
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment (see the following) in the product documentation is required.
Portions Copyright © 2002-2013 Charlie Poole or Copyright © 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov or Copyright © 2000-2002 Philip A. Craig
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Copyright © 2002-2014 Charlie Poole
Copyright © 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov
Copyright © 2000-2002 Philip A. Craig
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment (see the following) in the product documentation is required.
Portions Copyright © 2002-2014 Charlie Poole or Copyright © 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov or Copyright © 2000-2002 Philip A. Craig
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.