Home

Awesome

Apizr

Refit based web api client, but resilient (retry, connectivity, cache, auth, log, priority...)

Read - Documentation

What

The Apizr project was motivated by this 2015 famous blog post about resilient networking.

Its main focus was to address at least everything explained into this article, meanning:

But also, some more core features like:

And more integration/extension independent optional features like:

The list is not exhaustive, there�s more, but what we wanted was playing with all of it with as less code as we could, not worrying about plumbing things and being sure everything is wired and handled by design or almost.

Inspired by Refit.Insane.PowerPack, we wanted to make it simple to use, mixing attribute decorations and fluent configuration.

Also, we built this lib to make it work with any .Net Standard 2.0 compliant platform, so we could use it seamlessly from any kind of app, with or without DI goodness.

How

An api definition with some attributes:

// (Polly) Define a resilience pipeline key
[assembly:ResiliencePipeline("TransientHttpError")]
namespace Apizr.Sample
{
    // (Apizr) Define your web api base url and ask for cache and logs
    [WebApi("https://reqres.in/"), Cache, Log]
    public interface IReqResService
    {
        // (Refit) Define your web api interface methods
        [Get("/api/users")]
        Task<UserList> GetUsersAsync();

        [Get("/api/users/{userId}")]
        Task<UserDetails> GetUserAsync([CacheKey] int userId);

        [Post("/api/users")]
        Task<User> CreateUser(User user);
    }
}

Some resilience strategies:

// (Polly) Create a resilience pipeline with some strategies
var resiliencePipelineBuilder = new ResiliencePipelineBuilder<HttpResponseMessage>()
    // Configure telemetry to get some logs from Polly process
    .ConfigureTelemetry(LoggerFactory.Create(loggingBuilder =>
        loggingBuilder.Debug()))
    // Add a retry strategy with some options
    .AddRetry(
        new RetryStrategyOptions<HttpResponseMessage>
        {
            ShouldHandle = new PredicateBuilder<HttpResponseMessage>()
                .Handle<HttpRequestException>()
                .HandleResult(response =>
                    response.StatusCode is >= HttpStatusCode.InternalServerError
                        or HttpStatusCode.RequestTimeout),
            Delay = TimeSpan.FromSeconds(1),
            MaxRetryAttempts = 3,
            UseJitter = true,
            BackoffType = DelayBackoffType.Exponential
        }));

An instance of this managed api:

Static

Relies on static builder instantiation approach.

// (Polly) Add the resilience pipeline with its key to a registry
var resiliencePipelineRegistry = new ResiliencePipelineRegistry<string>();
resiliencePipelineRegistry.TryAddBuilder<HttpResponseMessage>("TransientHttpError", 
    (builder, _) => builder.AddPipeline(resiliencePipelineBuilder.Build()));

// (Apizr) Get your manager instance
var reqResManager = ApizrBuilder.Current.CreateManagerFor<IReqResService>(
    options => options
        // With a logger
        .WithLoggerFactory(LoggerFactory.Create(loggingBuilder =>
            loggingBuilder.Debug()))
        // With the defined resilience pipeline registry
        .WithResiliencePipelineRegistry(resiliencePipelineRegistry)
        // And with a cache handler
        .WithAkavacheCacheHandler());

Extended

Relies on IServiceCollection extension methods approach.


// (Logger) Configure logging the way you want, like
services.AddLogging(loggingBuilder => loggingBuilder.AddDebug());

// (Apizr) Add an Apizr manager for the defined api to your container
services.AddApizrManagerFor<IReqResService>(options => 
    // With a cache handler
    options.WithAkavacheCacheHandler());

// (Polly) Add the resilience pipeline with its key to your container
services.AddResiliencePipeline<string, HttpResponseMessage>("TransientHttpError",
    builder => builder.AddPipeline(resiliencePipelineBuilder.Build()));
...

// (Apizr) Get your manager instance the way you want, like
var reqResManager = serviceProvider.GetRequiredService<IApizrManager<IReqResService>>();

And then you're good to go:

var userList = await reqResManager.ExecuteAsync(api => api.GetUsersAsync());

This request will be managed with the defined resilience strategies, data cached and all logged.

Apizr has a lot more to offer, just read the doc!

Where

Change Log

Managing (Core)

ProjectCurrentUpcoming
ApizrNuGetNuGet Pre Release
Apizr.Extensions.Microsoft.DependencyInjectionNuGetNuGet Pre Release

Caching

ProjectCurrentUpcoming
Apizr.Extensions.Microsoft.CachingNuGetNuGet Pre Release
Apizr.Integrations.AkavacheNuGetNuGet Pre Release
Apizr.Integrations.MonkeyCacheNuGetNuGet Pre Release

Handling

ProjectCurrentUpcoming
Apizr.Integrations.FusilladeNuGetNuGet Pre Release
Apizr.Integrations.MediatRNuGetNuGet Pre Release
Apizr.Integrations.OptionalNuGetNuGet Pre Release

Mapping

ProjectCurrentUpcoming
Apizr.Integrations.AutoMapperNuGetNuGet Pre Release
Apizr.Integrations.MapsterNuGetNuGet Pre Release

Transferring

ProjectCurrentUpcoming
Apizr.Integrations.FileTransferNuGetNuGet Pre Release
Apizr.Extensions.Microsoft.FileTransferNuGetNuGet Pre Release
Apizr.Integrations.FileTransfer.MediatRNuGetNuGet Pre Release
Apizr.Integrations.FileTransfer.OptionalNuGetNuGet Pre Release

Generating

ProjectCurrentUpcoming
Apizr.Tools.NSwagNuGetNuGet Pre Release

Install the NuGet reference package of your choice:

Install the NuGet .NET CLI Tool package if needed:

Apizr core package make use of well known nuget packages to make the magic appear:

PackageFeatures
RefitAuto-implement web api interface and deal with HttpClient
PollyApply some policies like Retry, CircuitBreaker, etc...
Microsoft.Extensions.Logging.AbstractionsDelegate logging layer to MS Extensions Logging

It also comes with some handling interfaces to let you provide your own services for: