Compare commits
2 Commits
690a117ffd
...
7721dfb669
| Author | SHA1 | Date |
|---|---|---|
|
|
7721dfb669 | |
|
|
c1ed05a335 |
|
|
@ -58,7 +58,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
|||
List<IPGeoFeed> cachedData = [];
|
||||
results.ForEach(cachedData.Add);
|
||||
|
||||
return cachedData.ToGeoFeedCsv();
|
||||
return cachedData.ToGeoFeedCsv(true, true);
|
||||
}
|
||||
|
||||
private async Task DBContextMigrate()
|
||||
|
|
|
|||
|
|
@ -5,10 +5,23 @@ namespace AS1024.GeoFeed.Core.Tools
|
|||
{
|
||||
public static class GeoFeedTools
|
||||
{
|
||||
public static string ToGeoFeedCsv(this List<IPGeoFeed> geoFeeds)
|
||||
/// <summary>
|
||||
/// Returns a CSV string for a given GeoFeed retreived from various sources
|
||||
/// </summary>
|
||||
/// <param name="geoFeeds">GeoFeed returned from the source of truth</param>
|
||||
/// <param name="timeStamp">If a timestamp should be appended at the header</param>
|
||||
/// <param name="isCached">If the result is cached</param>
|
||||
/// <returns></returns>
|
||||
public static string ToGeoFeedCsv(this List<IPGeoFeed> 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}");
|
||||
|
|
|
|||
|
|
@ -55,10 +55,12 @@ namespace AS1024.GeoFeed.MinimalAPI
|
|||
IMemoryCache memoryCache,
|
||||
IWebHostEnvironment environment)
|
||||
{
|
||||
bool isCached = true;
|
||||
try
|
||||
{
|
||||
if (!memoryCache.TryGetValue("Geofeed", out List<IPGeoFeed>? 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");
|
||||
|
||||
|
|
|
|||
|
|
@ -36,10 +36,12 @@ namespace AS1024.GeoFeed.Controllers
|
|||
[Route("")]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
bool isCached = true;
|
||||
try
|
||||
{
|
||||
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? 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<IPGeoFeed>? feed)
|
||||
private IActionResult ReturnFile(List<IPGeoFeed>? 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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue