Updated API Interface
Updated API Interface to incorporate a new response model in order to simplify responses and make error handling easier.
This commit is contained in:
parent
34efdb1cec
commit
d3970fba18
@ -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<T> Get<T>(string Endpoint, Dictionary<string, string> Headers){
|
||||
public async Task<ResponseModel> Get<T>(string Endpoint, Dictionary<string, string> Headers){
|
||||
|
||||
this.HttpClient = new HttpClient();
|
||||
|
||||
@ -25,6 +26,8 @@ namespace EFB.Controllers.API
|
||||
}
|
||||
|
||||
if (Form.FormAuthenticator.ValidateEndpoint(Endpoint))
|
||||
{
|
||||
try
|
||||
{
|
||||
var pendingResult = this.HttpClient.GetAsync(Endpoint);
|
||||
|
||||
@ -32,23 +35,26 @@ namespace EFB.Controllers.API
|
||||
|
||||
string resultString = result.Content.ReadAsStringAsync().Result;
|
||||
|
||||
return JsonConvert.DeserializeObject<T>(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<T>(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<T> Post<T>(string Endpoint, Dictionary<string, string> Headers, HttpContent Body){
|
||||
}
|
||||
|
||||
public async Task<ResponseModel> Post<T>(string Endpoint, Dictionary<string, string> 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))
|
||||
{
|
||||
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 JsonConvert.DeserializeObject<T>(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<T>(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"};
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
16
Models/ResponseModel.cs
Normal file
16
Models/ResponseModel.cs
Normal file
@ -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;
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user