2021-10-31 16:36:14 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
2021-11-22 13:07:56 +00:00
|
|
|
using System;
|
2021-10-31 16:36:14 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System.Net.Http;
|
2021-11-07 21:30:31 +00:00
|
|
|
using EFB.Models;
|
2021-10-31 16:36:14 +00:00
|
|
|
|
|
|
|
namespace EFB.Controllers.API
|
|
|
|
{
|
|
|
|
public class APIInterface
|
|
|
|
{
|
|
|
|
private HttpClient HttpClient { get; set; }
|
|
|
|
|
2021-12-28 21:53:31 +00:00
|
|
|
public async Task<ResponseModel<T>> Get<T>(string Endpoint, Dictionary<string, string> Headers){
|
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
|
|
|
{
|
2021-11-07 21:30:31 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var pendingResult = this.HttpClient.GetAsync(Endpoint);
|
2021-10-31 16:36:14 +00:00
|
|
|
|
2021-11-07 21:30:31 +00:00
|
|
|
var result = await pendingResult;
|
2021-10-31 16:36:14 +00:00
|
|
|
|
2021-11-07 21:30:31 +00:00
|
|
|
string resultString = result.Content.ReadAsStringAsync().Result;
|
2021-10-31 16:36:14 +00:00
|
|
|
|
2021-11-22 13:07:56 +00:00
|
|
|
//Assess return value (object or raw string)
|
2021-12-28 21:53:31 +00:00
|
|
|
object response = resultString;
|
|
|
|
|
|
|
|
if (typeof(T) != typeof(string))
|
|
|
|
{//If the user requests string for return type
|
2021-11-22 13:07:56 +00:00
|
|
|
response = JsonConvert.DeserializeObject<T>(resultString);
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:53:31 +00:00
|
|
|
return new ResponseModel<T>(){
|
|
|
|
//Sender should be aware of type T becuase of Generic type
|
|
|
|
Result = (T)response
|
2021-11-07 21:30:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
catch (System.Exception e)
|
|
|
|
{
|
2021-12-28 21:53:31 +00:00
|
|
|
return new ResponseModel<T>{Error = e.Message};
|
2021-11-07 21:30:31 +00:00
|
|
|
}
|
2021-10-31 16:36:14 +00:00
|
|
|
|
|
|
|
}
|
2021-11-07 21:30:31 +00:00
|
|
|
//Returned in the event No other response has been returned
|
2021-12-28 21:53:31 +00:00
|
|
|
return new ResponseModel<T>{Error = "Invalid Endpoint - Please try again later"};
|
2021-10-31 16:36:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:53:31 +00:00
|
|
|
public async Task<ResponseModel<T>> Post<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body){
|
2021-10-31 16:36:14 +00:00
|
|
|
|
|
|
|
this.HttpClient = new HttpClient();
|
2021-11-07 21:30:31 +00:00
|
|
|
this.HttpClient.DefaultRequestHeaders.Clear();
|
2021-10-31 16:36:14 +00:00
|
|
|
|
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
|
|
|
{
|
2021-11-07 21:30:31 +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;
|
2021-11-22 13:07:56 +00:00
|
|
|
|
|
|
|
object response = resultString;
|
2021-11-07 21:30:31 +00:00
|
|
|
|
2021-11-22 13:07:56 +00:00
|
|
|
if (typeof(T) != typeof(string))
|
|
|
|
{//If the user requests string for return type
|
|
|
|
response = JsonConvert.DeserializeObject<T>(resultString);
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:53:31 +00:00
|
|
|
return new ResponseModel<T>(){
|
|
|
|
//Sender should be aware of type T becuase of Generic type
|
|
|
|
Result = (T)response
|
|
|
|
};
|
|
|
|
}catch(System.Exception e){
|
|
|
|
return new ResponseModel<T>{Error = e.Message};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Returned in the event No other response has been returned
|
|
|
|
return new ResponseModel<T>{Error = "Invalid Endpoint - Please try again later"};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<ResponseModel<T>> Put<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body){
|
|
|
|
|
|
|
|
this.HttpClient = new HttpClient();
|
|
|
|
this.HttpClient.DefaultRequestHeaders.Clear();
|
|
|
|
|
|
|
|
if (Headers != null)
|
|
|
|
{
|
|
|
|
foreach (var Header in Headers)
|
|
|
|
{
|
|
|
|
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Form.FormAuthenticator.ValidateEndpoint(Endpoint))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
object response = resultString;
|
|
|
|
|
|
|
|
if (typeof(T) != typeof(string))
|
|
|
|
{//If the user requests string for return type
|
|
|
|
response = JsonConvert.DeserializeObject<T>(resultString);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ResponseModel<T>(){
|
|
|
|
//Sender should be aware of type T becuase of Generic type
|
|
|
|
Result = (T)response
|
2021-11-07 21:30:31 +00:00
|
|
|
};
|
|
|
|
}catch(System.Exception e){
|
2021-12-28 21:53:31 +00:00
|
|
|
return new ResponseModel<T>{Error = e.Message};
|
2021-11-07 21:30:31 +00:00
|
|
|
}
|
2021-10-31 16:36:14 +00:00
|
|
|
}
|
2021-11-07 21:30:31 +00:00
|
|
|
//Returned in the event No other response has been returned
|
2021-12-28 21:53:31 +00:00
|
|
|
return new ResponseModel<T>{Error = "Invalid Endpoint - Please try again later"};
|
2021-10-31 16:36:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2021-11-07 21:30:31 +00:00
|
|
|
|
2021-10-31 16:36:14 +00:00
|
|
|
}
|