Consolidated and added comments to the API interface class

This commit is contained in:
Luke Else 2022-02-28 21:25:00 +00:00
parent e783f8c614
commit b1e8bfe8d0

View File

@ -13,9 +13,8 @@ namespace EFB.Controllers.API
private HttpClient HttpClient { get; set; }
public async Task<ResponseModel<T>> Get<T>(string Endpoint, Dictionary<string, string> Headers){
//Create the HTTP client used for the GetRequest.
this.HttpClient = new HttpClient();
this.HttpClient.DefaultRequestHeaders.Clear();
if (Headers != null)
@ -30,10 +29,9 @@ namespace EFB.Controllers.API
{
try
{
//Get the response from the server
var pendingResult = this.HttpClient.GetAsync(Endpoint);
var result = await pendingResult;
string resultString = result.Content.ReadAsStringAsync().Result;
//Assess return value (object or raw string)
@ -43,7 +41,6 @@ namespace EFB.Controllers.API
{//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
@ -53,20 +50,18 @@ namespace EFB.Controllers.API
{
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>> Post<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body){
//Create a HTTP client to allow for making Post requests
this.HttpClient = new HttpClient();
this.HttpClient.DefaultRequestHeaders.Clear();
if (Headers != null)
{
{//Go through and add each header to the HTTP Client
foreach (var Header in Headers)
{
this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
@ -79,7 +74,6 @@ namespace EFB.Controllers.API
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))
@ -97,18 +91,14 @@ namespace EFB.Controllers.API
}
//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){
//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);
@ -122,6 +112,7 @@ namespace EFB.Controllers.API
var result = await pendingResult;
string resultString = result.Content.ReadAsStringAsync().Result;
//Recieve the response as a string which will be morphed in other types
object response = resultString;
if (typeof(T) != typeof(string))
@ -139,9 +130,7 @@ namespace EFB.Controllers.API
}
//Returned in the event No other response has been returned
return new ResponseModel<T>{Error = "Invalid Endpoint - Please try again later"};
}
}
}