using EFB.Models; using Newtonsoft.Json; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; namespace EFB.Controllers.API { public class APIInterface { private HttpClient HttpClient { get; set; } public async Task> Get(string Endpoint, Dictionary Headers) { //Create the HTTP client used for the GetRequest. 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 { //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) object response = resultString; if (typeof(T) != typeof(string)) {//If the user requests string for return type response = JsonConvert.DeserializeObject(resultString); } return new ResponseModel() { //Sender should be aware of type T becuase of Generic type Result = (T)response }; } catch (System.Exception e) { return new ResponseModel { Error = e.Message }; } } //Returned in the event No other response has been returned return new ResponseModel { Error = "Invalid Endpoint - Please try again later" }; } public async Task> Post(string Endpoint, Dictionary 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); } } if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { 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(resultString); } return new ResponseModel() { //Sender should be aware of type T becuase of Generic type Result = (T)response }; } catch (System.Exception e) { return new ResponseModel { Error = e.Message }; } } //Returned in the event No other response has been returned return new ResponseModel { Error = "Invalid Endpoint - Please try again later" }; } public async Task> Put(string Endpoint, Dictionary 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)) { 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; if (typeof(T) != typeof(string)) {//If the user requests string for return type response = JsonConvert.DeserializeObject(resultString); } return new ResponseModel() { //Sender should be aware of type T becuase of Generic type Result = (T)response }; } catch (System.Exception e) { return new ResponseModel { Error = e.Message }; } } //Returned in the event No other response has been returned return new ResponseModel { Error = "Invalid Endpoint - Please try again later" }; } } }