From 76878e7745f44b769922e3dc61dc54132160ac24 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Fri, 11 Feb 2022 17:00:08 +0000 Subject: [PATCH] Updated Chart Fetch functions in order to create a more generic template --- Controllers/RouteController.cs | 26 +++++++---------------- Models/ChartModel.cs | 38 +++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/Controllers/RouteController.cs b/Controllers/RouteController.cs index 602be30..632186c 100644 --- a/Controllers/RouteController.cs +++ b/Controllers/RouteController.cs @@ -127,18 +127,8 @@ namespace EFB.Controllers /*-----Chart Fetching Operations--------*/ - //Prepare data to be send off with request(charts) - headerData = new Dictionary(); - headerData.Add("referer", "luke-else.co.uk"); - - Dictionary formData = new Dictionary(); - - formData.Add("token", Environment.GetEnvironmentVariable("ChartFoxAPIKey", EnvironmentVariableTarget.User)); - var body = new FormUrlEncodedContent(formData); - - //make Charts request - var requestDepartureCharts = API.Post($"https://chartfox.org/api/charts/grouped/{departure}", headerData, body); - var requestArrivalCharts = API.Post($"https://chartfox.org/api/charts/grouped/{arrival}", headerData, body); + var requestDepartureCharts = ChartModel.FetchAsync(departure); + var requestArrivalCharts = ChartModel.FetchAsync(arrival); /*----------------------------------*/ @@ -170,16 +160,16 @@ namespace EFB.Controllers if (collected) { //Get Response from Charts - ResponseModel responseDepartureCharts = await requestDepartureCharts; - if (responseDepartureCharts.Error == null && responseDepartureCharts.Result.Status == "success") + ChartList departureCharts = await requestDepartureCharts; + if (departureCharts != null) { - user.DepartureCharts = new ChartModel(responseDepartureCharts.Result.Response); + user.DepartureCharts = new ChartModel(departureCharts); } - ResponseModel responseArrivalCharts = await requestArrivalCharts; - if (responseArrivalCharts.Error == null && responseArrivalCharts.Result.Status == "success") + ChartList arrivalCharts = await requestArrivalCharts; + if (arrivalCharts != null) { - user.ArrivalCharts = new ChartModel(responseArrivalCharts.Result.Response); + user.ArrivalCharts = new ChartModel(arrivalCharts); } //fill in route diff --git a/Models/ChartModel.cs b/Models/ChartModel.cs index 8b2ffba..c6313c7 100644 --- a/Models/ChartModel.cs +++ b/Models/ChartModel.cs @@ -1,9 +1,18 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; -using EFB.Models.JSON; +using System.Threading; +using System.Collections.Generic; +using System.Text; +using System.Net.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Http; using Newtonsoft.Json; +using EFB.Models; +using EFB.Models.JSON; +using EFB.Sessions; +using EFB.Controllers.Form; +using EFB.Controllers.API; namespace EFB.Models { @@ -41,6 +50,29 @@ namespace EFB.Models } return new Chart[]{}; } + + public static async Task FetchAsync(string ICAO){ + Console.WriteLine("Start"); + if (FormAuthenticator.ValidateICAOCode(ICAO)) + { + APIInterface API = new APIInterface(); + Dictionary headerData = new Dictionary(); + headerData.Add("referer", "luke-else.co.uk"); + + Dictionary formData = new Dictionary(); + + formData.Add("token", Environment.GetEnvironmentVariable("ChartFoxAPIKey", EnvironmentVariableTarget.User)); + FormUrlEncodedContent body = new FormUrlEncodedContent(formData); + + //make Charts request + var requestCharts = await API.Post($"https://chartfox.org/api/charts/grouped/{ICAO}", headerData, body); + Console.WriteLine("End"); + return requestCharts.Result.Response; + }else + { + return null; + } + } } public class Pinned