2024-11-23 11:53:53 +04:00
|
|
|
|
using EventVisitorLogic.BindingModels;
|
|
|
|
|
using EventVisitorLogic.ViewModels;
|
2024-11-03 19:42:19 +04:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace EventVisitorClientApp
|
|
|
|
|
{
|
|
|
|
|
public class APIClient
|
|
|
|
|
{
|
|
|
|
|
private static readonly HttpClient _client = new();
|
|
|
|
|
public static OrganizerViewModel? Client { 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<T>(string requestUrl)
|
|
|
|
|
{
|
|
|
|
|
var response = _client.GetAsync(requestUrl);
|
|
|
|
|
var result = response.Result.Content.ReadAsStringAsync().Result;
|
|
|
|
|
if (response.Result.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(result);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static void PostRequest<T>(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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-23 11:53:53 +04:00
|
|
|
|
|
|
|
|
|
public static int PostRequestAndGetId<T>(string requestUrl, T model)
|
|
|
|
|
{
|
|
|
|
|
var json = JsonConvert.SerializeObject(model);
|
|
|
|
|
var data = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
|
var response = _client.PostAsync(requestUrl, data).Result;
|
|
|
|
|
var result = response.Content.ReadAsStringAsync().Result;
|
|
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
// Попробуем десериализовать результат
|
|
|
|
|
var createdObject = JsonConvert.DeserializeObject<VisitorBindingModel>(result);
|
|
|
|
|
|
|
|
|
|
// Добавьте проверку на null
|
|
|
|
|
if (createdObject == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Не удалось получить созданный объект из ответа сервера: " + result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Теперь безопасно обращаемся к Id
|
|
|
|
|
return createdObject.Id;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<int> PostRequestAsync<T>(string requestUrl, T model)
|
|
|
|
|
{
|
|
|
|
|
var json = JsonConvert.SerializeObject(model);
|
|
|
|
|
var data = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
|
|
|
|
|
|
// Выполняем асинхронный запрос
|
|
|
|
|
var response = await _client.PostAsync(requestUrl, data);
|
|
|
|
|
|
|
|
|
|
// Читаем результат как строку
|
|
|
|
|
var result = await response.Content.ReadAsStringAsync();
|
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Попытка десериализовать результат, чтобы получить ID (например, ожидаем, что API возвращает объект с ID)
|
|
|
|
|
var createdVisitor = JsonConvert.DeserializeObject<VisitorBindingModel>(result);
|
|
|
|
|
return createdVisitor.Id; // Предполагается, что у вас есть свойство Id в VisitorBindingModel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-11-03 19:42:19 +04:00
|
|
|
|
}
|
|
|
|
|
}
|