using Newtonsoft.Json;
using System.Text;
namespace Wrapper_Api_SequentMicrosystems.Main
{
internal class ApiClient
{
private HttpClient _httpClient;
public ApiClient()
{
_httpClient = new HttpClient();
}
///
/// Get data with GET method
///
/// Waited type of data
/// String with API endpoint
/// Requested Data
/// Error message
public async Task GetData(string Endpoint)
{
string response = await _httpClient.GetStringAsync(Endpoint);
try
{
return JsonConvert.DeserializeObject(response);//await response.Content.ReadAsStringAsync());
}
catch (Exception ex) { }
{
throw new Exception("GetData deserialize error");
}
}
///
/// Send data to api with POST method
///
///
/// Api Enpoint
/// Data for send to API
/// Error Message
public async void PostData(string Endpoint, Type data)
{
StringContent sc = new(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
HttpResponseMessage response = await _httpClient.PostAsync(Endpoint, sc);
if (response.IsSuccessStatusCode)
{
return;
}
else
{
throw new Exception(response.EnsureSuccessStatusCode().Content.ToString());
}
}
///
/// Delete data from Api with DELETE method
///
/// Api Endpoint
/// Error message
public async void DeleteData(string Endpoint)
{
HttpResponseMessage response = await _httpClient.DeleteAsync(Endpoint);
if (response.IsSuccessStatusCode)
{
return;
}
else
{
throw new Exception(response.EnsureSuccessStatusCode().Content.ToString());
}
}
}
}