2023-05-14 22:11:06 +04:00
|
|
|
|
using ComputerStoreContracts.BindingModels;
|
|
|
|
|
using ComputerStoreContracts.ViewModels;
|
2023-05-14 02:14:53 +04:00
|
|
|
|
using ComputerStoreDataModels.Models;
|
2023-05-13 18:23:36 +04:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace ComputerStoreEmployeeApp
|
|
|
|
|
{
|
|
|
|
|
public class APIClient
|
|
|
|
|
{
|
|
|
|
|
private static readonly HttpClient _client = new();
|
|
|
|
|
public static EmployeeViewModel? Employee { get; set; } = null;
|
|
|
|
|
|
2023-05-14 22:11:06 +04:00
|
|
|
|
public static Dictionary<int, (IComponentModel Component, int Quantity)>? productComponents;
|
|
|
|
|
public static Dictionary<int, (IComponentModel Component, int Quantity)>? pcComponents;
|
2023-05-13 18:23:36 +04:00
|
|
|
|
public static void Connect(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
_client.BaseAddress = new Uri(configuration["IPAddress"]);
|
|
|
|
|
_client.DefaultRequestHeaders.Accept.Clear();
|
|
|
|
|
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<T?> GetRequest<T>(string requestUrl)
|
|
|
|
|
{
|
|
|
|
|
var response = await _client.GetAsync(requestUrl);
|
|
|
|
|
var result = response.Content.ReadAsStringAsync().Result;
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
2023-05-14 22:11:06 +04:00
|
|
|
|
return JsonConvert.DeserializeObject<T>(result, new JsonSerializerSettings()
|
|
|
|
|
{
|
|
|
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
|
|
|
|
});
|
2023-05-13 18:23:36 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 21:14:47 +04:00
|
|
|
|
public static async Task<bool> PostRequest<T>(string requestUrl, T model)
|
2023-05-13 18:23:36 +04:00
|
|
|
|
{
|
|
|
|
|
var json = JsonConvert.SerializeObject(model);
|
2023-05-14 22:11:06 +04:00
|
|
|
|
|
2023-05-13 18:23:36 +04:00
|
|
|
|
var data = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
|
|
|
|
|
|
var response = await _client.PostAsync(requestUrl, data);
|
|
|
|
|
|
|
|
|
|
var result = response.Content.ReadAsStringAsync().Result;
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(result);
|
|
|
|
|
}
|
2023-05-14 22:11:06 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-05-13 21:14:47 +04:00
|
|
|
|
public static async Task<bool> PatchRequest<T>(string requestUrl, T model)
|
2023-05-13 18:23:36 +04:00
|
|
|
|
{
|
|
|
|
|
var json = JsonConvert.SerializeObject(model);
|
|
|
|
|
var data = new StringContent(json, Encoding.UTF8, "application/json-patch+json");
|
|
|
|
|
|
|
|
|
|
var response = await _client.PatchAsync(requestUrl, data);
|
|
|
|
|
|
|
|
|
|
var result = response.Content.ReadAsStringAsync().Result;
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(result);
|
|
|
|
|
}
|
2023-05-13 21:14:47 +04:00
|
|
|
|
return Convert.ToBoolean(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<bool> DeleteRequest<T>(string requestUrl)
|
|
|
|
|
{
|
|
|
|
|
var response = await _client.DeleteAsync(requestUrl);
|
|
|
|
|
var result = response.Content.ReadAsStringAsync().Result;
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(result);
|
|
|
|
|
}
|
|
|
|
|
return Convert.ToBoolean(result);
|
2023-05-13 18:23:36 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|