Provide a way to customize the HttpClient on UriImageSource
Description
Provide a way to customize the HttpClient (or possible the HttpClientHandler) used on UIrImageSource here:
https://github.com/dotnet/maui/blob/abada83230d6ffb6f78af0b510b8ea3fcba1b863/src/Controls/src/Core/UriImageSource.cs#L117
Public API Changes
I'm not sure of the best API shape.
Unsealing UriImageSource and changing DownloadStreamAsync to be protected virtual would be one solution.
Getting HttpClient from the IServiceCollection would be a bigger change (or changing to use IHttpClientFactory).
Intended Use-Case
My main requirement is the ability to add custom headers when using HTTP sourced images.
Related https://github.com/dotnet/maui/pull/10995
I saw 2 closed PRs when with this feature, would really appreciate to hear today's team's vision on this matter.
Bringing some thoughts:
- A real-world serious app would (personal bet and exp) include http extensions nuget to use httpfactory for intensive API calls. Not to include it targeting light demo app cases is a debatable choice for me.
- Platform implementations like Android Glide should not limit the shared code to become more robust and modern, but should adapt to it. At the same time we can just omit http extensions for Android and think of a way to provide a fancy Glide builder customization method.
Lyrics: For me this issue became an obvious problem for me when I got app to build where I had like 50% of images not loading, URLs were provided by third-party API. The cause was found to be that those URLs were coming from wiki that required UserAgent to be specified in client headers.
Created some example code for the discussion.
I used <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.4" /> to add retry policy to image source.
https://github.com/taublast/maui/commit/4e4bd82c66022815d054064f71aeb318a513a2c9
Some tests:
One of the tests demonstrate how one could customize the client, we allow access to builder too:
This extension method is intended to be public.
To marry Glide with this, would think of providing a platform-conditional delegate code allowing to customize Glide builder in a same way we could allow with the IHttpCLientBuilder.
Customizing Glide loader example: https://github.com/taublast/maui/commit/5c1736dcd55b52eda701e0fd91a5c96a79b68460
Anyway we all need more thoughts and opinions from the team.