Some thoughts on CallWebApiForUserAsync2<TInput, TOutput>
See original GitHub issueI 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 });
}
- Current CallWebApiForUserAsync only accepts OK status code
- Current CallWebApiForUserAsync seems to not be able to accept Ok() returned from webapi i.e. blank string
- 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:
- Created 3 years ago
- Comments:12 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @parwejp these are fair points. Would you mind opening a new issue with this?
@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