christianhelle / refitter

Refit Client API Generator for OpenAPI
https://refitter.github.io
MIT License
156 stars 36 forks source link

Add support for Microsoft HTTP Resilience library #400

Closed christianhelle closed 1 week ago

christianhelle commented 2 weeks ago

Previously, the UsePolly setting was used for transient fault handling in the dependency injection settings. This pull request updates the code to use the new TransientErrorHandler property instead. The UsePolly setting is deprecated and will be removed in future versions. This change improves the clarity and maintainability of the code.

The following code is generated:

namespace Petstore
{
    using System;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Http.Resilience;

    public static partial class IServiceCollectionExtensions
    {
        public static IServiceCollection ConfigureRefitClients(this IServiceCollection services, Action<IHttpClientBuilder>? builder = default)
        {
            var clientBuilderISwaggerPetstoreDirect = services
                .AddRefitClient<ISwaggerPetstoreDirect>()
                .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://petstore3.swagger.io/api/v3"));

            clientBuilderISwaggerPetstoreDirect
                .AddStandardResilienceHandler(config =>
                {
                    config.Retry = new HttpRetryStrategyOptions
                    {
                        UseJitter = true,
                        MaxRetryAttempts = 3,
                        Delay = TimeSpan.FromSeconds(0.5)
                    };
                });

            builder?.Invoke(clientBuilderISwaggerPetstoreDirect);

            return services;
        }
    }
}

using the following .refitter file

{
  "openApiPath": "./OpenAPI/v3.0/petstore.json",
  "dependencyInjectionSettings": {
    "baseUrl": "https://petstore3.swagger.io/api/v3",
    "transientErrorHandler": "HttpResilience",
    "maxRetryCount": 3,
    "firstBackoffRetryInSeconds": 0.5
  }
}
christianhelle commented 2 weeks ago

@dammitjanet Does this work for you? See description...

dammitjanet commented 2 weeks ago

@christianhelle looks pretty good, nice update

sonarcloud[bot] commented 1 week ago

Quality Gate Passed Quality Gate passed

Issues
3 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud