mirror of
https://gitlab.com/Hukuta47/cartrix.git
synced 2026-12-05 04:20:55 +00:00
34 lines
1020 B
C#
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;
|
|
}
|
|
}
|
|
} |