-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Aggregator] Added HttpClientExtensions
- Loading branch information
1 parent
82749d5
commit 9299d18
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/ApiGateways/Shopping.Aggregator/Extensions/HttpClientExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
|
||
namespace Shopping.Aggregator.Extensions; | ||
|
||
public static class HttpClientExtensions | ||
{ | ||
public static async Task<T> ReadContentAs<T>(this HttpResponseMessage response) | ||
{ | ||
if(!response.IsSuccessStatusCode) | ||
throw new ApplicationException($"Something went wrong calling the API: {response.ReasonPhrase}"); | ||
|
||
var dataAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
return JsonSerializer.Deserialize<T>(dataAsString, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); | ||
} | ||
|
||
// public static Task<HttpResponseMessage> PostAsJsonAsync<T>( | ||
// this HttpClient httpClient, string url, T data) | ||
// { | ||
// var dataAsString = JsonConvert.SerializeObject(data); | ||
// var content = new StringContent(dataAsString); | ||
// content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); | ||
// return httpClient.PostAsync(url, content); | ||
// } | ||
|
||
// public static async Task<T> ReadAsJsonAsync<T>(this HttpContent content) | ||
// { | ||
// var dataAsString = await content.ReadAsStringAsync(); | ||
// return JsonConvert.DeserializeObject<T>(dataAsString); | ||
// } | ||
} |