public CacheManager(IMemoryCache cache, ILogger <CacheManager> logger, RootDb rootDb, GuernseyDb guernseyDb) { _memoryCache = cache; _logger = logger; _rootDb = rootDb; _guernseyDb = guernseyDb; DataCategories = new CachableThing <DataCategoryDto[]>(nameof(DataCategories), _memoryCache, _rootDb.GetData); DataCache = new CachableThing <DataCache>(nameof(DataCache), _memoryCache, _guernseyDb.GetDataCache); LiveDataCache = new CachableThing <LiveDataCache>(nameof(LiveDataCache), _memoryCache, async() => { // TODO: Figure out good way to use the injected client using var client = new HttpClient(); var airportScraper = new AirportScraper(client); var airportScraperTask = airportScraper.Get(); var harbourScraper = new HarbourScraper(client); var harbourScraperTask = harbourScraper.Get(); var airportLatest = await airportScraperTask; var harbourLatest = await harbourScraperTask; return(new LiveDataCache { AirportArrivals = airportLatest.Arrivals, AirportDepartures = airportLatest.Departures, Harbour = harbourLatest, }); });
/// <summary> /// An Asynchronous method that uses an instance of HttpClient to fetch Information from the API /// Saves the content According to the class created in Root Class and Datum class /// Deserializes the Content using JsonSerializer and store the information into a LIst. /// </summary> /// <param name="url"></param> async static void GetResponse(string url) { HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(url); HttpContent content = response.Content; string mycontent = await content.ReadAsStringAsync(); RootDb myDeserializedClass = JsonSerializer.Deserialize <RootDb>(mycontent); foreach (var item in myDeserializedClass.data) { AuthorInfo.Details.Add(item); } }