Skip to content

Commit

Permalink
[Aggregator] Added HttpClientExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
hidayatarg committed Oct 12, 2023
1 parent 82749d5 commit 9299d18
Showing 1 changed file with 31 additions and 0 deletions.
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);
// }
}

0 comments on commit 9299d18

Please sign in to comment.