Code cleanup

This commit is contained in:
2022-02-28 21:53:28 +00:00
parent b1e8bfe8d0
commit 72124e7f15
31 changed files with 250 additions and 312 deletions

View File

@ -1,10 +1,8 @@
using System.Collections.Generic;
using System.Text;
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Net.Http;
using EFB.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace EFB.Controllers.API
{
@ -12,7 +10,8 @@ namespace EFB.Controllers.API
{
private HttpClient HttpClient { get; set; }
public async Task<ResponseModel<T>> Get<T>(string Endpoint, Dictionary<string, string> Headers){
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();
@ -41,21 +40,23 @@ namespace EFB.Controllers.API
{//If the user requests string for return type
response = JsonConvert.DeserializeObject<T>(resultString);
}
return new ResponseModel<T>(){
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};
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"};
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){
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();
@ -70,7 +71,8 @@ namespace EFB.Controllers.API
if (Form.FormAuthenticator.ValidateEndpoint(Endpoint))
{
try{//Try statement to catch errors in the process of making the request
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;
@ -81,18 +83,22 @@ namespace EFB.Controllers.API
response = JsonConvert.DeserializeObject<T>(resultString);
}
return new ResponseModel<T>(){
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};
}
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"};
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){
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();
@ -107,11 +113,12 @@ namespace EFB.Controllers.API
if (Form.FormAuthenticator.ValidateEndpoint(Endpoint))
{
try{//Try statement to catch errors in the process of making the request
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;
//Recieve the response as a string which will be morphed in other types
object response = resultString;
@ -120,18 +127,21 @@ namespace EFB.Controllers.API
response = JsonConvert.DeserializeObject<T>(resultString);
}
return new ResponseModel<T>(){
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};
}
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"};
}
return new ResponseModel<T> { Error = "Invalid Endpoint - Please try again later" };
}
}
}