question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Some thoughts on CallWebApiForUserAsync2<TInput, TOutput>

See original GitHub issue

I have switched some existing code to use IDownstreamWebApi which is great…

In the end I i knocked together the following so the app could access an existing API

public static async Task<TOutput> CallWebApiForUserAsync2<TInput, TOutput>(this IDownstreamWebApi downstreamWebApi, string optionsInstanceName, TInput input, Action<DownstreamWebApiOptions> downstreamWebApiOptionsOverride = null, ClaimsPrincipal user = null)
        {
            HttpResponseMessage response = await downstreamWebApi.CallWebApiForUserAsync(
                optionsInstanceName,
                downstreamWebApiOptionsOverride,
                user,
                new StringContent(JsonSerializer.Serialize(input), Encoding.UTF8, "application/json")
                ).ConfigureAwait(false);

            try
            {
                response.EnsureSuccessStatusCode();
            }
            catch
            {
                string error = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                throw new HttpRequestException($"{(int)response.StatusCode} {response.StatusCode} {error}");
            }

            string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (string.IsNullOrWhiteSpace(content))
                return default;

            return JsonSerializer.Deserialize<TOutput>(content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
        }
  1. Current CallWebApiForUserAsync only accepts OK status code
  2. Current CallWebApiForUserAsync seems to not be able to accept Ok() returned from webapi i.e. blank string
  3. Current CallWebApiForUserAsync only accepts TOutput as class so no OK(332)

I imagine IDownstreamWebApi might be still a work in progress?.. be great to hear how best to utilize it.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jmprieurcommented, Mar 15, 2021

Thanks @parwejp these are fair points. Would you mind opening a new issue with this?

0reactions
parwejpcommented, Mar 15, 2021

@jmprieur : I am no expert, but according to .NET microservices - Architecture e-book HttpMessageHandlers are pooled, not HttpClient. The document encourages the use of IHttpClientFactory as provided by Typed Clients, described as

… just an HttpClient that’s pre-configured …[with some] … specific values such as the base server, HTTP headers or time outs.

Of course, DownstreamWebApi is just such a typed client.

The document also encourages the use of DelegatingHandlers for fine tuning requests on the way out and in. It also has a reference to Make HTTP requests using IHttpClientFactory in ASP.NET Core describing the pattern in more detail.

To be able to do this, though, one needs access to the IHttpClientBuilder returned from AddHttpClient.

In a production app, one would most likely write a dedicated typed client in any respect. However, providing a way to configure and add handlers to DownstreamWebApi would, together with the suggestions above, make it more flexible.

Maybe an overload of AddDownstreamApi that accepts Action<IHttpClientBuilder> would do the trick?

What do you think?

@jennyf19 : I am not able to find out how to send request headers while using get requests on iDownStreamWebAPI. I recently got an error while calling MS graph api to add “ConsistencyLevel = eventual” header. Microsoft Graph advanced queries Header Have switched to ITokenAquisition implementation for now but would be nice to use IDownStreamWebAPI for it. As @rollandjb mentioned it would be possible to set request headers with pooled HttpClient with proper isolation. I am no expert but creating HttpRequestMessage could be quick fix. If it is implemented already, could you please be kind enough to point me out to documentation?

Many Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

IDownstreamWebApi.CallWebApiForUserAsync Method
Calls a downstream web API consuming JSON with some data and returns data. CallWebApiForUserAsync<TInput,TOutput>(String, TInput, String, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found