Added APIInterface

This commit is contained in:
luke-else 2021-10-31 16:36:14 +00:00
parent e1875fde75
commit d86378a9a8
3 changed files with 85 additions and 4 deletions

View File

@ -0,0 +1,80 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Net.Http;
namespace EFB.Controllers.API
{
public class APIInterface
{
private HttpClient HttpClient { get; set; }
public async Task<T> Get<T>(string Endpoint, Dictionary<string, string> Headers){
this.HttpClient = new HttpClient();
this.HttpClient.DefaultRequestHeaders.Clear();
foreach (var Header in Headers)
{
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
}
if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint))
{
var pendingResult = this.HttpClient.GetAsync(Endpoint);
var result = await pendingResult;
string resultString = result.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<T>(resultString);
}else{
T empty = default(T);
return empty;
}
}
public async Task<T> Post<T>(string Endpoint, Dictionary<string, string> Headers, object Body){
this.HttpClient = new HttpClient();
this.HttpClient.DefaultRequestHeaders.Clear();
foreach (var Header in Headers)
{
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
}
StringContent content = new StringContent(JsonConvert.SerializeObject(Body), Encoding.UTF8, "application/json");
if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint))
{
var pendingResult = this.HttpClient.PostAsync(Endpoint, content);
var result = await pendingResult;
string resultString = result.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<T>(resultString);
}else{
T empty = default(T);
return empty;
}
}
}
}

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EFB.Controllers.FormAuthenticator
namespace EFB.Controllers.Form
{
public static class FormAuthenticator
{

View File

@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
</ItemGroup>
</Project>