Files
CartriX/CartriX.Shared/RequestsToApi/PrinterApiRequest.cs
T
2026-05-06 22:32:22 +09:00

34 lines
1020 B
C#

using CartriX.Shared.Models;
using Newtonsoft.Json;
namespace CartriX.Shared.RequestsToApi
{
public static class PrinterApiRequest
{
static readonly HttpClient client = new HttpClient();
public static async Task<Printer?> PrinterByIdAsync(int id)
{
if (id <= 0) return null;
var url = $"http://localhost:5197/api/Printer/{id}";
try
{
var response = await client.GetAsync(url).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<Printer?>(content);
}
catch (HttpRequestException e)
{
Console.WriteLine(e.Message);
}
catch (JsonException e)
{
Console.WriteLine($"JSON error: {e.Message}");
}
return null;
}
}
}