forked from loafle/openapi-generator-original
Add unity 2019 support and update samples (#21036)
This commit is contained in:
parent
6b13ad522f
commit
047ceeaa45
@ -924,6 +924,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
|
||||
addSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir, authPackageDir);
|
||||
supportingFiles.add(new SupportingFile("ConnectionException.mustache", clientPackageDir, "ConnectionException.cs"));
|
||||
supportingFiles.add(new SupportingFile("UnexpectedResponseException.mustache", clientPackageDir, "UnexpectedResponseException.cs"));
|
||||
supportingFiles.add(new SupportingFile("UnityWebRequestAwaiterExtension.mustache", clientPackageDir, "UnityWebRequestAwaiterExtension.cs"));
|
||||
break;
|
||||
default: // generichost
|
||||
addGenericHostSupportingFiles(clientPackageDir, packageFolder, excludeTests, testPackageFolder, testPackageName, modelPackageDir);
|
||||
|
@ -377,10 +377,18 @@ namespace {{packageName}}.Client
|
||||
|
||||
using (request)
|
||||
{
|
||||
{{#useIntForTimeout}}
|
||||
if (configuration.Timeout > 0)
|
||||
{
|
||||
request.timeout = (int)Math.Ceiling(configuration.Timeout / 1000.0f);
|
||||
request.timeout = configuration.Timeout;
|
||||
}
|
||||
{{/useIntForTimeout}}
|
||||
{{^useIntForTimeout}}
|
||||
if (configuration.Timeout > TimeSpan.Zero)
|
||||
{
|
||||
request.timeout = (int)Math.Ceiling(configuration.Timeout.TotalSeconds);
|
||||
}
|
||||
{{/useIntForTimeout}}
|
||||
|
||||
if (configuration.Proxy != null)
|
||||
{
|
||||
@ -400,21 +408,34 @@ namespace {{packageName}}.Client
|
||||
|
||||
InterceptRequest(request, path, options, configuration);
|
||||
|
||||
#if UNITY_2020_2_OR_NEWER
|
||||
// For Unity 2020.2 and newer, use UnityWebRequest.Result.
|
||||
var asyncOp = request.SendWebRequest();
|
||||
|
||||
TaskCompletionSource<UnityWebRequest.Result> tsc = new TaskCompletionSource<UnityWebRequest.Result>();
|
||||
asyncOp.completed += (_) => tsc.TrySetResult(request.result);
|
||||
|
||||
TaskCompletionSource<UnityWebRequest.Result> tcs = new TaskCompletionSource<UnityWebRequest.Result>();
|
||||
asyncOp.completed += (_) => tcs.TrySetResult(request.result);
|
||||
using (var tokenRegistration = cancellationToken.Register(request.Abort, true))
|
||||
{
|
||||
await tsc.Task;
|
||||
await tcs.Task;
|
||||
}
|
||||
|
||||
|
||||
if (request.result == UnityWebRequest.Result.ConnectionError ||
|
||||
request.result == UnityWebRequest.Result.DataProcessingError)
|
||||
{
|
||||
throw new ConnectionException(request);
|
||||
}
|
||||
#else
|
||||
// For Unity 2019 and earlier, await the operation directly.
|
||||
var asyncOp = request.SendWebRequest();
|
||||
using (var tokenRegistration = cancellationToken.Register(request.Abort, true))
|
||||
{
|
||||
await asyncOp;
|
||||
}
|
||||
|
||||
if (request.isNetworkError || request.isHttpError)
|
||||
{
|
||||
throw new ConnectionException(request);
|
||||
}
|
||||
#endif
|
||||
|
||||
object responseData = deserializer.Deserialize<T>(request);
|
||||
|
||||
@ -636,4 +657,4 @@ namespace {{packageName}}.Client
|
||||
}
|
||||
#endregion ISynchronousClient
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using UnityEngine.Networking;
|
||||
|
||||
namespace {{packageName}}.Client
|
||||
{
|
||||
#if UNITY_2020_1_OR_NEWER
|
||||
public class ConnectionException : Exception
|
||||
{
|
||||
public UnityWebRequest.Result Result { get; private set; }
|
||||
@ -18,4 +19,39 @@ namespace {{packageName}}.Client
|
||||
Error = request.error ?? "";
|
||||
}
|
||||
}
|
||||
#else
|
||||
// For Unity 2019, define a custom enum that roughly simulates UnityWebRequest.Result.
|
||||
public enum UnityWebRequestResultLegacy
|
||||
{
|
||||
Success,
|
||||
ConnectionError,
|
||||
ProtocolError,
|
||||
DataProcessingError
|
||||
}
|
||||
|
||||
public class ConnectionException : Exception
|
||||
{
|
||||
public UnityWebRequestResultLegacy Result { get; private set; }
|
||||
public string Error { get; private set; }
|
||||
|
||||
public ConnectionException(UnityWebRequest request)
|
||||
: base($"Error: {request.error}")
|
||||
{
|
||||
if (request.isNetworkError)
|
||||
{
|
||||
Result = UnityWebRequestResultLegacy.ConnectionError;
|
||||
}
|
||||
else if (request.isHttpError)
|
||||
{
|
||||
Result = UnityWebRequestResultLegacy.ProtocolError;
|
||||
}
|
||||
else
|
||||
{
|
||||
Result = UnityWebRequestResultLegacy.Success;
|
||||
}
|
||||
|
||||
Error = request.error ?? "";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ This C# SDK is automatically generated by the [OpenAPI Generator](https://openap
|
||||
<a id="version-support"></a>
|
||||
## Version support
|
||||
This generator should support all current LTS versions of Unity
|
||||
- Unity 2020.3 (LTS) and up
|
||||
- Unity 2019.4 (LTS) and up
|
||||
- .NET Standard 2.1 / .NET Framework
|
||||
|
||||
<a id="dependencies"></a>
|
||||
|
@ -0,0 +1,23 @@
|
||||
{{>partial_header}}
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace {{packageName}}.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an awaiter for UnityWebRequestAsyncOperation to allow `await request.SendWebRequest()`
|
||||
/// in Unity async methods. Unity's AsyncOperation types are not awaitable by default, so this
|
||||
/// extension bridges that gap by converting the operation into a Task.
|
||||
/// </summary>
|
||||
public static class UnityWebRequestAwaiterExtensions
|
||||
{
|
||||
public static TaskAwaiter<UnityWebRequestAsyncOperation> GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<UnityWebRequestAsyncOperation>();
|
||||
asyncOp.completed += _ => tcs.SetResult(asyncOp);
|
||||
return tcs.Task.GetAwaiter();
|
||||
}
|
||||
}
|
||||
}
|
@ -137,6 +137,7 @@ src/Org.OpenAPITools/Client/Multimap.cs
|
||||
src/Org.OpenAPITools/Client/OpenAPIDateConverter.cs
|
||||
src/Org.OpenAPITools/Client/RequestOptions.cs
|
||||
src/Org.OpenAPITools/Client/UnexpectedResponseException.cs
|
||||
src/Org.OpenAPITools/Client/UnityWebRequestAwaiterExtension.cs
|
||||
src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs
|
||||
src/Org.OpenAPITools/Model/AbstractOpenAPISchema.cs
|
||||
src/Org.OpenAPITools/Model/Activity.cs
|
||||
|
@ -12,7 +12,7 @@ This C# SDK is automatically generated by the [OpenAPI Generator](https://openap
|
||||
<a id="version-support"></a>
|
||||
## Version support
|
||||
This generator should support all current LTS versions of Unity
|
||||
- Unity 2020.3 (LTS) and up
|
||||
- Unity 2019.4 (LTS) and up
|
||||
- .NET Standard 2.1 / .NET Framework
|
||||
|
||||
<a id="dependencies"></a>
|
||||
|
@ -385,9 +385,9 @@ namespace Org.OpenAPITools.Client
|
||||
|
||||
using (request)
|
||||
{
|
||||
if (configuration.Timeout > 0)
|
||||
if (configuration.Timeout > TimeSpan.Zero)
|
||||
{
|
||||
request.timeout = (int)Math.Ceiling(configuration.Timeout / 1000.0f);
|
||||
request.timeout = (int)Math.Ceiling(configuration.Timeout.TotalSeconds);
|
||||
}
|
||||
|
||||
if (configuration.Proxy != null)
|
||||
@ -408,21 +408,34 @@ namespace Org.OpenAPITools.Client
|
||||
|
||||
InterceptRequest(request, path, options, configuration);
|
||||
|
||||
#if UNITY_2020_2_OR_NEWER
|
||||
// For Unity 2020.2 and newer, use UnityWebRequest.Result.
|
||||
var asyncOp = request.SendWebRequest();
|
||||
|
||||
TaskCompletionSource<UnityWebRequest.Result> tsc = new TaskCompletionSource<UnityWebRequest.Result>();
|
||||
asyncOp.completed += (_) => tsc.TrySetResult(request.result);
|
||||
|
||||
TaskCompletionSource<UnityWebRequest.Result> tcs = new TaskCompletionSource<UnityWebRequest.Result>();
|
||||
asyncOp.completed += (_) => tcs.TrySetResult(request.result);
|
||||
using (var tokenRegistration = cancellationToken.Register(request.Abort, true))
|
||||
{
|
||||
await tsc.Task;
|
||||
await tcs.Task;
|
||||
}
|
||||
|
||||
|
||||
if (request.result == UnityWebRequest.Result.ConnectionError ||
|
||||
request.result == UnityWebRequest.Result.DataProcessingError)
|
||||
{
|
||||
throw new ConnectionException(request);
|
||||
}
|
||||
#else
|
||||
// For Unity 2019 and earlier, await the operation directly.
|
||||
var asyncOp = request.SendWebRequest();
|
||||
using (var tokenRegistration = cancellationToken.Register(request.Abort, true))
|
||||
{
|
||||
await asyncOp;
|
||||
}
|
||||
|
||||
if (request.isNetworkError || request.isHttpError)
|
||||
{
|
||||
throw new ConnectionException(request);
|
||||
}
|
||||
#endif
|
||||
|
||||
object responseData = deserializer.Deserialize<T>(request);
|
||||
|
||||
@ -642,4 +655,4 @@ namespace Org.OpenAPITools.Client
|
||||
}
|
||||
#endregion ISynchronousClient
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ using UnityEngine.Networking;
|
||||
|
||||
namespace Org.OpenAPITools.Client
|
||||
{
|
||||
#if UNITY_2020_1_OR_NEWER
|
||||
public class ConnectionException : Exception
|
||||
{
|
||||
public UnityWebRequest.Result Result { get; private set; }
|
||||
@ -26,4 +27,39 @@ namespace Org.OpenAPITools.Client
|
||||
Error = request.error ?? "";
|
||||
}
|
||||
}
|
||||
#else
|
||||
// For Unity 2019, define a custom enum that roughly simulates UnityWebRequest.Result.
|
||||
public enum UnityWebRequestResultLegacy
|
||||
{
|
||||
Success,
|
||||
ConnectionError,
|
||||
ProtocolError,
|
||||
DataProcessingError
|
||||
}
|
||||
|
||||
public class ConnectionException : Exception
|
||||
{
|
||||
public UnityWebRequestResultLegacy Result { get; private set; }
|
||||
public string Error { get; private set; }
|
||||
|
||||
public ConnectionException(UnityWebRequest request)
|
||||
: base($"Error: {request.error}")
|
||||
{
|
||||
if (request.isNetworkError)
|
||||
{
|
||||
Result = UnityWebRequestResultLegacy.ConnectionError;
|
||||
}
|
||||
else if (request.isHttpError)
|
||||
{
|
||||
Result = UnityWebRequestResultLegacy.ProtocolError;
|
||||
}
|
||||
else
|
||||
{
|
||||
Result = UnityWebRequestResultLegacy.Success;
|
||||
}
|
||||
|
||||
Error = request.error ?? "";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* OpenAPI Petstore
|
||||
*
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
* Generated by: https://github.com/openapitools/openapi-generator.git
|
||||
*/
|
||||
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace Org.OpenAPITools.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an awaiter for UnityWebRequestAsyncOperation to allow `await request.SendWebRequest()`
|
||||
/// in Unity async methods. Unity's AsyncOperation types are not awaitable by default, so this
|
||||
/// extension bridges that gap by converting the operation into a Task.
|
||||
/// </summary>
|
||||
public static class UnityWebRequestAwaiterExtensions
|
||||
{
|
||||
public static TaskAwaiter<UnityWebRequestAsyncOperation> GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<UnityWebRequestAsyncOperation>();
|
||||
asyncOp.completed += _ => tcs.SetResult(asyncOp);
|
||||
return tcs.Task.GetAwaiter();
|
||||
}
|
||||
}
|
||||
}
|
@ -137,6 +137,7 @@ src/Org.OpenAPITools/Client/Multimap.cs
|
||||
src/Org.OpenAPITools/Client/OpenAPIDateConverter.cs
|
||||
src/Org.OpenAPITools/Client/RequestOptions.cs
|
||||
src/Org.OpenAPITools/Client/UnexpectedResponseException.cs
|
||||
src/Org.OpenAPITools/Client/UnityWebRequestAwaiterExtension.cs
|
||||
src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs
|
||||
src/Org.OpenAPITools/Model/AbstractOpenAPISchema.cs
|
||||
src/Org.OpenAPITools/Model/Activity.cs
|
||||
|
@ -12,7 +12,7 @@ This C# SDK is automatically generated by the [OpenAPI Generator](https://openap
|
||||
<a id="version-support"></a>
|
||||
## Version support
|
||||
This generator should support all current LTS versions of Unity
|
||||
- Unity 2020.3 (LTS) and up
|
||||
- Unity 2019.4 (LTS) and up
|
||||
- .NET Standard 2.1 / .NET Framework
|
||||
|
||||
<a id="dependencies"></a>
|
||||
|
@ -385,9 +385,9 @@ namespace Org.OpenAPITools.Client
|
||||
|
||||
using (request)
|
||||
{
|
||||
if (configuration.Timeout > 0)
|
||||
if (configuration.Timeout > TimeSpan.Zero)
|
||||
{
|
||||
request.timeout = (int)Math.Ceiling(configuration.Timeout / 1000.0f);
|
||||
request.timeout = (int)Math.Ceiling(configuration.Timeout.TotalSeconds);
|
||||
}
|
||||
|
||||
if (configuration.Proxy != null)
|
||||
@ -408,21 +408,34 @@ namespace Org.OpenAPITools.Client
|
||||
|
||||
InterceptRequest(request, path, options, configuration);
|
||||
|
||||
#if UNITY_2020_2_OR_NEWER
|
||||
// For Unity 2020.2 and newer, use UnityWebRequest.Result.
|
||||
var asyncOp = request.SendWebRequest();
|
||||
|
||||
TaskCompletionSource<UnityWebRequest.Result> tsc = new TaskCompletionSource<UnityWebRequest.Result>();
|
||||
asyncOp.completed += (_) => tsc.TrySetResult(request.result);
|
||||
|
||||
TaskCompletionSource<UnityWebRequest.Result> tcs = new TaskCompletionSource<UnityWebRequest.Result>();
|
||||
asyncOp.completed += (_) => tcs.TrySetResult(request.result);
|
||||
using (var tokenRegistration = cancellationToken.Register(request.Abort, true))
|
||||
{
|
||||
await tsc.Task;
|
||||
await tcs.Task;
|
||||
}
|
||||
|
||||
|
||||
if (request.result == UnityWebRequest.Result.ConnectionError ||
|
||||
request.result == UnityWebRequest.Result.DataProcessingError)
|
||||
{
|
||||
throw new ConnectionException(request);
|
||||
}
|
||||
#else
|
||||
// For Unity 2019 and earlier, await the operation directly.
|
||||
var asyncOp = request.SendWebRequest();
|
||||
using (var tokenRegistration = cancellationToken.Register(request.Abort, true))
|
||||
{
|
||||
await asyncOp;
|
||||
}
|
||||
|
||||
if (request.isNetworkError || request.isHttpError)
|
||||
{
|
||||
throw new ConnectionException(request);
|
||||
}
|
||||
#endif
|
||||
|
||||
object responseData = deserializer.Deserialize<T>(request);
|
||||
|
||||
@ -642,4 +655,4 @@ namespace Org.OpenAPITools.Client
|
||||
}
|
||||
#endregion ISynchronousClient
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ using UnityEngine.Networking;
|
||||
|
||||
namespace Org.OpenAPITools.Client
|
||||
{
|
||||
#if UNITY_2020_1_OR_NEWER
|
||||
public class ConnectionException : Exception
|
||||
{
|
||||
public UnityWebRequest.Result Result { get; private set; }
|
||||
@ -26,4 +27,39 @@ namespace Org.OpenAPITools.Client
|
||||
Error = request.error ?? "";
|
||||
}
|
||||
}
|
||||
#else
|
||||
// For Unity 2019, define a custom enum that roughly simulates UnityWebRequest.Result.
|
||||
public enum UnityWebRequestResultLegacy
|
||||
{
|
||||
Success,
|
||||
ConnectionError,
|
||||
ProtocolError,
|
||||
DataProcessingError
|
||||
}
|
||||
|
||||
public class ConnectionException : Exception
|
||||
{
|
||||
public UnityWebRequestResultLegacy Result { get; private set; }
|
||||
public string Error { get; private set; }
|
||||
|
||||
public ConnectionException(UnityWebRequest request)
|
||||
: base($"Error: {request.error}")
|
||||
{
|
||||
if (request.isNetworkError)
|
||||
{
|
||||
Result = UnityWebRequestResultLegacy.ConnectionError;
|
||||
}
|
||||
else if (request.isHttpError)
|
||||
{
|
||||
Result = UnityWebRequestResultLegacy.ProtocolError;
|
||||
}
|
||||
else
|
||||
{
|
||||
Result = UnityWebRequestResultLegacy.Success;
|
||||
}
|
||||
|
||||
Error = request.error ?? "";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* OpenAPI Petstore
|
||||
*
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
* Generated by: https://github.com/openapitools/openapi-generator.git
|
||||
*/
|
||||
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace Org.OpenAPITools.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an awaiter for UnityWebRequestAsyncOperation to allow `await request.SendWebRequest()`
|
||||
/// in Unity async methods. Unity's AsyncOperation types are not awaitable by default, so this
|
||||
/// extension bridges that gap by converting the operation into a Task.
|
||||
/// </summary>
|
||||
public static class UnityWebRequestAwaiterExtensions
|
||||
{
|
||||
public static TaskAwaiter<UnityWebRequestAsyncOperation> GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<UnityWebRequestAsyncOperation>();
|
||||
asyncOp.completed += _ => tcs.SetResult(asyncOp);
|
||||
return tcs.Task.GetAwaiter();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user