4 years and counting between Apizr v1 and its latest v6.4.
This resilient Refit manager has been developped and maintained for my own needs, but open sourced and published on NuGet for free from the begining.
That said, I did not took the time to show how to use each and every features of it, so that’s what I’m trying to do now by creating this series.
I’ll build a MAUI sample app called StarCellar to manage a wine cellar (I’m french after all 🙂 ), both with and without Apizr to show what’s different while requesting a minimal API.
So let’s get started!
If you’re new to Apizr, you shoud know that it’s based on Refit and aims to provide many more features on top of it, like retry handling, connectivity check, cache management, authentication handling, priority management, and so on… It’s here to help me to don’t repeat myself into each and every projects while dealing with api request calls and resilience needs.
Get the big picture by reading the documentation:
Feel free to browse code, tests and samples and maybe to submit PRs or ideas:
Don’t forget the YouTube Channel Playlist about Apizr:
The starcellar series
The StarCellar walkthrough series is built thanks to multiple episodes, each with a dedicated source code branch, blog post and playlist video.
Feel free to fork the project and start from the main branch, so that you could follow this tutorial coding by yourself. If you do so, don’t forget to run the API through a Dev Tunnel and to update the MAUI app’s base address.
the starcellar episode 01
The current episode is covered by this video:
The application
Like I said, there’s a minimal backend API project and 2 MAUI projects:
Project | Using |
---|---|
StarCellar.Api | Minimal backend API offering wine bottle CRUDing, file management and authentication |
StarCellar.Without.Apizr | MAUI app to manage the cellar without using Apizr but Refit with 3rd part libs |
StarCellar.With.Apizr | MAUI app to manage the cellar by using Apizr with integrated libs |
The MAUI app is quite simple with features like:
- Show all wines from the cellar in a list view
- Show more infos about the selected wine in a details view
- Change wine infos in a edit view (image, title, description and score)
- Add a new wine to the cellar in the edit view
For this Getting Started episode, both With and Without projects are yet configured and functional with just Refit in place, so I’ll work on the With project only.
installing apizr
Start by removing the Refit package from the With project and install the Apizr.Extensions.Microsoft.DependencyInjection one instead.
Setting base address by attribute
Open the ICellarApi
interface from Services > Apis > Cellar folder.
Add a BaseAddress
attribute over the interface with Constants.BaseAddress
as parameter.
Repeat the operation with the IFileApi
.
Registering Apizr
Into MauiProgram, replace folowing former Refit registration block:
builder.Services.AddRefitClient<ICellarApi>() .ConfigureHttpClient(c => c.BaseAddress = new Uri(Constants.BaseAddress)); builder.Services.AddRefitClient<IFileApi>() .ConfigureHttpClient(c => c.BaseAddress = new Uri(Constants.BaseAddress));
by the Apizr one:
builder.Services.AddApizrManagerFor<ICellarApi>(); builder.Services.AddApizrManagerFor<IFileApi>();
Using Apizr managers from ViewModels
Now the managers are registered, let’s use it from our ViewModels.
Open all ViewModels from ViewModels folder.
Inject IApizrManager<ICellarApi> _cellarApiManager
instead of ICellarApi _cellarApi
into all ViewModel’s constructors.
Now use the managed api!
- Replace call:
_cellarApi.GetWinesAsync();
by the managed one:
_cellarApiManager.ExecuteAsync(api => api.GetWinesAsync());
- Then replace call:
_cellarApi.DeleteWineAsync(Wine.Id);
by the managed one:
_cellarApiManager.ExecuteAsync(api => api.DeleteWineAsync(Wine.Id));
- Then replace call:
_cellarApi.CreateWineAsync(Wine);
by the managed one:
_cellarApiManager.ExecuteAsync(api => api.CreateWineAsync(Wine));
- Finaly replace call:
_cellarApi.UpdateWineAsync(Wine.Id, Wine);
by the managed on:
_cellarApiManager.ExecuteAsync(api => api.UpdateWineAsync(Wine.Id, Wine));
We also have to manage the file api, so inject IApizrManager<IFileApi> _fileApiManager
instead of IFileApi _fileApi
into WineEditViewModel’s constructor.
- Then replace call:
_fileApi.UploadAsync(streamPart);
by the managed one:
_fileApiManager.ExecuteAsync(api => api.UploadAsync(streamPart));
And we’re good!
All calls will be managed by Apizr, so here in this example, Apizr will set the base address by design.
Running the app
Nothing’s changed on the user experience point of view. The same app, but with requests managed by Apizr:
get more of it
Feel free to ask me anything on twitter, or opening a discussion, issue or PR on GitHub.
Again, it’s basically all built for my own use and motivated by my needs. But it’s live on Nuget and opened to all so feel free to contribute.
Apizr brings many more features so you should head to the next episode to continue your walkthrough path.