Skip to content

Commit afff1e0

Browse files
Split aot compatible and incompatible methods (#3090)
Co-authored-by: jennyf19 <[email protected]>
1 parent d5c57f9 commit afff1e0

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

src/Microsoft.Identity.Web.DownstreamApi/DownstreamApi.cs

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,56 @@ public Task<HttpResponseMessage> CallApiForAppAsync(
351351

352352
internal static async Task<TOutput?> DeserializeOutputAsync<TOutput>(HttpResponseMessage response, DownstreamApiOptions effectiveOptions)
353353
where TOutput : class
354-
{
355-
return await DeserializeOutputImplAsync<TOutput>(response, effectiveOptions, null);
356-
}
354+
{
355+
try
356+
{
357+
response.EnsureSuccessStatusCode();
358+
}
359+
catch
360+
{
361+
string error = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
357362

358-
private static async Task<TOutput?> DeserializeOutputImplAsync<TOutput>(HttpResponseMessage response, DownstreamApiOptions effectiveOptions, JsonTypeInfo<TOutput>? outputJsonTypeInfo = null)
363+
#if NET5_0_OR_GREATER
364+
throw new HttpRequestException($"{(int)response.StatusCode} {response.StatusCode} {error}", null, response.StatusCode);
365+
#else
366+
throw new HttpRequestException($"{(int)response.StatusCode} {response.StatusCode} {error}");
367+
#endif
368+
}
369+
370+
HttpContent content = response.Content;
371+
372+
if (content == null)
373+
{
374+
return default;
375+
}
376+
377+
string? mediaType = content.Headers.ContentType?.MediaType;
378+
379+
if (effectiveOptions.Deserializer != null)
380+
{
381+
return effectiveOptions.Deserializer(content) as TOutput;
382+
}
383+
else if (typeof(TOutput).IsAssignableFrom(typeof(HttpContent)))
384+
{
385+
return content as TOutput;
386+
}
387+
else
388+
{
389+
string stringContent = await content.ReadAsStringAsync();
390+
if (mediaType == "application/json")
391+
{
392+
return JsonSerializer.Deserialize<TOutput>(stringContent, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
393+
}
394+
if (mediaType != null && !mediaType.StartsWith("text/", StringComparison.OrdinalIgnoreCase))
395+
{
396+
// Handle other content types here
397+
throw new NotSupportedException("Content type not supported. Provide your own deserializer. ");
398+
}
399+
return stringContent as TOutput;
400+
}
401+
}
402+
403+
private static async Task<TOutput?> DeserializeOutputImplAsync<TOutput>(HttpResponseMessage response, DownstreamApiOptions effectiveOptions, JsonTypeInfo<TOutput> outputJsonTypeInfo)
359404
where TOutput : class
360405
{
361406
try
@@ -395,14 +440,7 @@ public Task<HttpResponseMessage> CallApiForAppAsync(
395440
string stringContent = await content.ReadAsStringAsync();
396441
if (mediaType == "application/json")
397442
{
398-
if (outputJsonTypeInfo != null)
399-
{
400-
return JsonSerializer.Deserialize<TOutput>(stringContent, outputJsonTypeInfo);
401-
}
402-
else
403-
{
404-
return JsonSerializer.Deserialize<TOutput>(stringContent, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
405-
}
443+
return JsonSerializer.Deserialize<TOutput>(stringContent, outputJsonTypeInfo);
406444
}
407445
if (mediaType != null && !mediaType.StartsWith("text/", StringComparison.OrdinalIgnoreCase))
408446
{

0 commit comments

Comments
 (0)