EFB/Controllers/API/APIInterface.cs

147 lines
5.8 KiB
C#
Raw Normal View History

2022-02-28 21:53:28 +00:00
using EFB.Models;
2021-10-31 16:36:14 +00:00
using Newtonsoft.Json;
2022-02-28 21:53:28 +00:00
using System.Collections.Generic;
2021-10-31 16:36:14 +00:00
using System.Net.Http;
2022-02-28 21:53:28 +00:00
using System.Threading.Tasks;
2021-10-31 16:36:14 +00:00
namespace EFB.Controllers.API
{
public class APIInterface
{
private HttpClient HttpClient { get; set; }
2022-02-28 21:53:28 +00:00
public async Task<ResponseModel<T>> Get<T>(string Endpoint, Dictionary<string, string> Headers)
{
//Create the HTTP client used for the GetRequest.
2021-10-31 16:36:14 +00:00
this.HttpClient = new HttpClient();
this.HttpClient.DefaultRequestHeaders.Clear();
2021-10-31 18:58:30 +00:00
if (Headers != null)
2021-10-31 16:36:14 +00:00
{
2021-10-31 18:58:30 +00:00
foreach (var Header in Headers)
{
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
}
2021-10-31 16:36:14 +00:00
}
2021-10-31 18:58:30 +00:00
if (Form.FormAuthenticator.ValidateEndpoint(Endpoint))
2021-10-31 16:36:14 +00:00
{
try
{
//Get the response from the server
var pendingResult = this.HttpClient.GetAsync(Endpoint);
var result = await pendingResult;
string resultString = result.Content.ReadAsStringAsync().Result;
2021-10-31 16:36:14 +00:00
//Assess return value (object or raw string)
object response = resultString;
if (typeof(T) != typeof(string))
{//If the user requests string for return type
response = JsonConvert.DeserializeObject<T>(resultString);
}
2022-02-28 21:53:28 +00:00
return new ResponseModel<T>()
{
//Sender should be aware of type T becuase of Generic type
Result = (T)response
};
}
catch (System.Exception e)
{
2022-02-28 21:53:28 +00:00
return new ResponseModel<T> { Error = e.Message };
}
2021-10-31 16:36:14 +00:00
}
//Returned in the event No other response has been returned
2022-02-28 21:53:28 +00:00
return new ResponseModel<T> { Error = "Invalid Endpoint - Please try again later" };
2021-10-31 16:36:14 +00:00
}
2022-02-28 21:53:28 +00:00
public async Task<ResponseModel<T>> Post<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body)
{
//Create a HTTP client to allow for making Post requests
2021-10-31 16:36:14 +00:00
this.HttpClient = new HttpClient();
this.HttpClient.DefaultRequestHeaders.Clear();
2021-10-31 16:36:14 +00:00
2021-10-31 18:58:30 +00:00
if (Headers != null)
{//Go through and add each header to the HTTP Client
2021-10-31 18:58:30 +00:00
foreach (var Header in Headers)
{
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
}
2021-10-31 16:36:14 +00:00
}
2021-10-31 18:58:30 +00:00
if (Form.FormAuthenticator.ValidateEndpoint(Endpoint))
2021-10-31 16:36:14 +00:00
{
2022-02-28 21:53:28 +00:00
try
{//Try statement to catch errors in the process of making the request
var pendingResult = this.HttpClient.PostAsync(Endpoint, Body);
var result = await pendingResult;
string resultString = result.Content.ReadAsStringAsync().Result;
object response = resultString;
if (typeof(T) != typeof(string))
{//If the user requests string for return type
response = JsonConvert.DeserializeObject<T>(resultString);
}
2022-02-28 21:53:28 +00:00
return new ResponseModel<T>()
{
//Sender should be aware of type T becuase of Generic type
Result = (T)response
};
2022-02-28 21:53:28 +00:00
}
catch (System.Exception e)
{
return new ResponseModel<T> { Error = e.Message };
}
}
//Returned in the event No other response has been returned
2022-02-28 21:53:28 +00:00
return new ResponseModel<T> { Error = "Invalid Endpoint - Please try again later" };
}
2022-02-28 21:53:28 +00:00
public async Task<ResponseModel<T>> Put<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body)
{
//Create HTTP client to allow for HttpRequests
this.HttpClient = new HttpClient();
this.HttpClient.DefaultRequestHeaders.Clear();
if (Headers != null)
{//Loop through and add each heder to the HTTP client
foreach (var Header in Headers)
{
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
}
}
if (Form.FormAuthenticator.ValidateEndpoint(Endpoint))
{
2022-02-28 21:53:28 +00:00
try
{//Try statement to catch errors in the process of making the request
var pendingResult = this.HttpClient.PutAsync(Endpoint, Body);
var result = await pendingResult;
string resultString = result.Content.ReadAsStringAsync().Result;
2022-02-28 21:53:28 +00:00
//Recieve the response as a string which will be morphed in other types
object response = resultString;
if (typeof(T) != typeof(string))
{//If the user requests string for return type
response = JsonConvert.DeserializeObject<T>(resultString);
}
2022-02-28 21:53:28 +00:00
return new ResponseModel<T>()
{
//Sender should be aware of type T becuase of Generic type
Result = (T)response
};
2022-02-28 21:53:28 +00:00
}
catch (System.Exception e)
{
return new ResponseModel<T> { Error = e.Message };
}
2021-10-31 16:36:14 +00:00
}
//Returned in the event No other response has been returned
2022-02-28 21:53:28 +00:00
return new ResponseModel<T> { Error = "Invalid Endpoint - Please try again later" };
}
2021-10-31 16:36:14 +00:00
}
2021-10-31 16:36:14 +00:00
}