Refit based web api client management, but resilient (retry, connectivity, cache, auth, log, priority...)
$ dotnet add package ApizrRefit based web api client, but resilient (retry, connectivity, cache, auth, log, priority...)
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.
An api definition with some attributes:
[assembly:Policy("TransientHttpError")]
namespace Apizr.Sample
{
[WebApi("https://reqres.in/"), Cache, Log]
public interface IReqResService
{
[Get("/api/users")]
Task<UserList> GetUsersAsync();
[Get("/api/users/{userId}")]
Task<UserDetails> GetUserAsync([CacheKey] int userId);
[Post("/api/users")]
Task<User> CreateUser(User user);
}
}
An instance of this managed api:
// Define some policies
var registry = new PolicyRegistry
{
{
"TransientHttpError",
HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
})
}
};
// Get your manager instance
var reqResManager = ApizrBuilder.Current.CreateManagerFor<IReqResService>(
options => options
.WithPolicyRegistry(registry)
.WithAkavacheCacheHandler());
And then you're good to go:
var userList = await reqResManager.ExecuteAsync(api => api.GetUsersAsync());This request will be managed with the defined policies, data cached, http traces logged.
Apizr has a lot more to offer, just read the doc!
| Project | Current | Upcoming |
|---|---|---|
| Apizr.Extensions.Microsoft.Caching | ||
| Apizr.Integrations.Akavache | ||
| Apizr.Integrations.MonkeyCache |
| Project | Current | Upcoming |
|---|---|---|
| Apizr.Integrations.Fusillade | ||
| Apizr.Integrations.MediatR | ||
| Apizr.Integrations.Optional |
| Project | Current | Upcoming |
|---|---|---|
| Apizr.Integrations.FileTransfer | ||
| Apizr.Extensions.Microsoft.FileTransfer | ||
| Apizr.Integrations.FileTransfer.MediatR | ||
| Apizr.Integrations.FileTransfer.Optional |
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:
| Package | Features |
|---|---|
| Refit | Auto-implement web api interface and deal with HttpClient |
| Polly | Apply some policies like Retry, CircuitBreaker, etc... |
| Microsoft.Extensions.Logging.Abstractions | Delegate logging layer to MS Extensions Logging |
It also comes with some handling interfaces to let you provide your own services for: