using ComputerShopContracts.ViewModels; using ComputerShopDatabaseImplement.Models; using ComputerShopDataModels.Models; using Newtonsoft.Json; using System.Data; using System.Net.Http.Headers; using System.Text; namespace ComputerShopImplementerApp { public class APIUser { private static readonly HttpClient _user = new HttpClient { Timeout = TimeSpan.FromMinutes(10) }; public static UserViewModel? User { get; set; } = null; public static void Connect(IConfiguration configuration) { _user.BaseAddress = new Uri(configuration["IPAddress"]); _user.DefaultRequestHeaders.Accept.Clear(); _user.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } public static T? GetRequest(string requestUrl) { var response = _user.GetAsync(requestUrl); var result = response.Result.Content.ReadAsStringAsync().Result; if (response.Result.IsSuccessStatusCode) { // var settings = new JsonSerializerSettings // { // TypeNameHandling = TypeNameHandling.All //}; //return JsonConvert.DeserializeObject(result, settings); return JsonConvert.DeserializeObject(result); } else { throw new Exception(result); } } public static async Task GetRequestAsync(string requestUrl) { var response = await _user.GetAsync(requestUrl); var result = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { return JsonConvert.DeserializeObject(result); } else { throw new Exception(result); } } public static async Task GetRequestShipmentAsync(string requestUrl) { var response = await _user.GetAsync(requestUrl); var result = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var settings = new JsonSerializerSettings { Converters = new List { new OrderConverter() } }; return JsonConvert.DeserializeObject(result, settings); } else { throw new Exception(result); } } public static async Task GetRequestRequestAsync(string requestUrl) { var response = await _user.GetAsync(requestUrl); var result = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var settings = new JsonSerializerSettings { Converters = new List { new RequestConverter(), new AssemblyConverter() } }; return JsonConvert.DeserializeObject(result, settings); } 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 = _user.PostAsync(requestUrl, data); var result = response.Result.Content.ReadAsStringAsync().Result; if (!response.Result.IsSuccessStatusCode) { throw new Exception(result); } } public static async Task PostRequestAsync(string requestUrl, T model) { var json = JsonConvert.SerializeObject(model); var data = new StringContent(json, Encoding.UTF8, "application/json"); var response = await _user.PostAsync(requestUrl, data); var result = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { throw new Exception(result); } } } public class OrderConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(IOrderModel); //return true; } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return serializer.Deserialize(reader); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { serializer.Serialize(writer, value); } } public class RequestConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(IRequestModel); //return true; } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return serializer.Deserialize(reader); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { serializer.Serialize(writer, value); } } public class AssemblyConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(IAssemblyModel); //return true; } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return serializer.Deserialize(reader); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { serializer.Serialize(writer, value); } } }