From d3970fba18c2ad090a5a38383370e5f8904064b9 Mon Sep 17 00:00:00 2001 From: luke-else <52086083+luke-else@users.noreply.github.com> Date: Sun, 7 Nov 2021 21:30:31 +0000 Subject: [PATCH] Updated API Interface Updated API Interface to incorporate a new response model in order to simplify responses and make error handling easier. --- Controllers/API/APIInterface.cs | 62 ++++++++++++++++++--------------- Models/ResponseModel.cs | 16 +++++++++ 2 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 Models/ResponseModel.cs diff --git a/Controllers/API/APIInterface.cs b/Controllers/API/APIInterface.cs index 9977091..f15712b 100644 --- a/Controllers/API/APIInterface.cs +++ b/Controllers/API/APIInterface.cs @@ -3,6 +3,7 @@ using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using System.Net.Http; +using EFB.Models; namespace EFB.Controllers.API { @@ -10,7 +11,7 @@ namespace EFB.Controllers.API { private HttpClient HttpClient { get; set; } - public async Task Get(string Endpoint, Dictionary Headers){ + public async Task Get(string Endpoint, Dictionary Headers){ this.HttpClient = new HttpClient(); @@ -26,29 +27,34 @@ namespace EFB.Controllers.API if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { - var pendingResult = this.HttpClient.GetAsync(Endpoint); + try + { + var pendingResult = this.HttpClient.GetAsync(Endpoint); - var result = await pendingResult; + var result = await pendingResult; - string resultString = result.Content.ReadAsStringAsync().Result; + string resultString = result.Content.ReadAsStringAsync().Result; - return JsonConvert.DeserializeObject(resultString); - - }else{ - - T empty = default(T); - - return empty; + return new ResponseModel{ + //Sender should be aware of type T becuase of Generic function + Result = JsonConvert.DeserializeObject(resultString) + }; + } + 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){ + public async Task Post(string Endpoint, Dictionary Headers, HttpContent Body){ this.HttpClient = new HttpClient(); - - //this.HttpClient.DefaultRequestHeaders.Clear(); + this.HttpClient.DefaultRequestHeaders.Clear(); if (Headers != null) { @@ -60,25 +66,25 @@ namespace EFB.Controllers.API if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { - var pendingResult = this.HttpClient.PostAsync(Endpoint, Body); - - var result = await pendingResult; - - string resultString = result.Content.ReadAsStringAsync().Result; - - return JsonConvert.DeserializeObject(resultString); - - }else{ - - T empty = default(T); - - return empty; + 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; + return new ResponseModel{ + //Sender should be aware of type T becuase of Generic function + Result = JsonConvert.DeserializeObject(resultString) + }; + }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"}; } } + } \ No newline at end of file diff --git a/Models/ResponseModel.cs b/Models/ResponseModel.cs new file mode 100644 index 0000000..debfb08 --- /dev/null +++ b/Models/ResponseModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EFB.Models +{ + public class ResponseModel + { + //Object should be of known type from sender + public object Result { get; set; } = null; + + public string Error { get; set; } = null; + + } +} \ No newline at end of file