diff --git a/Controllers/API/APIInterface.cs b/Controllers/API/APIInterface.cs index 9cd8528..5e2db81 100644 --- a/Controllers/API/APIInterface.cs +++ b/Controllers/API/APIInterface.cs @@ -12,7 +12,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(); @@ -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(resultString); - }catch { - response = resultString; } - return new ResponseModel{ - //Sender should be aware of type T becuase of Generic function - Result = response + 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}; + 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"}; + 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(); @@ -88,16 +87,58 @@ namespace EFB.Controllers.API response = JsonConvert.DeserializeObject(resultString); } - return new ResponseModel(){ - //Sender should be aware of type T becuase of Generic function - Result = response + 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}; + 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"}; + return new ResponseModel{Error = "Invalid Endpoint - Please try again later"}; + + } + + + + public async Task> Put(string Endpoint, Dictionary 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(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"}; } diff --git a/Controllers/RouteController.cs b/Controllers/RouteController.cs index 78da5c6..acd5164 100644 --- a/Controllers/RouteController.cs +++ b/Controllers/RouteController.cs @@ -72,7 +72,7 @@ namespace EFB.Controllers //Make initial Route Request var requestRoute = API.Post("https://api.autorouter.aero/v1.0/router", headerData, content); - ResponseModel responseRoute = await requestRoute; + ResponseModel 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 Poll(){ if (HttpContext.Session.GetString("User") != null) {//If the user is currently logged in - UserModel User = HttpContext.Session.GetObject("User"); + UserModel user = HttpContext.Session.GetObject("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 headerData = new Dictionary(); + headerData.Add("Authorization", $"Bearer {user.Token.TokenValue}"); + + while (collected == false && count <= 5) + { + count ++; + + //Make Polling Request + var pollingRequest = API.Put>($"https://api.autorouter.aero/v1.0/router/{user.Route.RouteID}/longpoll", headerData, null); + + ResponseModel> responsePoll = await pollingRequest; + + Console.WriteLine(responsePoll); + + } + return RedirectToAction("Index", "Route"); }else{ diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index d961576..07d893c 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -47,7 +47,7 @@ namespace EFB.Controllers var request = API.Post("https://api.autorouter.aero/v1.0/oauth2/token", null, content); //Wait for the response to come through - ResponseModel response = await request; + ResponseModel 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) diff --git a/Models/JSON/PollResponse.cs b/Models/JSON/PollResponse.cs new file mode 100644 index 0000000..5169d10 --- /dev/null +++ b/Models/JSON/PollResponse.cs @@ -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; } + } + +} \ No newline at end of file diff --git a/Models/ResponseModel.cs b/Models/ResponseModel.cs index debfb08..92f2f32 100644 --- a/Models/ResponseModel.cs +++ b/Models/ResponseModel.cs @@ -5,11 +5,10 @@ using System.Threading.Tasks; namespace EFB.Models { - public class ResponseModel + public class ResponseModel { //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; }