Files
Prog4_Beadando/WD7UVN_HFT_2023241.Client/RestService.cs
2023-12-05 15:29:00 +01:00

154 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
namespace WD7UVN_HFT_2023241.Client
{
class RestService
{
HttpClient client;
public RestService(string baseurl, string pingableEndpoint = "api")
{
bool isOk = false;
do
{
isOk = Ping(baseurl + pingableEndpoint);
} while (isOk == false);
Init(baseurl);
}
private bool Ping(string url)
{
try
{
WebClient wc = new WebClient();
wc.DownloadData(url);
return true;
}
catch
{
return false;
}
}
private void Init(string baseurl)
{
client = new HttpClient();
client.BaseAddress = new Uri(baseurl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue
("application/json"));
try
{
client.GetAsync("").GetAwaiter().GetResult();
}
catch (HttpRequestException)
{
throw new ArgumentException("Endpoint is not available!");
}
}
public List<T> Get<T>(string endpoint)
{
List<T> items = new List<T>();
HttpResponseMessage response = client.GetAsync(endpoint).GetAwaiter().GetResult();
if (response.IsSuccessStatusCode)
{
items = response.Content.ReadAsAsync<List<T>>().GetAwaiter().GetResult();
}
else
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
return items;
}
public T GetSingle<T>(string endpoint)
{
T item = default(T);
HttpResponseMessage response = client.GetAsync(endpoint).GetAwaiter().GetResult();
if (response.IsSuccessStatusCode)
{
item = response.Content.ReadAsAsync<T>().GetAwaiter().GetResult();
}
else
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
return item;
}
public T Get<T>(int id, string endpoint)
{
T item = default(T);
HttpResponseMessage response = client.GetAsync(endpoint + "/" + id.ToString()).GetAwaiter().GetResult();
if (response.IsSuccessStatusCode)
{
item = response.Content.ReadAsAsync<T>().GetAwaiter().GetResult();
}
else
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
return item;
}
public void Post<T>(T item, string endpoint)
{
HttpResponseMessage response =
client.PostAsJsonAsync(endpoint, item).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
response.EnsureSuccessStatusCode();
}
public void Delete(int id, string endpoint)
{
HttpResponseMessage response =
client.DeleteAsync(endpoint + "/" + id.ToString()).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
response.EnsureSuccessStatusCode();
}
public void Put<T>(T item, string endpoint)
{
HttpResponseMessage response =
client.PutAsJsonAsync(endpoint, item).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
var error = response.Content.ReadAsAsync<RestExceptionInfo>().GetAwaiter().GetResult();
throw new ArgumentException(error.Msg);
}
response.EnsureSuccessStatusCode();
}
}
public class RestExceptionInfo
{
public RestExceptionInfo()
{
}
public string Msg { get; set; }
}
}