using System.Net.Http.Json;
using Front.Program.Models;

namespace Front.Program.Services;

public class APIService
{
    private readonly HttpClient _http;
    

    public APIService(HttpClient http)
    {
        _http = http;
    }

    
    private string ChangeToString<T>(T data)
    {
        if (data == null) return string.Empty;
        var properties = typeof(T).GetProperties();
        var value = properties.Select(p => $"{p.Name}={p.GetValue(data)}");
        return string.Join("&", value);
    }
    
    public async Task<APIResponseStatus<TResponse>?> GetJsonAsync<TResponse,TRequest>(string url, TRequest value)
    {
        string parameter = ChangeToString(value);
        var response = await _http.GetFromJsonAsync<APIResponseStatus<TResponse>>($"{url}?{parameter}");
        return response;
    }
}