diff --git a/AS1024.GeoFeed.Core/Tools/GeoFeedTools.cs b/AS1024.GeoFeed.Core/Tools/GeoFeedTools.cs index ee19c89..981ab8c 100644 --- a/AS1024.GeoFeed.Core/Tools/GeoFeedTools.cs +++ b/AS1024.GeoFeed.Core/Tools/GeoFeedTools.cs @@ -5,10 +5,23 @@ namespace AS1024.GeoFeed.Core.Tools { public static class GeoFeedTools { - public static string ToGeoFeedCsv(this List geoFeeds) + /// + /// Returns a CSV string for a given GeoFeed retreived from various sources + /// + /// GeoFeed returned from the source of truth + /// If a timestamp should be appended at the header + /// If the result is cached + /// + public static string ToGeoFeedCsv(this List geoFeeds, bool timeStamp = false, bool isCached = false) { StringBuilder csvContent = new(); + if (timeStamp) + csvContent.AppendLine($"# GeoFeed generated on {DateTime.UtcNow:R}"); + + if (isCached) + csvContent.AppendLine($"# Geofeed data is returned from local in memory cache"); + foreach (IPGeoFeed feed in geoFeeds) { csvContent.AppendLine($"{feed.Prefix},{feed.GeolocCountry},{feed.GeolocRegion},{feed.GeolocCity},{feed.GeolocPostalCode}"); diff --git a/AS1024.GeoFeed.MinimalAPI/Program.cs b/AS1024.GeoFeed.MinimalAPI/Program.cs index ef0a2b8..4712da5 100644 --- a/AS1024.GeoFeed.MinimalAPI/Program.cs +++ b/AS1024.GeoFeed.MinimalAPI/Program.cs @@ -55,10 +55,12 @@ namespace AS1024.GeoFeed.MinimalAPI IMemoryCache memoryCache, IWebHostEnvironment environment) { + bool isCached = true; try { if (!memoryCache.TryGetValue("Geofeed", out List? feed)) { + isCached = false; feed = await provider.GetGeoFeedData(); if (environment.IsProduction()) { @@ -68,7 +70,7 @@ namespace AS1024.GeoFeed.MinimalAPI } } - return Results.File(Encoding.UTF8.GetBytes(feed.ToGeoFeedCsv()), + return Results.File(Encoding.UTF8.GetBytes(feed.ToGeoFeedCsv(true, isCached)), "text/csv", "geofeed.csv"); diff --git a/AS1024.GeoFeed/Controllers/GeofeedController.cs b/AS1024.GeoFeed/Controllers/GeofeedController.cs index 62c8ace..82ff5eb 100644 --- a/AS1024.GeoFeed/Controllers/GeofeedController.cs +++ b/AS1024.GeoFeed/Controllers/GeofeedController.cs @@ -36,10 +36,12 @@ namespace AS1024.GeoFeed.Controllers [Route("")] public async Task Get() { + bool isCached = true; try { if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List? feed)) { + isCached = false; feed = await builder.GetGeoFeedData(); if (environment.IsProduction()) { @@ -49,7 +51,7 @@ namespace AS1024.GeoFeed.Controllers } } - return ReturnFile(feed); + return ReturnFile(feed, isCached); } catch (HttpRequestException ex) { logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}"); @@ -66,9 +68,9 @@ namespace AS1024.GeoFeed.Controllers } [NonAction] - private IActionResult ReturnFile(List? feed) + private IActionResult ReturnFile(List? feed, bool isCached = false) { - string csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format. + string csvContent = feed.ToGeoFeedCsv(true, isCached); // Assuming ToGeoFeedCsv() returns a string in CSV format. return ReturnFile(csvContent); }