Make the code cleaner to read

This commit is contained in:
Jeff Leung 2024-01-08 23:18:26 -08:00
parent dd643ab8ab
commit 1f3778095c
1 changed files with 32 additions and 30 deletions

View File

@ -31,7 +31,7 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
public async Task<List<IPGeoFeed>> GetGeoFeedData() public async Task<List<IPGeoFeed>> GetGeoFeedData()
{ {
List<IPGeoFeed> geoFeed = new List<IPGeoFeed>(); List<IPGeoFeed> geoFeed = [];
using HttpClient 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"));
@ -46,45 +46,47 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
logger.LogDebug($"Making request to {uri}..."); logger.LogDebug($"Making request to {uri}...");
using HttpResponseMessage result = await client.GetAsync(uri); using HttpResponseMessage result = await client.GetAsync(uri);
if (result.IsSuccessStatusCode) if (!result.IsSuccessStatusCode)
{ {
string stringResult = await result.Content.ReadAsStringAsync(); break;
jsonData = JsonSerializer.Deserialize<NetboxData>(stringResult, new JsonSerializerOptions { }
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower string stringResult = await result.Content.ReadAsStringAsync();
}); jsonData = JsonSerializer.Deserialize<NetboxData>(stringResult, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
});
if (jsonData?.Results == null || jsonData.Results.Count == 0) if (jsonData?.Results == null || jsonData.Results.Count == 0)
{ {
break; break;
} }
foreach (Result data in jsonData.Results) foreach (Result data in jsonData.Results)
{
try
{ {
try geoFeed.Add(new IPGeoFeed
{ {
geoFeed.Add(new IPGeoFeed Prefix = data.Prefix,
{ GeolocCity = data.CustomFields.GeolocCity,
Prefix = data.Prefix, GeolocRegion = data.CustomFields.GeolocRegion,
GeolocCity = data.CustomFields.GeolocCity, GeolocCountry = data.CustomFields.GeolocCountry,
GeolocRegion = data.CustomFields.GeolocRegion, GeolocHasLocation = data.CustomFields.GeolocHasLocation,
GeolocCountry = data.CustomFields.GeolocCountry, GeolocPostalCode = data.CustomFields.GeolocPostalCode
GeolocHasLocation = data.CustomFields.GeolocHasLocation, });
GeolocPostalCode = data.CustomFields.GeolocPostalCode
});
}
catch (Exception ex)
{
logger.LogError(ex.ToString());
}
} }
catch (Exception ex)
if (!string.IsNullOrEmpty(jsonData.Next))
{ {
uri = new Uri(jsonData.Next); logger.LogError(ex.ToString());
continue;
} }
} }
if (!string.IsNullOrEmpty(jsonData.Next))
{
uri = new Uri(jsonData.Next);
continue;
}
break; break;
} }
} }