(response);
}
public async Task Putusing Microsoft.Extensions.Logging;
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace MyProject;
public interface IMyApiClient
{
Task<T?> Get<T>(string url);
Task Put<T>(string url, T body);
Task Post<T>(string url, T body);
}
public class MyApiClient : IMyApiClient
{
private readonly HttpClient client;
private readonly ILogger<MyApiClient> logger;
public MyApiClient(HttpClient client, ILogger<MyApiClient> logger)
{
this.client = client;
this.logger = logger;
}
public async Task<T?> Get<T>(string url)
{
HttpResponseMessage response = await client.GetAsync($"{CleanedBaseAddress}/{CleanUrl(url)}");
return await ParseResponse<T?>(response);
}
public async Task Put<T>(string url, T body)
{
string bodyText = Serialize(body);
HttpContent content = new StringContent(bodyText, Encoding.UTF8, "application/json");
await client.PutAsync($"{CleanedBaseAddress}/{CleanUrl(url)}", content);
}
public async Task Post<T>(string url, T body)
{
string bodyText = Serialize(body);
HttpContent content = new StringContent(bodyText, Encoding.UTF8, "application/json");
await client.PostAsync($"{CleanedBaseAddress}/{CleanUrl(url)}", content);
}
private static async Task<T?> ParseResponse<T>(HttpResponseMessage response)
{
string contentString = await response.Content.ReadAsStringAsync();
return Deserialize<T>(contentString);
}
private static readonly JsonSerializerOptions JSON_SERIALIZER_POLICY = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
private static T? Deserialize<T>(string value)
{
try
{
return JsonSerializer.Deserialize<T>(value, JSON_SERIALIZER_POLICY);
}
catch (Exception ex)
{
throw new InvalidOperationException($"Unable to parse request from API: \\n\\n{value}", ex);
}
}
private string? CleanedBaseAddress => client.BaseAddress?.ToString().Trim().TrimEnd('/');
private string CleanUrl(string url) => url.Trim().TrimStart('/');
private static string Serialize<T>(T value) =>
JsonSerializer.Serialize(value, JSON_SERIALIZER_POLICY);public async Task<T?> Get<T>(string url)
{
HttpResponseMessage response = await client.GetAsync($"{CleanedBaseAddress}/{CleanUrl(url)}");
return await ParseResponse<T?>(response);
}
public async Task Put<T>(string url, T body)
{
string bodyText = Serialize(body);
HttpContent content = new StringContent(bodyText, Encoding.UTF8, "application/json");
await client.PutAsync($"{CleanedBaseAddress}/{CleanUrl(url)}", content);
}
public async Task Post<T>(string url, T body)
{
string bodyText = Serialize(body);
HttpContent content = new StringContent(bodyText, Encoding.UTF8, "application/json");
await client.PostAsync($"{CleanedBaseAddress}/{CleanUrl(url)}", content);
}
private static async Task<T?> ParseResponse<T>(HttpResponseMessage response)
{
string contentString = await response.Content.ReadAsStringAsync();
return Deserialize<T>(contentString);
}
private static readonly JsonSerializerOptions JSON_SERIALIZER_POLICY = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
private static T? Deserialize<T>(string value)
{
try
{
return JsonSerializer.Deserialize<T>(value, JSON_SERIALIZER_POLICY);
}
catch (Exception ex)
{
throw new InvalidOperationException($"Unable to parse request from API: \\n\\n{value}", ex);
}
}
private string? CleanedBaseAddress => client.BaseAddress?.ToString().Trim().TrimEnd('/');
private string CleanUrl(string url) => url.Trim().TrimStart('/');
private static string Serialize<T>(T value) =>
JsonSerializer.Serialize(value, JSON_SERIALIZER_POLICY);
}