using System.Net.Http.Json;

namespace RobotNet.Clients;

public static class HttpClientExtensions
{
    public static async Task<TValue?> PostFromJsonAsync<TValue>(this HttpClient client, string requestUri, object value)
    {
        var response = await client.PostAsJsonAsync(requestUri, value);
        if (response.IsSuccessStatusCode)
        {
            return await response.Content.ReadFromJsonAsync<TValue>();
        }
        return default;
    }

    public static async Task<TValue?> PutFromJsonAsync<TValue>(this HttpClient client, string requestUri, object value)
    {
        var response = await client.PutAsJsonAsync(requestUri, value);
        if (response.IsSuccessStatusCode)
        {
            return await response.Content.ReadFromJsonAsync<TValue>();
        }
        return default;
    }

    public static async Task<TValue?> PatchFromJsonAsync<TValue>(this HttpClient client, string requestUri, object value)
    {
        var response = await client.PatchAsJsonAsync(requestUri, value);
        if (response.IsSuccessStatusCode)
        {
            return await response.Content.ReadFromJsonAsync<TValue>();
        }
        return default;
    }
}
