using ComputerShopContracts.ViewModels; using Newtonsoft.Json; using System.Net.Http.Headers; using System.Text; namespace ComputerShopGuarantorApp { public static class ApiUser { private static readonly HttpClient _client = new(); public static UserViewModel? User { get; set; } = null; 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 T? GetRequest(string RequestUrl) { var Response = _client.GetAsync(RequestUrl); var Result = Response.Result.Content.ReadAsStringAsync().Result; if (Response.Result.IsSuccessStatusCode) { return JsonConvert.DeserializeObject(Result); } else { throw new Exception(Result); } } public static void PostRequest(string RequestUrl, T Model) { var Json = JsonConvert.SerializeObject(Model); var Data = new StringContent(Json, Encoding.UTF8, "application/json"); var Response = _client.PostAsync(RequestUrl, Data); var Result = Response.Result.Content.ReadAsStringAsync().Result; if (!Response.Result.IsSuccessStatusCode) { throw new Exception(Result); } } } }