Updated Chart Fetch functions in order to create a more generic template

This commit is contained in:
Luke Else 2022-02-11 17:00:08 +00:00
parent 35a6686cd5
commit 76878e7745
2 changed files with 43 additions and 21 deletions

View File

@ -127,18 +127,8 @@ namespace EFB.Controllers
/*-----Chart Fetching Operations--------*/ /*-----Chart Fetching Operations--------*/
//Prepare data to be send off with request(charts) var requestDepartureCharts = ChartModel.FetchAsync(departure);
headerData = new Dictionary<string, string>(); var requestArrivalCharts = ChartModel.FetchAsync(arrival);
headerData.Add("referer", "luke-else.co.uk");
Dictionary<string, string> formData = new Dictionary<string, string>();
formData.Add("token", Environment.GetEnvironmentVariable("ChartFoxAPIKey", EnvironmentVariableTarget.User));
var body = new FormUrlEncodedContent(formData);
//make Charts request
var requestDepartureCharts = API.Post<ChartResponse>($"https://chartfox.org/api/charts/grouped/{departure}", headerData, body);
var requestArrivalCharts = API.Post<ChartResponse>($"https://chartfox.org/api/charts/grouped/{arrival}", headerData, body);
/*----------------------------------*/ /*----------------------------------*/
@ -170,16 +160,16 @@ namespace EFB.Controllers
if (collected) if (collected)
{ {
//Get Response from Charts //Get Response from Charts
ResponseModel<ChartResponse> responseDepartureCharts = await requestDepartureCharts; ChartList departureCharts = await requestDepartureCharts;
if (responseDepartureCharts.Error == null && responseDepartureCharts.Result.Status == "success") if (departureCharts != null)
{ {
user.DepartureCharts = new ChartModel(responseDepartureCharts.Result.Response); user.DepartureCharts = new ChartModel(departureCharts);
} }
ResponseModel<ChartResponse> responseArrivalCharts = await requestArrivalCharts; ChartList arrivalCharts = await requestArrivalCharts;
if (responseArrivalCharts.Error == null && responseArrivalCharts.Result.Status == "success") if (arrivalCharts != null)
{ {
user.ArrivalCharts = new ChartModel(responseArrivalCharts.Result.Response); user.ArrivalCharts = new ChartModel(arrivalCharts);
} }
//fill in route //fill in route

View File

@ -1,9 +1,18 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; 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 Newtonsoft.Json;
using EFB.Models;
using EFB.Models.JSON;
using EFB.Sessions;
using EFB.Controllers.Form;
using EFB.Controllers.API;
namespace EFB.Models namespace EFB.Models
{ {
@ -41,6 +50,29 @@ namespace EFB.Models
} }
return new Chart[]{}; return new Chart[]{};
} }
public static async Task<ChartList> FetchAsync(string ICAO){
Console.WriteLine("Start");
if (FormAuthenticator.ValidateICAOCode(ICAO))
{
APIInterface API = new APIInterface();
Dictionary <string, string> headerData = new Dictionary<string, string>();
headerData.Add("referer", "luke-else.co.uk");
Dictionary<string, string> formData = new Dictionary<string, string>();
formData.Add("token", Environment.GetEnvironmentVariable("ChartFoxAPIKey", EnvironmentVariableTarget.User));
FormUrlEncodedContent body = new FormUrlEncodedContent(formData);
//make Charts request
var requestCharts = await API.Post<ChartResponse>($"https://chartfox.org/api/charts/grouped/{ICAO}", headerData, body);
Console.WriteLine("End");
return requestCharts.Result.Response;
}else
{
return null;
}
}
} }
public class Pinned public class Pinned