forked from loafle/openapi-generator-original
feat(csharp): adding retry configuration (#5929)
* feat(csharp): adding retry configuration * fix(csharp-retry) : adding retry policy through configuration * fix(csharp): moving RetryConfiguration out of Configuration class * fix(csharp): minor changes * fix(csharp): fixing tabs * fix(csharp): reverting FILES * fix(csharp) :reverting some file changes * fix(csharp): reverting changes * update samples * fix(csharp-retry) : fixing merge conflicts and build failure * fix(chsarp-retry) : FILES changes * update samples Co-authored-by: William Cheng <wing328hk@gmail.com>
This commit is contained in:
parent
98c606c32d
commit
ed1e30e75e
@ -616,6 +616,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
|
||||
supportingFiles.add(new SupportingFile("ISynchronousClient.mustache", clientPackageDir, "ISynchronousClient.cs"));
|
||||
supportingFiles.add(new SupportingFile("RequestOptions.mustache", clientPackageDir, "RequestOptions.cs"));
|
||||
supportingFiles.add(new SupportingFile("Multimap.mustache", clientPackageDir, "Multimap.cs"));
|
||||
supportingFiles.add(new SupportingFile("RetryConfiguration.mustache", clientPackageDir, "RetryConfiguration.cs"));
|
||||
|
||||
supportingFiles.add(new SupportingFile("IReadableConfiguration.mustache",
|
||||
clientPackageDir, "IReadableConfiguration.cs"));
|
||||
|
@ -13,6 +13,7 @@ using System.Net;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
@ -21,6 +22,7 @@ using RestSharp;
|
||||
using RestSharp.Deserializers;
|
||||
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
|
||||
using RestSharpMethod = RestSharp.Method;
|
||||
using Polly;
|
||||
|
||||
namespace {{packageName}}.Client
|
||||
{
|
||||
@ -419,7 +421,21 @@ namespace {{packageName}}.Client
|
||||
|
||||
InterceptRequest(req);
|
||||
|
||||
var response = client.Execute<T>(req);
|
||||
IRestResponse<T> response;
|
||||
if (RetryConfiguration.RetryPolicy != null)
|
||||
{
|
||||
var policy = RetryConfiguration.RetryPolicy;
|
||||
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req));
|
||||
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
|
||||
{
|
||||
Request = req,
|
||||
ErrorException = policyResult.FinalException
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
response = client.Execute<T>(req);
|
||||
}
|
||||
|
||||
InterceptResponse(req, response);
|
||||
|
||||
@ -503,7 +519,21 @@ namespace {{packageName}}.Client
|
||||
|
||||
InterceptRequest(req);
|
||||
|
||||
var response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false);
|
||||
IRestResponse<T> response;
|
||||
if (RetryConfiguration.AsyncRetryPolicy != null)
|
||||
{
|
||||
var policy = RetryConfiguration.AsyncRetryPolicy;
|
||||
var policyResult = await policy.ExecuteAndCaptureAsync(() => client.ExecuteAsync(req, cancellationToken)).ConfigureAwait(false);
|
||||
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
|
||||
{
|
||||
Request = req,
|
||||
ErrorException = policyResult.FinalException
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
InterceptResponse(req, response);
|
||||
|
||||
@ -750,4 +780,4 @@ namespace {{packageName}}.Client
|
||||
}
|
||||
#endregion ISynchronousClient
|
||||
}
|
||||
}
|
||||
}
|
@ -417,4 +417,4 @@ namespace {{packageName}}.Client
|
||||
}
|
||||
#endregion Static Members
|
||||
}
|
||||
}
|
||||
}
|
@ -97,4 +97,4 @@ namespace {{packageName}}.Client
|
||||
/// <value>X509 Certificate collection.</value>
|
||||
X509CertificateCollection ClientCertificates { get; }
|
||||
}
|
||||
}
|
||||
}
|
21
modules/openapi-generator/src/main/resources/csharp-netcore/RetryConfiguration.mustache
vendored
Normal file
21
modules/openapi-generator/src/main/resources/csharp-netcore/RetryConfiguration.mustache
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
using Polly.Retry;
|
||||
using RestSharp;
|
||||
|
||||
namespace {{packageName}}.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration class to set the polly retry policies to be applied to the requests.
|
||||
/// </summary>
|
||||
public class RetryConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// Retry policy
|
||||
/// </summary>
|
||||
public static RetryPolicy<IRestResponse> RetryPolicy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Async retry policy
|
||||
/// </summary>
|
||||
public static AsyncRetryPolicy<IRestResponse> AsyncRetryPolicy { get; set; }
|
||||
}
|
||||
}
|
@ -28,6 +28,7 @@
|
||||
<PackageReference Include="JsonSubTypes" Version="1.7.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="RestSharp" Version="106.11.4" />
|
||||
<PackageReference Include="Polly" Version="7.2.0" />
|
||||
{{#validatable}}
|
||||
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
|
||||
{{/validatable}}
|
||||
|
@ -81,6 +81,7 @@ src/Org.OpenAPITools/Client/ISynchronousClient.cs
|
||||
src/Org.OpenAPITools/Client/Multimap.cs
|
||||
src/Org.OpenAPITools/Client/OpenAPIDateConverter.cs
|
||||
src/Org.OpenAPITools/Client/RequestOptions.cs
|
||||
src/Org.OpenAPITools/Client/RetryConfiguration.cs
|
||||
src/Org.OpenAPITools/Model/AdditionalPropertiesClass.cs
|
||||
src/Org.OpenAPITools/Model/Animal.cs
|
||||
src/Org.OpenAPITools/Model/ApiResponse.cs
|
||||
|
@ -18,6 +18,7 @@ using System.Net;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
@ -26,6 +27,7 @@ using RestSharp;
|
||||
using RestSharp.Deserializers;
|
||||
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
|
||||
using RestSharpMethod = RestSharp.Method;
|
||||
using Polly;
|
||||
|
||||
namespace Org.OpenAPITools.Client
|
||||
{
|
||||
@ -423,7 +425,21 @@ namespace Org.OpenAPITools.Client
|
||||
|
||||
InterceptRequest(req);
|
||||
|
||||
var response = client.Execute<T>(req);
|
||||
IRestResponse<T> response;
|
||||
if (RetryConfiguration.RetryPolicy != null)
|
||||
{
|
||||
var policy = RetryConfiguration.RetryPolicy;
|
||||
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req));
|
||||
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
|
||||
{
|
||||
Request = req,
|
||||
ErrorException = policyResult.FinalException
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
response = client.Execute<T>(req);
|
||||
}
|
||||
|
||||
InterceptResponse(req, response);
|
||||
|
||||
@ -506,7 +522,21 @@ namespace Org.OpenAPITools.Client
|
||||
|
||||
InterceptRequest(req);
|
||||
|
||||
var response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false);
|
||||
IRestResponse<T> response;
|
||||
if (RetryConfiguration.AsyncRetryPolicy != null)
|
||||
{
|
||||
var policy = RetryConfiguration.AsyncRetryPolicy;
|
||||
var policyResult = await policy.ExecuteAndCaptureAsync(() => client.ExecuteAsync(req, cancellationToken)).ConfigureAwait(false);
|
||||
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
|
||||
{
|
||||
Request = req,
|
||||
ErrorException = policyResult.FinalException
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
InterceptResponse(req, response);
|
||||
|
||||
@ -752,4 +782,4 @@ namespace Org.OpenAPITools.Client
|
||||
}
|
||||
#endregion ISynchronousClient
|
||||
}
|
||||
}
|
||||
}
|
@ -413,4 +413,4 @@ namespace Org.OpenAPITools.Client
|
||||
}
|
||||
#endregion Static Members
|
||||
}
|
||||
}
|
||||
}
|
@ -105,4 +105,4 @@ namespace Org.OpenAPITools.Client
|
||||
/// <value>X509 Certificate collection.</value>
|
||||
X509CertificateCollection ClientCertificates { get; }
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
using Polly.Retry;
|
||||
using RestSharp;
|
||||
|
||||
namespace Org.OpenAPITools.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration class to set the polly retry policies to be applied to the requests.
|
||||
/// </summary>
|
||||
public class RetryConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// Retry policy
|
||||
/// </summary>
|
||||
public static RetryPolicy<IRestResponse> RetryPolicy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Async retry policy
|
||||
/// </summary>
|
||||
public static AsyncRetryPolicy<IRestResponse> AsyncRetryPolicy { get; set; }
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@
|
||||
<PackageReference Include="JsonSubTypes" Version="1.7.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="RestSharp" Version="106.11.4" />
|
||||
<PackageReference Include="Polly" Version="7.2.0" />
|
||||
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -81,6 +81,7 @@ src/Org.OpenAPITools/Client/ISynchronousClient.cs
|
||||
src/Org.OpenAPITools/Client/Multimap.cs
|
||||
src/Org.OpenAPITools/Client/OpenAPIDateConverter.cs
|
||||
src/Org.OpenAPITools/Client/RequestOptions.cs
|
||||
src/Org.OpenAPITools/Client/RetryConfiguration.cs
|
||||
src/Org.OpenAPITools/Model/AdditionalPropertiesClass.cs
|
||||
src/Org.OpenAPITools/Model/Animal.cs
|
||||
src/Org.OpenAPITools/Model/ApiResponse.cs
|
||||
|
@ -19,6 +19,7 @@ using System.Net;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
@ -27,6 +28,7 @@ using RestSharp;
|
||||
using RestSharp.Deserializers;
|
||||
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
|
||||
using RestSharpMethod = RestSharp.Method;
|
||||
using Polly;
|
||||
|
||||
namespace Org.OpenAPITools.Client
|
||||
{
|
||||
@ -424,7 +426,21 @@ namespace Org.OpenAPITools.Client
|
||||
|
||||
InterceptRequest(req);
|
||||
|
||||
var response = client.Execute<T>(req);
|
||||
IRestResponse<T> response;
|
||||
if (RetryConfiguration.RetryPolicy != null)
|
||||
{
|
||||
var policy = RetryConfiguration.RetryPolicy;
|
||||
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req));
|
||||
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
|
||||
{
|
||||
Request = req,
|
||||
ErrorException = policyResult.FinalException
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
response = client.Execute<T>(req);
|
||||
}
|
||||
|
||||
InterceptResponse(req, response);
|
||||
|
||||
@ -507,7 +523,21 @@ namespace Org.OpenAPITools.Client
|
||||
|
||||
InterceptRequest(req);
|
||||
|
||||
var response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false);
|
||||
IRestResponse<T> response;
|
||||
if (RetryConfiguration.AsyncRetryPolicy != null)
|
||||
{
|
||||
var policy = RetryConfiguration.AsyncRetryPolicy;
|
||||
var policyResult = await policy.ExecuteAndCaptureAsync(() => client.ExecuteAsync(req, cancellationToken)).ConfigureAwait(false);
|
||||
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
|
||||
{
|
||||
Request = req,
|
||||
ErrorException = policyResult.FinalException
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
InterceptResponse(req, response);
|
||||
|
||||
@ -753,4 +783,4 @@ namespace Org.OpenAPITools.Client
|
||||
}
|
||||
#endregion ISynchronousClient
|
||||
}
|
||||
}
|
||||
}
|
@ -418,4 +418,4 @@ namespace Org.OpenAPITools.Client
|
||||
}
|
||||
#endregion Static Members
|
||||
}
|
||||
}
|
||||
}
|
@ -105,4 +105,4 @@ namespace Org.OpenAPITools.Client
|
||||
/// <value>X509 Certificate collection.</value>
|
||||
X509CertificateCollection ClientCertificates { get; }
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
using Polly.Retry;
|
||||
using RestSharp;
|
||||
|
||||
namespace Org.OpenAPITools.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration class to set the polly retry policies to be applied to the requests.
|
||||
/// </summary>
|
||||
public class RetryConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// Retry policy
|
||||
/// </summary>
|
||||
public static RetryPolicy<IRestResponse> RetryPolicy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Async retry policy
|
||||
/// </summary>
|
||||
public static AsyncRetryPolicy<IRestResponse> AsyncRetryPolicy { get; set; }
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@
|
||||
<PackageReference Include="JsonSubTypes" Version="1.7.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="RestSharp" Version="106.11.4" />
|
||||
<PackageReference Include="Polly" Version="7.2.0" />
|
||||
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user