dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.63k stars 1.62k forks source link

Provide a way to customize the HttpClient on UriImageSource #22057

Open OliverBrown-Next opened 2 weeks ago

OliverBrown-Next commented 2 weeks ago

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.

PureWeen commented 2 weeks ago

Related https://github.com/dotnet/maui/pull/10995

taublast commented 1 week ago

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:

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:

image

One of the tests demonstrate how one could customize the client, we allow access to builder too:

image

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.