[csharp] Update RestSharp to 110.2.0 (#16122)

* [csharp] Update RestSharp to 110.2.0

* Post './bin/generate-samples.sh bin/configs/csharp*' and './bin/utils/export_docs_generators.sh' scripts

* OAuthAuthenticator: use configureSerialization

* ContentType prop = RestSharp.ContentType.Json

* `req` -> `request` in `Exec()` and `ExecAsync()`

* Regenerate samples
This commit is contained in:
Renny S 2023-07-21 10:19:38 +01:00 committed by GitHub
parent 7252d1a698
commit bd76c1b102
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 227 additions and 297 deletions

View File

@ -37,7 +37,6 @@ namespace {{packageName}}.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{ {
private readonly IReadableConfiguration _configuration; private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{ {
// OpenAPI generated types generally hide default constructors. // OpenAPI generated types generally hide default constructors.
@ -150,17 +149,13 @@ namespace {{packageName}}.Client
public ISerializer Serializer => this; public ISerializer Serializer => this;
public IDeserializer Deserializer => this; public IDeserializer Deserializer => this;
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept; public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
public SupportsContentType SupportsContentType => contentType => public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) || contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase); contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
public string ContentType public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public DataFormat DataFormat => DataFormat.Json; public DataFormat DataFormat => DataFormat.Json;
} }
@ -434,7 +429,7 @@ namespace {{packageName}}.Client
return transformed; return transformed;
} }
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration) private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -459,8 +454,8 @@ namespace {{packageName}}.Client
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
{{#hasOAuthMethods}} {{#hasOAuthMethods}}
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
@ -478,22 +473,21 @@ namespace {{packageName}}.Client
} }
{{/hasOAuthMethods}} {{/hasOAuthMethods}}
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null) if (RetryConfiguration.RetryPolicy != null)
{ {
var policy = RetryConfiguration.RetryPolicy; var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = client.Execute<T>(req); response = client.Execute<T>(request);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -521,7 +515,7 @@ namespace {{packageName}}.Client
response.Data = (T)(object)response.Content; response.Data = (T)(object)response.Content;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)
@ -559,7 +553,7 @@ namespace {{packageName}}.Client
} }
{{#supportsAsync}} {{#supportsAsync}}
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -572,8 +566,8 @@ namespace {{packageName}}.Client
UseDefaultCredentials = configuration.UseDefaultCredentials UseDefaultCredentials = configuration.UseDefaultCredentials
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
{{#hasOAuthMethods}} {{#hasOAuthMethods}}
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
@ -591,24 +585,23 @@ namespace {{packageName}}.Client
} }
{{/hasOAuthMethods}} {{/hasOAuthMethods}}
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
{{#supportsRetry}} {{#supportsRetry}}
if (RetryConfiguration.AsyncRetryPolicy != null) if (RetryConfiguration.AsyncRetryPolicy != null)
{ {
var policy = RetryConfiguration.AsyncRetryPolicy; var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false); var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
{{/supportsRetry}} {{/supportsRetry}}
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false); response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
{{#supportsRetry}} {{#supportsRetry}}
} }
{{/supportsRetry}} {{/supportsRetry}}
@ -627,7 +620,7 @@ namespace {{packageName}}.Client
response.Data = (T)(object)response.RawBytes; response.Data = (T)(object)response.RawBytes;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)

View File

@ -73,8 +73,8 @@ namespace {{packageName}}.Client.Auth
/// <returns>An authentication token.</returns> /// <returns>An authentication token.</returns>
async Task<string> GetToken() async Task<string> GetToken()
{ {
var client = new RestClient(_tokenUrl) var client = new RestClient(_tokenUrl,
.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
var request = new RestRequest() var request = new RestRequest()
.AddParameter("grant_type", _grantType) .AddParameter("grant_type", _grantType)

View File

@ -32,7 +32,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
{{/useGenericHost}} {{/useGenericHost}}
{{#useRestSharp}} {{#useRestSharp}}
<PackageReference Include="RestSharp" Version="108.0.3" /> <PackageReference Include="RestSharp" Version="110.2.0" />
{{/useRestSharp}} {{/useRestSharp}}
{{#useGenericHost}} {{#useGenericHost}}
<PackageReference Include="Microsoft.Extensions.Http" Version="{{#lambda.first}}{{#netStandard}}5.0.0 {{/netStandard}}{{#net47}}7.0.0 {{/net47}}{{#net48}}7.0.0 {{/net48}}{{#net6.0}}6.0.0 {{/net6.0}}{{#net70OrLater}}7.0.0{{/net70OrLater}}{{/lambda.first}}" /> <PackageReference Include="Microsoft.Extensions.Http" Version="{{#lambda.first}}{{#netStandard}}5.0.0 {{/netStandard}}{{#net47}}7.0.0 {{/net47}}{{#net48}}7.0.0 {{/net48}}{{#net6.0}}6.0.0 {{/net6.0}}{{#net70OrLater}}7.0.0{{/net70OrLater}}{{/lambda.first}}" />

View File

@ -32,7 +32,7 @@
<dependency id="Newtonsoft.Json" version="13.0.3" /> <dependency id="Newtonsoft.Json" version="13.0.3" />
{{#useRestSharp}} {{#useRestSharp}}
<dependency id="RestSharp" version="108.0.3" /> <dependency id="RestSharp" version="110.2.0" />
{{/useRestSharp}} {{/useRestSharp}}
{{#useCompareNetObjects}} {{#useCompareNetObjects}}
<dependency id="CompareNETObjects" version="4.61.0" /> <dependency id="CompareNETObjects" version="4.61.0" />

View File

@ -39,7 +39,6 @@ namespace Org.OpenAPITools.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{ {
private readonly IReadableConfiguration _configuration; private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{ {
// OpenAPI generated types generally hide default constructors. // OpenAPI generated types generally hide default constructors.
@ -152,17 +151,13 @@ namespace Org.OpenAPITools.Client
public ISerializer Serializer => this; public ISerializer Serializer => this;
public IDeserializer Deserializer => this; public IDeserializer Deserializer => this;
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept; public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
public SupportsContentType SupportsContentType => contentType => public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) || contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase); contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
public string ContentType public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public DataFormat DataFormat => DataFormat.Json; public DataFormat DataFormat => DataFormat.Json;
} }
@ -435,7 +430,7 @@ namespace Org.OpenAPITools.Client
return transformed; return transformed;
} }
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration) private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -460,25 +455,24 @@ namespace Org.OpenAPITools.Client
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null) if (RetryConfiguration.RetryPolicy != null)
{ {
var policy = RetryConfiguration.RetryPolicy; var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = client.Execute<T>(req); response = client.Execute<T>(request);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -506,7 +500,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.Content; response.Data = (T)(object)response.Content;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)
@ -543,7 +537,7 @@ namespace Org.OpenAPITools.Client
return result; return result;
} }
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -556,25 +550,24 @@ namespace Org.OpenAPITools.Client
UseDefaultCredentials = configuration.UseDefaultCredentials UseDefaultCredentials = configuration.UseDefaultCredentials
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.AsyncRetryPolicy != null) if (RetryConfiguration.AsyncRetryPolicy != null)
{ {
var policy = RetryConfiguration.AsyncRetryPolicy; var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false); var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false); response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -591,7 +584,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.RawBytes; response.Data = (T)(object)response.RawBytes;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)

View File

@ -23,7 +23,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="JsonSubTypes" Version="2.0.1" /> <PackageReference Include="JsonSubTypes" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="108.0.3" /> <PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Polly" Version="7.2.3" /> <PackageReference Include="Polly" Version="7.2.3" />
</ItemGroup> </ItemGroup>

View File

@ -37,7 +37,6 @@ namespace Org.OpenAPITools.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{ {
private readonly IReadableConfiguration _configuration; private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{ {
// OpenAPI generated types generally hide default constructors. // OpenAPI generated types generally hide default constructors.
@ -150,17 +149,13 @@ namespace Org.OpenAPITools.Client
public ISerializer Serializer => this; public ISerializer Serializer => this;
public IDeserializer Deserializer => this; public IDeserializer Deserializer => this;
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept; public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
public SupportsContentType SupportsContentType => contentType => public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) || contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase); contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
public string ContentType public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public DataFormat DataFormat => DataFormat.Json; public DataFormat DataFormat => DataFormat.Json;
} }
@ -433,7 +428,7 @@ namespace Org.OpenAPITools.Client
return transformed; return transformed;
} }
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration) private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -458,25 +453,24 @@ namespace Org.OpenAPITools.Client
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null) if (RetryConfiguration.RetryPolicy != null)
{ {
var policy = RetryConfiguration.RetryPolicy; var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = client.Execute<T>(req); response = client.Execute<T>(request);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -504,7 +498,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.Content; response.Data = (T)(object)response.Content;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)
@ -541,7 +535,7 @@ namespace Org.OpenAPITools.Client
return result; return result;
} }
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -554,25 +548,24 @@ namespace Org.OpenAPITools.Client
UseDefaultCredentials = configuration.UseDefaultCredentials UseDefaultCredentials = configuration.UseDefaultCredentials
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.AsyncRetryPolicy != null) if (RetryConfiguration.AsyncRetryPolicy != null)
{ {
var policy = RetryConfiguration.AsyncRetryPolicy; var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false); var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false); response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -589,7 +582,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.RawBytes; response.Data = (T)(object)response.RawBytes;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)

View File

@ -23,7 +23,7 @@
<PackageReference Include="CompareNETObjects" Version="4.61.0" /> <PackageReference Include="CompareNETObjects" Version="4.61.0" />
<PackageReference Include="JsonSubTypes" Version="2.0.1" /> <PackageReference Include="JsonSubTypes" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="108.0.3" /> <PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Polly" Version="7.2.3" /> <PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" /> <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup> </ItemGroup>

View File

@ -38,7 +38,6 @@ namespace Org.OpenAPITools.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{ {
private readonly IReadableConfiguration _configuration; private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{ {
// OpenAPI generated types generally hide default constructors. // OpenAPI generated types generally hide default constructors.
@ -151,17 +150,13 @@ namespace Org.OpenAPITools.Client
public ISerializer Serializer => this; public ISerializer Serializer => this;
public IDeserializer Deserializer => this; public IDeserializer Deserializer => this;
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept; public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
public SupportsContentType SupportsContentType => contentType => public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) || contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase); contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
public string ContentType public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public DataFormat DataFormat => DataFormat.Json; public DataFormat DataFormat => DataFormat.Json;
} }
@ -434,7 +429,7 @@ namespace Org.OpenAPITools.Client
return transformed; return transformed;
} }
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration) private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -459,8 +454,8 @@ namespace Org.OpenAPITools.Client
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -476,22 +471,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null) if (RetryConfiguration.RetryPolicy != null)
{ {
var policy = RetryConfiguration.RetryPolicy; var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = client.Execute<T>(req); response = client.Execute<T>(request);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -519,7 +513,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.Content; response.Data = (T)(object)response.Content;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)
@ -556,7 +550,7 @@ namespace Org.OpenAPITools.Client
return result; return result;
} }
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -569,8 +563,8 @@ namespace Org.OpenAPITools.Client
UseDefaultCredentials = configuration.UseDefaultCredentials UseDefaultCredentials = configuration.UseDefaultCredentials
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -586,22 +580,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.AsyncRetryPolicy != null) if (RetryConfiguration.AsyncRetryPolicy != null)
{ {
var policy = RetryConfiguration.AsyncRetryPolicy; var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false); var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false); response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -618,7 +611,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.RawBytes; response.Data = (T)(object)response.RawBytes;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)

View File

@ -81,8 +81,8 @@ namespace Org.OpenAPITools.Client.Auth
/// <returns>An authentication token.</returns> /// <returns>An authentication token.</returns>
async Task<string> GetToken() async Task<string> GetToken()
{ {
var client = new RestClient(_tokenUrl) var client = new RestClient(_tokenUrl,
.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
var request = new RestRequest() var request = new RestRequest()
.AddParameter("grant_type", _grantType) .AddParameter("grant_type", _grantType)

View File

@ -23,7 +23,7 @@
<PackageReference Include="CompareNETObjects" Version="4.61.0" /> <PackageReference Include="CompareNETObjects" Version="4.61.0" />
<PackageReference Include="JsonSubTypes" Version="2.0.1" /> <PackageReference Include="JsonSubTypes" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="108.0.3" /> <PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Polly" Version="7.2.3" /> <PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" /> <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup> </ItemGroup>

View File

@ -39,7 +39,6 @@ namespace Org.OpenAPITools.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{ {
private readonly IReadableConfiguration _configuration; private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{ {
// OpenAPI generated types generally hide default constructors. // OpenAPI generated types generally hide default constructors.
@ -152,17 +151,13 @@ namespace Org.OpenAPITools.Client
public ISerializer Serializer => this; public ISerializer Serializer => this;
public IDeserializer Deserializer => this; public IDeserializer Deserializer => this;
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept; public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
public SupportsContentType SupportsContentType => contentType => public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) || contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase); contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
public string ContentType public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public DataFormat DataFormat => DataFormat.Json; public DataFormat DataFormat => DataFormat.Json;
} }
@ -435,7 +430,7 @@ namespace Org.OpenAPITools.Client
return transformed; return transformed;
} }
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration) private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -460,8 +455,8 @@ namespace Org.OpenAPITools.Client
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -477,22 +472,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null) if (RetryConfiguration.RetryPolicy != null)
{ {
var policy = RetryConfiguration.RetryPolicy; var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = client.Execute<T>(req); response = client.Execute<T>(request);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -520,7 +514,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.Content; response.Data = (T)(object)response.Content;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)
@ -557,7 +551,7 @@ namespace Org.OpenAPITools.Client
return result; return result;
} }
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -570,8 +564,8 @@ namespace Org.OpenAPITools.Client
UseDefaultCredentials = configuration.UseDefaultCredentials UseDefaultCredentials = configuration.UseDefaultCredentials
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -587,22 +581,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.AsyncRetryPolicy != null) if (RetryConfiguration.AsyncRetryPolicy != null)
{ {
var policy = RetryConfiguration.AsyncRetryPolicy; var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false); var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false); response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -619,7 +612,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.RawBytes; response.Data = (T)(object)response.RawBytes;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)

View File

@ -81,8 +81,8 @@ namespace Org.OpenAPITools.Client.Auth
/// <returns>An authentication token.</returns> /// <returns>An authentication token.</returns>
async Task<string> GetToken() async Task<string> GetToken()
{ {
var client = new RestClient(_tokenUrl) var client = new RestClient(_tokenUrl,
.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
var request = new RestRequest() var request = new RestRequest()
.AddParameter("grant_type", _grantType) .AddParameter("grant_type", _grantType)

View File

@ -23,7 +23,7 @@
<PackageReference Include="CompareNETObjects" Version="4.61.0" /> <PackageReference Include="CompareNETObjects" Version="4.61.0" />
<PackageReference Include="JsonSubTypes" Version="2.0.1" /> <PackageReference Include="JsonSubTypes" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="108.0.3" /> <PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Polly" Version="7.2.3" /> <PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" /> <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup> </ItemGroup>

View File

@ -39,7 +39,6 @@ namespace Org.OpenAPITools.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{ {
private readonly IReadableConfiguration _configuration; private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{ {
// OpenAPI generated types generally hide default constructors. // OpenAPI generated types generally hide default constructors.
@ -152,17 +151,13 @@ namespace Org.OpenAPITools.Client
public ISerializer Serializer => this; public ISerializer Serializer => this;
public IDeserializer Deserializer => this; public IDeserializer Deserializer => this;
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept; public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
public SupportsContentType SupportsContentType => contentType => public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) || contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase); contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
public string ContentType public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public DataFormat DataFormat => DataFormat.Json; public DataFormat DataFormat => DataFormat.Json;
} }
@ -435,7 +430,7 @@ namespace Org.OpenAPITools.Client
return transformed; return transformed;
} }
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration) private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -460,8 +455,8 @@ namespace Org.OpenAPITools.Client
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -477,22 +472,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null) if (RetryConfiguration.RetryPolicy != null)
{ {
var policy = RetryConfiguration.RetryPolicy; var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = client.Execute<T>(req); response = client.Execute<T>(request);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -520,7 +514,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.Content; response.Data = (T)(object)response.Content;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)
@ -557,7 +551,7 @@ namespace Org.OpenAPITools.Client
return result; return result;
} }
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -570,8 +564,8 @@ namespace Org.OpenAPITools.Client
UseDefaultCredentials = configuration.UseDefaultCredentials UseDefaultCredentials = configuration.UseDefaultCredentials
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -587,22 +581,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.AsyncRetryPolicy != null) if (RetryConfiguration.AsyncRetryPolicy != null)
{ {
var policy = RetryConfiguration.AsyncRetryPolicy; var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false); var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false); response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -619,7 +612,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.RawBytes; response.Data = (T)(object)response.RawBytes;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)

View File

@ -81,8 +81,8 @@ namespace Org.OpenAPITools.Client.Auth
/// <returns>An authentication token.</returns> /// <returns>An authentication token.</returns>
async Task<string> GetToken() async Task<string> GetToken()
{ {
var client = new RestClient(_tokenUrl) var client = new RestClient(_tokenUrl,
.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
var request = new RestRequest() var request = new RestRequest()
.AddParameter("grant_type", _grantType) .AddParameter("grant_type", _grantType)

View File

@ -23,7 +23,7 @@
<PackageReference Include="CompareNETObjects" Version="4.61.0" /> <PackageReference Include="CompareNETObjects" Version="4.61.0" />
<PackageReference Include="JsonSubTypes" Version="2.0.1" /> <PackageReference Include="JsonSubTypes" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="108.0.3" /> <PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Polly" Version="7.2.3" /> <PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" /> <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup> </ItemGroup>

View File

@ -39,7 +39,6 @@ namespace Org.OpenAPITools.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{ {
private readonly IReadableConfiguration _configuration; private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{ {
// OpenAPI generated types generally hide default constructors. // OpenAPI generated types generally hide default constructors.
@ -152,17 +151,13 @@ namespace Org.OpenAPITools.Client
public ISerializer Serializer => this; public ISerializer Serializer => this;
public IDeserializer Deserializer => this; public IDeserializer Deserializer => this;
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept; public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
public SupportsContentType SupportsContentType => contentType => public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) || contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase); contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
public string ContentType public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public DataFormat DataFormat => DataFormat.Json; public DataFormat DataFormat => DataFormat.Json;
} }
@ -435,7 +430,7 @@ namespace Org.OpenAPITools.Client
return transformed; return transformed;
} }
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration) private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -460,8 +455,8 @@ namespace Org.OpenAPITools.Client
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -477,22 +472,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null) if (RetryConfiguration.RetryPolicy != null)
{ {
var policy = RetryConfiguration.RetryPolicy; var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = client.Execute<T>(req); response = client.Execute<T>(request);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -520,7 +514,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.Content; response.Data = (T)(object)response.Content;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)
@ -557,7 +551,7 @@ namespace Org.OpenAPITools.Client
return result; return result;
} }
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -570,8 +564,8 @@ namespace Org.OpenAPITools.Client
UseDefaultCredentials = configuration.UseDefaultCredentials UseDefaultCredentials = configuration.UseDefaultCredentials
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -587,22 +581,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.AsyncRetryPolicy != null) if (RetryConfiguration.AsyncRetryPolicy != null)
{ {
var policy = RetryConfiguration.AsyncRetryPolicy; var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false); var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false); response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -619,7 +612,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.RawBytes; response.Data = (T)(object)response.RawBytes;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)

View File

@ -81,8 +81,8 @@ namespace Org.OpenAPITools.Client.Auth
/// <returns>An authentication token.</returns> /// <returns>An authentication token.</returns>
async Task<string> GetToken() async Task<string> GetToken()
{ {
var client = new RestClient(_tokenUrl) var client = new RestClient(_tokenUrl,
.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
var request = new RestRequest() var request = new RestRequest()
.AddParameter("grant_type", _grantType) .AddParameter("grant_type", _grantType)

View File

@ -24,7 +24,7 @@
<PackageReference Include="CompareNETObjects" Version="4.61.0" /> <PackageReference Include="CompareNETObjects" Version="4.61.0" />
<PackageReference Include="JsonSubTypes" Version="2.0.1" /> <PackageReference Include="JsonSubTypes" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="108.0.3" /> <PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Polly" Version="7.2.3" /> <PackageReference Include="Polly" Version="7.2.3" />
</ItemGroup> </ItemGroup>

View File

@ -38,7 +38,6 @@ namespace Org.OpenAPITools.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{ {
private readonly IReadableConfiguration _configuration; private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{ {
// OpenAPI generated types generally hide default constructors. // OpenAPI generated types generally hide default constructors.
@ -151,17 +150,13 @@ namespace Org.OpenAPITools.Client
public ISerializer Serializer => this; public ISerializer Serializer => this;
public IDeserializer Deserializer => this; public IDeserializer Deserializer => this;
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept; public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
public SupportsContentType SupportsContentType => contentType => public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) || contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase); contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
public string ContentType public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public DataFormat DataFormat => DataFormat.Json; public DataFormat DataFormat => DataFormat.Json;
} }
@ -434,7 +429,7 @@ namespace Org.OpenAPITools.Client
return transformed; return transformed;
} }
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration) private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -459,8 +454,8 @@ namespace Org.OpenAPITools.Client
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -476,22 +471,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null) if (RetryConfiguration.RetryPolicy != null)
{ {
var policy = RetryConfiguration.RetryPolicy; var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = client.Execute<T>(req); response = client.Execute<T>(request);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -519,7 +513,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.Content; response.Data = (T)(object)response.Content;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)
@ -556,7 +550,7 @@ namespace Org.OpenAPITools.Client
return result; return result;
} }
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -569,8 +563,8 @@ namespace Org.OpenAPITools.Client
UseDefaultCredentials = configuration.UseDefaultCredentials UseDefaultCredentials = configuration.UseDefaultCredentials
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -586,22 +580,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.AsyncRetryPolicy != null) if (RetryConfiguration.AsyncRetryPolicy != null)
{ {
var policy = RetryConfiguration.AsyncRetryPolicy; var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false); var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false); response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -618,7 +611,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.RawBytes; response.Data = (T)(object)response.RawBytes;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)

View File

@ -81,8 +81,8 @@ namespace Org.OpenAPITools.Client.Auth
/// <returns>An authentication token.</returns> /// <returns>An authentication token.</returns>
async Task<string> GetToken() async Task<string> GetToken()
{ {
var client = new RestClient(_tokenUrl) var client = new RestClient(_tokenUrl,
.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
var request = new RestRequest() var request = new RestRequest()
.AddParameter("grant_type", _grantType) .AddParameter("grant_type", _grantType)

View File

@ -23,7 +23,7 @@
<PackageReference Include="CompareNETObjects" Version="4.61.0" /> <PackageReference Include="CompareNETObjects" Version="4.61.0" />
<PackageReference Include="JsonSubTypes" Version="2.0.1" /> <PackageReference Include="JsonSubTypes" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="108.0.3" /> <PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Polly" Version="7.2.3" /> <PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" /> <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup> </ItemGroup>

View File

@ -39,7 +39,6 @@ namespace Org.OpenAPITools.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{ {
private readonly IReadableConfiguration _configuration; private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{ {
// OpenAPI generated types generally hide default constructors. // OpenAPI generated types generally hide default constructors.
@ -152,17 +151,13 @@ namespace Org.OpenAPITools.Client
public ISerializer Serializer => this; public ISerializer Serializer => this;
public IDeserializer Deserializer => this; public IDeserializer Deserializer => this;
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept; public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
public SupportsContentType SupportsContentType => contentType => public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) || contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase); contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
public string ContentType public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public DataFormat DataFormat => DataFormat.Json; public DataFormat DataFormat => DataFormat.Json;
} }
@ -435,7 +430,7 @@ namespace Org.OpenAPITools.Client
return transformed; return transformed;
} }
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration) private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -460,8 +455,8 @@ namespace Org.OpenAPITools.Client
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -477,22 +472,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null) if (RetryConfiguration.RetryPolicy != null)
{ {
var policy = RetryConfiguration.RetryPolicy; var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = client.Execute<T>(req); response = client.Execute<T>(request);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -520,7 +514,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.Content; response.Data = (T)(object)response.Content;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)
@ -557,7 +551,7 @@ namespace Org.OpenAPITools.Client
return result; return result;
} }
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -570,8 +564,8 @@ namespace Org.OpenAPITools.Client
UseDefaultCredentials = configuration.UseDefaultCredentials UseDefaultCredentials = configuration.UseDefaultCredentials
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -587,22 +581,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.AsyncRetryPolicy != null) if (RetryConfiguration.AsyncRetryPolicy != null)
{ {
var policy = RetryConfiguration.AsyncRetryPolicy; var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false); var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false); response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -619,7 +612,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.RawBytes; response.Data = (T)(object)response.RawBytes;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)

View File

@ -81,8 +81,8 @@ namespace Org.OpenAPITools.Client.Auth
/// <returns>An authentication token.</returns> /// <returns>An authentication token.</returns>
async Task<string> GetToken() async Task<string> GetToken()
{ {
var client = new RestClient(_tokenUrl) var client = new RestClient(_tokenUrl,
.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
var request = new RestRequest() var request = new RestRequest()
.AddParameter("grant_type", _grantType) .AddParameter("grant_type", _grantType)

View File

@ -24,7 +24,7 @@
<PackageReference Include="CompareNETObjects" Version="4.61.0" /> <PackageReference Include="CompareNETObjects" Version="4.61.0" />
<PackageReference Include="JsonSubTypes" Version="2.0.1" /> <PackageReference Include="JsonSubTypes" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="108.0.3" /> <PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Polly" Version="7.2.3" /> <PackageReference Include="Polly" Version="7.2.3" />
</ItemGroup> </ItemGroup>

View File

@ -38,7 +38,6 @@ namespace Org.OpenAPITools.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{ {
private readonly IReadableConfiguration _configuration; private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{ {
// OpenAPI generated types generally hide default constructors. // OpenAPI generated types generally hide default constructors.
@ -151,17 +150,13 @@ namespace Org.OpenAPITools.Client
public ISerializer Serializer => this; public ISerializer Serializer => this;
public IDeserializer Deserializer => this; public IDeserializer Deserializer => this;
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept; public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
public SupportsContentType SupportsContentType => contentType => public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) || contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase); contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
public string ContentType public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public DataFormat DataFormat => DataFormat.Json; public DataFormat DataFormat => DataFormat.Json;
} }
@ -434,7 +429,7 @@ namespace Org.OpenAPITools.Client
return transformed; return transformed;
} }
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration) private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -459,8 +454,8 @@ namespace Org.OpenAPITools.Client
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -476,22 +471,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null) if (RetryConfiguration.RetryPolicy != null)
{ {
var policy = RetryConfiguration.RetryPolicy; var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = client.Execute<T>(req); response = client.Execute<T>(request);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -519,7 +513,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.Content; response.Data = (T)(object)response.Content;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)
@ -556,7 +550,7 @@ namespace Org.OpenAPITools.Client
return result; return result;
} }
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{ {
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
@ -569,8 +563,8 @@ namespace Org.OpenAPITools.Client
UseDefaultCredentials = configuration.UseDefaultCredentials UseDefaultCredentials = configuration.UseDefaultCredentials
}; };
RestClient client = new RestClient(clientOptions) RestClient client = new RestClient(clientOptions,
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
!string.IsNullOrEmpty(configuration.OAuthClientId) && !string.IsNullOrEmpty(configuration.OAuthClientId) &&
@ -586,22 +580,21 @@ namespace Org.OpenAPITools.Client
configuration)); configuration));
} }
InterceptRequest(req); InterceptRequest(request);
RestResponse<T> response; RestResponse<T> response;
if (RetryConfiguration.AsyncRetryPolicy != null) if (RetryConfiguration.AsyncRetryPolicy != null)
{ {
var policy = RetryConfiguration.AsyncRetryPolicy; var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false); var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T> response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{ {
Request = req,
ErrorException = policyResult.FinalException ErrorException = policyResult.FinalException
}; };
} }
else else
{ {
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false); response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
} }
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@ -618,7 +611,7 @@ namespace Org.OpenAPITools.Client
response.Data = (T)(object)response.RawBytes; response.Data = (T)(object)response.RawBytes;
} }
InterceptResponse(req, response); InterceptResponse(request, response);
var result = ToApiResponse(response); var result = ToApiResponse(response);
if (response.ErrorMessage != null) if (response.ErrorMessage != null)

View File

@ -81,8 +81,8 @@ namespace Org.OpenAPITools.Client.Auth
/// <returns>An authentication token.</returns> /// <returns>An authentication token.</returns>
async Task<string> GetToken() async Task<string> GetToken()
{ {
var client = new RestClient(_tokenUrl) var client = new RestClient(_tokenUrl,
.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)); configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
var request = new RestRequest() var request = new RestRequest()
.AddParameter("grant_type", _grantType) .AddParameter("grant_type", _grantType)

View File

@ -23,7 +23,7 @@
<PackageReference Include="CompareNETObjects" Version="4.61.0" /> <PackageReference Include="CompareNETObjects" Version="4.61.0" />
<PackageReference Include="JsonSubTypes" Version="2.0.1" /> <PackageReference Include="JsonSubTypes" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="108.0.3" /> <PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Polly" Version="7.2.3" /> <PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" /> <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup> </ItemGroup>