Added Poll response function

JSON is not properly serialised... still in progress
This commit is contained in:
Luke Else 2021-12-28 21:53:31 +00:00
parent e134751745
commit 5a2f109e65
5 changed files with 107 additions and 25 deletions

View File

@ -12,7 +12,7 @@ namespace EFB.Controllers.API
{
private HttpClient HttpClient { get; set; }
public async Task<ResponseModel> Get<T>(string Endpoint, Dictionary<string, string> Headers){
public async Task<ResponseModel<T>> Get<T>(string Endpoint, Dictionary<string, string> Headers){
this.HttpClient = new HttpClient();
@ -36,32 +36,31 @@ namespace EFB.Controllers.API
string resultString = result.Content.ReadAsStringAsync().Result;
object response;
//Assess return value (object or raw string)
try{
object response = resultString;
if (typeof(T) != typeof(string))
{//If the user requests string for return type
response = JsonConvert.DeserializeObject<T>(resultString);
}catch {
response = resultString;
}
return new ResponseModel{
//Sender should be aware of type T becuase of Generic function
Result = response
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{Error = e.Message};
return new ResponseModel<T>{Error = e.Message};
}
}
//Returned in the event No other response has been returned
return new ResponseModel{Error = "Invalid Endpoint - Please try again later"};
return new ResponseModel<T>{Error = "Invalid Endpoint - Please try again later"};
}
public async Task<ResponseModel> 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){
this.HttpClient = new HttpClient();
this.HttpClient.DefaultRequestHeaders.Clear();
@ -88,16 +87,58 @@ namespace EFB.Controllers.API
response = JsonConvert.DeserializeObject<T>(resultString);
}
return new ResponseModel(){
//Sender should be aware of type T becuase of Generic function
Result = response
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{Error = e.Message};
return new ResponseModel<T>{Error = e.Message};
}
}
//Returned in the event No other response has been returned
return new ResponseModel{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){
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{//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;
object response = resultString;
if (typeof(T) != typeof(string))
{//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
};
}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"};
}

View File

@ -72,7 +72,7 @@ namespace EFB.Controllers
//Make initial Route Request
var requestRoute = API.Post<string>("https://api.autorouter.aero/v1.0/router", headerData, content);
ResponseModel responseRoute = await requestRoute;
ResponseModel<string> responseRoute = await requestRoute;
if (responseRoute.Error == null)
{//Update User session and add route ID
@ -83,6 +83,8 @@ namespace EFB.Controllers
user.Route = route;
HttpContext.Session.SetObject("User", user);
return await Poll();
}
TempData["Error"] = responseRoute.Error;
@ -105,12 +107,33 @@ namespace EFB.Controllers
public async Task<IActionResult> Poll(){
if (HttpContext.Session.GetString("User") != null)
{//If the user is currently logged in
UserModel User = HttpContext.Session.GetObject<UserModel>("User");
UserModel user = HttpContext.Session.GetObject<UserModel>("User");
if (User.Route != null)
if (user.Route != null)
{//If the user has a route object (e.g, they have been to the route page)
//Make calls to the server to fetch route
bool collected = false;
int count = 0;
APIInterface API = new APIInterface();
Dictionary<string, string> headerData = new Dictionary<string, string>();
headerData.Add("Authorization", $"Bearer {user.Token.TokenValue}");
while (collected == false && count <= 5)
{
count ++;
//Make Polling Request
var pollingRequest = API.Put<List<PollResponse>>($"https://api.autorouter.aero/v1.0/router/{user.Route.RouteID}/longpoll", headerData, null);
ResponseModel<List<PollResponse>> responsePoll = await pollingRequest;
Console.WriteLine(responsePoll);
}
return RedirectToAction("Index", "Route");
}else{

View File

@ -47,7 +47,7 @@ namespace EFB.Controllers
var request = API.Post<Models.JSON.LoginResponse>("https://api.autorouter.aero/v1.0/oauth2/token", null, content);
//Wait for the response to come through
ResponseModel response = await request;
ResponseModel<LoginResponse> response = await request;
if (response.Error != null)
{
@ -57,7 +57,7 @@ namespace EFB.Controllers
}else{
//Type cast required but we know response will be of known type
LoginResponse login = (LoginResponse)response.Result;
LoginResponse login = response.Result;
//Generate User Session
if (login.error == null)

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace EFB.Models.JSON
{
[JsonArray]
public class PollResponse
{
[JsonProperty(PropertyName = "cmdname")]
public string Command { get; set; }
[JsonProperty(PropertyName = "fpl")]
public string FlightPlan { get; set; }
}
}

View File

@ -5,11 +5,10 @@ using System.Threading.Tasks;
namespace EFB.Models
{
public class ResponseModel
public class ResponseModel<T>
{
//Object should be of known type from sender
public object Result { get; set; } = null;
public T Result { get; set; } = default(T);
public string Error { get; set; } = null;
}