Move towards explicit types

This commit is contained in:
2024-01-05 15:22:16 -08:00
parent db05b0b815
commit 5b367121c4
4 changed files with 15 additions and 15 deletions

View File

@@ -34,7 +34,7 @@ namespace AS1024.GeoFeed.Controllers
&& environment.IsProduction()) && environment.IsProduction())
{ {
feed = await builder.GetGeoFeedData(); feed = await builder.GetGeoFeedData();
var cacheEntryOptions = new MemoryCacheEntryOptions() MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromMinutes(15)); .SetSlidingExpiration(TimeSpan.FromMinutes(15));
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions); memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
} else } else
@@ -42,9 +42,9 @@ namespace AS1024.GeoFeed.Controllers
feed = await builder.GetGeoFeedData(); feed = await builder.GetGeoFeedData();
} }
var csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format. string csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format.
var contentBytes = Encoding.UTF8.GetBytes(csvContent); byte[] contentBytes = Encoding.UTF8.GetBytes(csvContent);
var contentType = "text/csv"; string contentType = "text/csv";
return new FileContentResult(contentBytes, contentType) return new FileContentResult(contentBytes, contentType)
{ {

View File

@@ -9,7 +9,7 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
{ {
StringBuilder csvContent = new(); StringBuilder csvContent = new();
foreach (var feed in geoFeeds) foreach (IPGeoFeed feed in geoFeeds)
{ {
csvContent.AppendLine($"{feed.Prefix},{feed.GeolocCountry},{feed.GeolocRegion},{feed.GeolocCity},"); csvContent.AppendLine($"{feed.Prefix},{feed.GeolocCountry},{feed.GeolocRegion},{feed.GeolocCity},");
} }

View File

@@ -31,12 +31,12 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
public async Task<List<IPGeoFeed>> GetGeoFeedData() public async Task<List<IPGeoFeed>> GetGeoFeedData()
{ {
var geoFeed = new List<IPGeoFeed>(); List<IPGeoFeed> geoFeed = new List<IPGeoFeed>();
using var client = httpClientFactory.CreateClient(); using HttpClient client = httpClientFactory.CreateClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", configuration["APIKey"]); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", configuration["APIKey"]);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
foreach (var family in addressFamilies) foreach (AddressFamily family in addressFamilies)
{ {
Uri uri = BuildNetBoxURI(family); Uri uri = BuildNetBoxURI(family);
NetboxData? jsonData = null; NetboxData? jsonData = null;
@@ -45,10 +45,10 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
{ {
logger.LogDebug($"Making request to {uri}..."); logger.LogDebug($"Making request to {uri}...");
using var result = await client.GetAsync(uri); using HttpResponseMessage result = await client.GetAsync(uri);
if (result.IsSuccessStatusCode) if (result.IsSuccessStatusCode)
{ {
var stringResult = await result.Content.ReadAsStringAsync(); string stringResult = await result.Content.ReadAsStringAsync();
jsonData = JsonConvert.DeserializeObject<NetboxData>(stringResult); jsonData = JsonConvert.DeserializeObject<NetboxData>(stringResult);
if (jsonData?.Results == null || jsonData.Results.Count == 0) if (jsonData?.Results == null || jsonData.Results.Count == 0)
@@ -56,7 +56,7 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
break; break;
} }
foreach (var data in jsonData.Results) foreach (Result data in jsonData.Results)
{ {
try try
{ {
@@ -92,7 +92,7 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
protected Uri BuildNetBoxURI(AddressFamily family) protected Uri BuildNetBoxURI(AddressFamily family)
{ {
var queryParameters = HttpUtility.ParseQueryString(string.Empty); System.Collections.Specialized.NameValueCollection queryParameters = HttpUtility.ParseQueryString(string.Empty);
queryParameters["cf_geoloc_has_location"] = "true"; queryParameters["cf_geoloc_has_location"] = "true";
queryParameters["limit"] = "50"; queryParameters["limit"] = "50";
@@ -109,7 +109,7 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
break; break;
} }
var endUrl = new UriBuilder UriBuilder endUrl = new UriBuilder
{ {
Path = "api/ipam/prefixes/", Path = "api/ipam/prefixes/",
Query = queryParameters.ToString(), Query = queryParameters.ToString(),

View File

@@ -23,8 +23,8 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
async Task IHostedService.StartAsync(CancellationToken cancellationToken) async Task IHostedService.StartAsync(CancellationToken cancellationToken)
{ {
logger.LogInformation("Preloading GeoFeed data in memory..."); logger.LogInformation("Preloading GeoFeed data in memory...");
var feed = await provider.GetGeoFeedData(); List<Models.IPGeoFeed> feed = await provider.GetGeoFeedData();
var cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(45)); MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(45));
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions); memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
} }