Files
CartriX/CartriX.Shared/RequestsToApi/PrinterApiRequest.cs
T

38 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
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;
}
}
}