Skip to content

[Feature Request] Allow for configuring HttpClient in AddDownstreamWebApiย #891

Closed

Description

Is your feature request related to a problem? Please describe.
I want to be able to configure the HttpClient made inside the AddDownstreamWebApi call.

I'd like access to HttpClient to be able to set default headers etc. client.DefaultRequestHeaders.
I'd like access to IHttpClientBuilder to be able to call ConfigurePrimaryHttpMessageHandler.

Describe the solution you'd like
The default DownstreamWebApi implementation is nice. But a bit restrictive.

Can't we just get a named client with the token stuff sorted :)

Example: (probably not a good api, but should highlight the idea)

// config
services.AddMicrosoftIdentityWebAppAuthentication(Configuration)
    .EnableTokenAcquisitionToCallDownstreamApi()
    .AddDownstreamHttpClient("MyDownstreamApi",
        client =>
        {
            client.DefaultRequestHeaders.Add(HeaderNames.Accept, MediaTypeNames.Application.Json);
        },
        builder => 
        {
            builder.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
            {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            });
        })
    .AddInMemoryTokenCaches();

// usage
using var httpClient = _httpClientFactory.CreateClient("MyDownstreamApi");
// .. do stuff

You would then wrap the usage in a service and everyone would be happy ๐ŸŒฎ

This could probably also work for typed clients.

Describe alternatives you've considered
Alternatively we would have to use ITokenAcquisition and write our own http client and copy this

public async Task<HttpResponseMessage> CallWebApiForUserAsync(

But that might change in future versions

Activity

  1. jennyf19 commented on Jan 8, 2022

    @jennyf19
    Collaborator
  2. added this to the 1.22.0 milestone on Jan 8, 2022
  3. jmprieur commented on Jan 8, 2022

    @jmprieur
    Collaborator
  4. snebjorn commented on Jan 10, 2022

    @snebjorn
    Author

    Excellent! Thank you โค๏ธ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions