From 1f3778095c017687abcbc8b1daf2924562118e72 Mon Sep 17 00:00:00 2001 From: Jeff Leung Date: Mon, 8 Jan 2024 23:18:26 -0800 Subject: [PATCH] Make the code cleaner to read --- .../GeoFeedBuilder/NetBoxGeoFeedProvider.cs | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/AS1024.GeoFeed/GeoFeedBuilder/NetBoxGeoFeedProvider.cs b/AS1024.GeoFeed/GeoFeedBuilder/NetBoxGeoFeedProvider.cs index 7f2aecc..607c36a 100644 --- a/AS1024.GeoFeed/GeoFeedBuilder/NetBoxGeoFeedProvider.cs +++ b/AS1024.GeoFeed/GeoFeedBuilder/NetBoxGeoFeedProvider.cs @@ -31,7 +31,7 @@ namespace AS1024.GeoFeed.GeoFeedBuilder public async Task> GetGeoFeedData() { - List geoFeed = new List(); + List geoFeed = []; using HttpClient client = httpClientFactory.CreateClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", configuration["APIKey"]); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); @@ -46,45 +46,47 @@ namespace AS1024.GeoFeed.GeoFeedBuilder logger.LogDebug($"Making request to {uri}..."); using HttpResponseMessage result = await client.GetAsync(uri); - if (result.IsSuccessStatusCode) + if (!result.IsSuccessStatusCode) { - string stringResult = await result.Content.ReadAsStringAsync(); - jsonData = JsonSerializer.Deserialize(stringResult, new JsonSerializerOptions { - PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower - }); + break; + } + string stringResult = await result.Content.ReadAsStringAsync(); + jsonData = JsonSerializer.Deserialize(stringResult, new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower + }); - if (jsonData?.Results == null || jsonData.Results.Count == 0) - { - break; - } + if (jsonData?.Results == null || jsonData.Results.Count == 0) + { + 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, - GeolocRegion = data.CustomFields.GeolocRegion, - GeolocCountry = data.CustomFields.GeolocCountry, - GeolocHasLocation = data.CustomFields.GeolocHasLocation, - GeolocPostalCode = data.CustomFields.GeolocPostalCode - }); - } - catch (Exception ex) - { - logger.LogError(ex.ToString()); - } + Prefix = data.Prefix, + GeolocCity = data.CustomFields.GeolocCity, + GeolocRegion = data.CustomFields.GeolocRegion, + GeolocCountry = data.CustomFields.GeolocCountry, + GeolocHasLocation = data.CustomFields.GeolocHasLocation, + GeolocPostalCode = data.CustomFields.GeolocPostalCode + }); } - - if (!string.IsNullOrEmpty(jsonData.Next)) + catch (Exception ex) { - uri = new Uri(jsonData.Next); - continue; + logger.LogError(ex.ToString()); } } + if (!string.IsNullOrEmpty(jsonData.Next)) + { + uri = new Uri(jsonData.Next); + continue; + } + break; } }