Skip to content

Commit

Permalink
fix c# generation
Browse files Browse the repository at this point in the history
  • Loading branch information
morganleroi committed Jan 15, 2024
1 parent 51b1ff4 commit bcb35ad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ internal class HttpTransport
private readonly AlgoliaConfig _algoliaConfig;
private string _errorMessage;

private class VoidResult
{
}

/// <summary>
/// Instantiate the transport class with the given configuration and requester
/// </summary>
Expand Down Expand Up @@ -52,6 +56,19 @@ public async Task<TResult> ExecuteRequestAsync<TResult>(HttpMethod method, strin
await ExecuteRequestAsync<TResult, object>(method, uri, requestOptions, ct)
.ConfigureAwait(false);

/// <summary>
/// Execute the request, without response
/// </summary>
/// <param name="method">The HttpMethod <see cref="HttpMethod"/></param>
/// <param name="uri">The endpoint URI</param>
/// <param name="requestOptions">Add extra http header or query parameters to Algolia</param>
/// <param name="ct">Optional cancellation token</param>
public async Task ExecuteRequestAsync(HttpMethod method, string uri,
InternalRequestOptions requestOptions = null,
CancellationToken ct = default) =>
await ExecuteRequestAsync<VoidResult>(method, uri, requestOptions, ct)
.ConfigureAwait(false);

/// <summary>
/// Call api with retry strategy
/// </summary>
Expand Down Expand Up @@ -93,7 +110,8 @@ private async Task<TResult> ExecuteRequestAsync<TResult, TData>(HttpMethod metho
foreach (var host in _retryStrategy.GetTryableHost(callType))
{
request.Body = CreateRequestContent(requestOptions?.Data, request.CanCompress);
request.Uri = BuildUri(host.Url, uri, requestOptions?.CustomPathParameters, requestOptions?.PathParameters, requestOptions?.QueryParameters);
request.Uri = BuildUri(host.Url, uri, requestOptions?.CustomPathParameters, requestOptions?.PathParameters,
requestOptions?.QueryParameters);
var requestTimeout =
TimeSpan.FromTicks((requestOptions?.Timeout ?? GetTimeOut(callType)).Ticks * (host.RetryCount + 1));

Expand All @@ -111,6 +129,11 @@ private async Task<TResult> ExecuteRequestAsync<TResult, TData>(HttpMethod metho
switch (_retryStrategy.Decide(host, response))
{
case RetryOutcomeType.Success:
if (typeof(TResult) == typeof(VoidResult))
{
return new VoidResult() as TResult;
}

return await _serializer.Deserialize<TResult>(response.Body);
case RetryOutcomeType.Retry:
continue;
Expand Down Expand Up @@ -158,7 +181,8 @@ private IDictionary<string, string> GenerateHeaders(IDictionary<string, string>
/// <param name="pathParameters"></param>
/// <param name="optionalQueryParameters"></param>
/// <returns></returns>
private Uri BuildUri(string url, string baseUri, IDictionary<string, string> customPathParameters = null, IDictionary<string, string> pathParameters = null,
private Uri BuildUri(string url, string baseUri, IDictionary<string, string> customPathParameters = null,
IDictionary<string, string> pathParameters = null,
IDictionary<string, string> optionalQueryParameters = null)
{
var path = $"{baseUri}";
Expand Down
6 changes: 3 additions & 3 deletions templates/csharp/libraries/httpclient/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Algolia.Search.Clients
{{#isDeprecated}}
[Obsolete]
{{/isDeprecated}}
Task<{{> return_type}}> {{operationId}}Async{{#returnType}}{{#vendorExtensions.x-is-generic}}<T>{{/vendorExtensions.x-is-generic}}{{/returnType}}({{#allParams}}{{{dataType}}}{{#isEnumRef}}{{^required}}?{{/required}}{{/isEnumRef}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default{{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}RequestOptions options = null, CancellationToken cancellationToken = default);
Task{{#returnType}}<{{> return_type}}>{{/returnType}} {{operationId}}Async{{#returnType}}{{#vendorExtensions.x-is-generic}}<T>{{/vendorExtensions.x-is-generic}}{{/returnType}}({{#allParams}}{{{dataType}}}{{#isEnumRef}}{{^required}}?{{/required}}{{/isEnumRef}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default{{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}RequestOptions options = null, CancellationToken cancellationToken = default);
{{/operation}}
}
{{/supportsAsync}}
Expand Down Expand Up @@ -114,7 +114,7 @@ namespace Algolia.Search.Clients
{{#isDeprecated}}
[Obsolete]
{{/isDeprecated}}
public async Task<{{> return_type}}> {{operationId}}Async{{#returnType}}{{#vendorExtensions.x-is-generic}}<T>{{/vendorExtensions.x-is-generic}}{{/returnType}}({{#allParams}}{{{dataType}}}{{#isEnumRef}}{{^required}}?{{/required}}{{/isEnumRef}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default{{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}RequestOptions options = null, CancellationToken cancellationToken = default)
public async Task{{#returnType}}<{{> return_type}}>{{/returnType}} {{operationId}}Async{{#returnType}}{{#vendorExtensions.x-is-generic}}<T>{{/vendorExtensions.x-is-generic}}{{/returnType}}({{#allParams}}{{{dataType}}}{{#isEnumRef}}{{^required}}?{{/required}}{{/isEnumRef}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = default{{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#allParams.0}}, {{/allParams.0}}RequestOptions options = null, CancellationToken cancellationToken = default)
{
{{#allParams}}
{{#required}}
Expand Down Expand Up @@ -184,7 +184,7 @@ namespace Algolia.Search.Clients
requestOptions.UseReadTransporter = true;
{{/x-use-read-transporter}}
{{/vendorExtensions}}
return await _transport.ExecuteRequestAsync<{{> return_type}}>(new HttpMethod("{{httpMethod}}"),"{{{path}}}", requestOptions, cancellationToken).ConfigureAwait(false);
{{#returnType}}return {{/returnType}}await _transport.ExecuteRequestAsync{{#returnType}}<{{> return_type}}>{{/returnType}}(new HttpMethod("{{httpMethod}}"),"{{{path}}}", requestOptions, cancellationToken).ConfigureAwait(false);
}
{{/supportsAsync}}
{{/operation}}
Expand Down

0 comments on commit bcb35ad

Please sign in to comment.