public class UsuarioService
{
private readonly HttpClient _httpClient;
public UsuarioService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<Usuario> GetUsuarioAsync(int id)
{
var response = await _httpClient.GetAsync($"/api/usuarios/{id}");
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Usuario>(content);
}
throw new HttpRequestException($"HTTP {response.StatusCode}: {response.ReasonPhrase}");
}
public async Task<Usuario> CreateUsuarioAsync(CreateUsuarioDto dto)
{
var json = JsonSerializer.Serialize(dto);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("/api/usuarios", content);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Usuario>(responseContent);
}
throw new HttpRequestException($"HTTP {response.StatusCode}: {response.ReasonPhrase}");
}
}