Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ namespace {{packageName}}.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{
private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{
// OpenAPI generated types generally hide default constructors.
Expand Down Expand Up @@ -150,17 +149,13 @@ namespace {{packageName}}.Client
public ISerializer Serializer => this;
public IDeserializer Deserializer => this;

public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept;
public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;

public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);

public string ContentType
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;

public DataFormat DataFormat => DataFormat.Json;
}
Expand Down Expand Up @@ -434,7 +429,7 @@ namespace {{packageName}}.Client
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;

Expand All @@ -459,8 +454,8 @@ namespace {{packageName}}.Client
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
};

RestClient client = new RestClient(clientOptions)
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration));
RestClient client = new RestClient(clientOptions,
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));

{{#hasOAuthMethods}}
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
Expand All @@ -478,22 +473,21 @@ namespace {{packageName}}.Client
}

{{/hasOAuthMethods}}
InterceptRequest(req);
InterceptRequest(request);

RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null)
{
var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{
Request = req,
ErrorException = policyResult.FinalException
};
}
else
{
response = client.Execute<T>(req);
response = client.Execute<T>(request);
}

// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
Expand Down Expand Up @@ -521,7 +515,7 @@ namespace {{packageName}}.Client
response.Data = (T)(object)response.Content;
}

InterceptResponse(req, response);
InterceptResponse(request, response);

var result = ToApiResponse(response);
if (response.ErrorMessage != null)
Expand Down Expand Up @@ -559,7 +553,7 @@ namespace {{packageName}}.Client
}

{{#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;

Expand All @@ -572,8 +566,8 @@ namespace {{packageName}}.Client
UseDefaultCredentials = configuration.UseDefaultCredentials
};

RestClient client = new RestClient(clientOptions)
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration));
RestClient client = new RestClient(clientOptions,
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));

{{#hasOAuthMethods}}
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
Expand All @@ -591,24 +585,23 @@ namespace {{packageName}}.Client
}

{{/hasOAuthMethods}}
InterceptRequest(req);
InterceptRequest(request);

RestResponse<T> response;
{{#supportsRetry}}
if (RetryConfiguration.AsyncRetryPolicy != null)
{
var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
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>(request)
{
Request = req,
ErrorException = policyResult.FinalException
};
}
else
{
{{/supportsRetry}}
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false);
response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
{{#supportsRetry}}
}
{{/supportsRetry}}
Expand All @@ -627,7 +620,7 @@ namespace {{packageName}}.Client
response.Data = (T)(object)response.RawBytes;
}

InterceptResponse(req, response);
InterceptResponse(request, response);

var result = ToApiResponse(response);
if (response.ErrorMessage != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ namespace {{packageName}}.Client.Auth
/// <returns>An authentication token.</returns>
async Task<string> GetToken()
{
var client = new RestClient(_tokenUrl)
.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration));
var client = new RestClient(_tokenUrl,
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));

var request = new RestRequest()
.AddParameter("grant_type", _grantType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
{{/useGenericHost}}
{{#useRestSharp}}
<PackageReference Include="RestSharp" Version="108.0.3" />
<PackageReference Include="RestSharp" Version="110.2.0" />
{{/useRestSharp}}
{{#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}}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<dependency id="Newtonsoft.Json" version="13.0.3" />
{{#useRestSharp}}
<dependency id="RestSharp" version="108.0.3" />
<dependency id="RestSharp" version="110.2.0" />
{{/useRestSharp}}
{{#useCompareNetObjects}}
<dependency id="CompareNETObjects" version="4.61.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ namespace Org.OpenAPITools.Client
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
{
private readonly IReadableConfiguration _configuration;
private static readonly string _contentType = "application/json";
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
{
// OpenAPI generated types generally hide default constructors.
Expand Down Expand Up @@ -152,17 +151,13 @@ internal object Deserialize(RestResponse response, Type type)
public ISerializer Serializer => this;
public IDeserializer Deserializer => this;

public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept;
public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;

public SupportsContentType SupportsContentType => contentType =>
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);

public string ContentType
{
get { return _contentType; }
set { throw new InvalidOperationException("Not allowed to set content type."); }
}
public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;

public DataFormat DataFormat => DataFormat.Json;
}
Expand Down Expand Up @@ -435,7 +430,7 @@ private ApiResponse<T> ToApiResponse<T>(RestResponse<T> response)
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;

Expand All @@ -460,25 +455,24 @@ private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadabl
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
};

RestClient client = new RestClient(clientOptions)
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration));
RestClient client = new RestClient(clientOptions,
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));

InterceptRequest(req);
InterceptRequest(request);

RestResponse<T> response;
if (RetryConfiguration.RetryPolicy != null)
{
var policy = RetryConfiguration.RetryPolicy;
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
{
Request = req,
ErrorException = policyResult.FinalException
};
}
else
{
response = client.Execute<T>(req);
response = client.Execute<T>(request);
}

// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
Expand Down Expand Up @@ -506,7 +500,7 @@ private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadabl
response.Data = (T)(object)response.Content;
}

InterceptResponse(req, response);
InterceptResponse(request, response);

var result = ToApiResponse(response);
if (response.ErrorMessage != null)
Expand Down Expand Up @@ -543,7 +537,7 @@ private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadabl
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;

Expand All @@ -556,25 +550,24 @@ private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadabl
UseDefaultCredentials = configuration.UseDefaultCredentials
};

RestClient client = new RestClient(clientOptions)
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration));
RestClient client = new RestClient(clientOptions,
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));

InterceptRequest(req);
InterceptRequest(request);

RestResponse<T> response;
if (RetryConfiguration.AsyncRetryPolicy != null)
{
var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
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>(request)
{
Request = req,
ErrorException = policyResult.FinalException
};
}
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
Expand All @@ -591,7 +584,7 @@ private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadabl
response.Data = (T)(object)response.RawBytes;
}

InterceptResponse(req, response);
InterceptResponse(request, response);

var result = ToApiResponse(response);
if (response.ErrorMessage != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<ItemGroup>
<PackageReference Include="JsonSubTypes" Version="2.0.1" />
<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" />
</ItemGroup>

Expand Down
Loading