Scroll Top

StarCellar E01: Getting Started with Apizr

SCE01_gif

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:

Read - Documentation

Feel free to browse code, tests and samples and maybe to submit PRs or ideas:

Browse - Source

Don’t forget the YouTube Channel Playlist about Apizr:

Watch - Tutorials

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.

Episode title Sample code Blog post Playlist video
> 01 – Getting started Branch - 01-Getting_started Blog - 01 - Getting started Watch - 01-Getting_started
02 – Adjusting basics Branch - 02-Adjusting_basics Blog - 02 - Adjusting basics Watch - 02 - Adjusting basics
03 – Configuring logging Branch - 03 - Configuring logging Blog - 03 - Configuring logging Watch - 03 - Configuring logging
04 – Applying policies Branch - 04 - Applying policies Blog - 04 - Applying policies Watch - 04 - Applying policies
05 – Checking connectivity Branch - 05 - Checking connectivity Blog - 05 - Checking connectivity Watch - 05 - Checking connectivity
06 – Handling exceptions Branch - 06 - Handling exceptions Blog - 06 - Handling exceptions Watch - 06 - Handling exceptions
07 – Caching data Branch - 07 - Caching data Blog - 07 - Caching data Watch - 07 - Caching data
08 – Mapping data Branch - 08 - Mapping data Blog - 08 - Mapping data Watch - 08 - Mapping data
09 – Managing priority Branch - 09 - Managing priority Blog - 09 - Managing priority Watch - 09 - Managing priority
10 – Handling authentication Branch - 10 - Handling authentication Blog - 10 - Handling authentication Watch - 10 - Handling authentication
11 – Using MediatR Branch - 11 - Using MediatR Blog - 11 - Using MediatR Watch - 11 - Using MediatR
12 – Using OptionalAsync Branch - 12 - Using OptionalAsync Blog - 12 - Using OptionalAsync Watch - 12 - Using OptionalAsync
13 – Transfering files Branch - 13 - Transfering files Blog - 13 - Transfering files Watch - 13 - Transfering files
14 – Generating all Branch - 14 - Generating all Blog - 14 - Generating all Watch - 14 - Generating all

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.

Specialized since 2013 in cross-platform applications development for iOS, Android and Windows, using technologies such as Microsoft Xamarin and Microsoft Azure. Initially focused, since 2005, on development, then administration of Customer Relationship Management systems, mainly around solutions such as Microsoft SharePoint and Microsoft Dynamics CRM.

Related Posts

Leave a comment