From ab2e0b2bd2bc8e34723ee897a0329a3d367e0dbc Mon Sep 17 00:00:00 2001 From: James Montemagno Date: Fri, 14 Jun 2024 11:47:52 -0700 Subject: [PATCH] Refactor projects and update JSON handling - Removed Linux-specific and debugging settings in Api.csproj, indicating a shift in deployment and development strategies. - Eliminated UserSecretsId and Docker configurations, suggesting changes in secrets management and containerization approach. - Removed OpenApiReference and Newtonsoft.Json references in MyWeatherHub.csproj, transitioning to System.Text.Json for JSON operations. - Added NwsManager class with async methods for weather data fetching, aligning with the new JSON strategy. - Introduced new data types `Zone` and `Forecast` for handling deserialized data, supporting the updated functionality in weather data processing. --- complete/Api/Api.csproj | 9 ------ complete/MyWeatherHub/MyWeatherHub.csproj | 10 ------- complete/MyWeatherHub/NwsManager.cs | 36 +++++++++++++++++++++++ start-with-api/Api/Api.csproj | 9 ------ 4 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 complete/MyWeatherHub/NwsManager.cs diff --git a/complete/Api/Api.csproj b/complete/Api/Api.csproj index 49a7446..ab6cf95 100644 --- a/complete/Api/Api.csproj +++ b/complete/Api/Api.csproj @@ -4,10 +4,6 @@ net8.0 enable enable - linux-x64 - True - 7b8931a0-bda7-4546-b99d-dc41f97af10e - Linux @@ -16,11 +12,6 @@ - - - 8081 - - diff --git a/complete/MyWeatherHub/MyWeatherHub.csproj b/complete/MyWeatherHub/MyWeatherHub.csproj index 5c5f8eb..350bdc3 100644 --- a/complete/MyWeatherHub/MyWeatherHub.csproj +++ b/complete/MyWeatherHub/MyWeatherHub.csproj @@ -6,11 +6,6 @@ enable - - - https://localhost:7032/swagger/v1/swagger.json - - @@ -18,11 +13,6 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/complete/MyWeatherHub/NwsManager.cs b/complete/MyWeatherHub/NwsManager.cs new file mode 100644 index 0000000..05fc894 --- /dev/null +++ b/complete/MyWeatherHub/NwsManager.cs @@ -0,0 +1,36 @@ +using System.Text.Json; + +namespace MyWeatherHub; + +public class NwsManager(HttpClient client) +{ + readonly JsonSerializerOptions options = new() + { + PropertyNameCaseInsensitive = true + }; + + public async Task GetZonesAsync() + { + var response = await client.GetAsync("zones"); + response.EnsureSuccessStatusCode(); + + var content = await response.Content.ReadAsStringAsync(); + var zones = JsonSerializer.Deserialize(content, options); + + return zones ?? []; + } + + public async Task GetForecastByZoneAsync(string zoneId) + { + var response = await client.GetAsync($"forecast/{zoneId}"); + response.EnsureSuccessStatusCode(); + var content = await response.Content.ReadAsStringAsync(); + var forecast = JsonSerializer.Deserialize(content, options); + + return forecast ?? []; + } +} + +public record Zone(string Key, string Name, string State); + +public record Forecast(string Name, string DetailedForecast); \ No newline at end of file diff --git a/start-with-api/Api/Api.csproj b/start-with-api/Api/Api.csproj index 9147f34..5459f9e 100644 --- a/start-with-api/Api/Api.csproj +++ b/start-with-api/Api/Api.csproj @@ -4,10 +4,6 @@ net8.0 enable enable - linux-x64 - True - 7b8931a0-bda7-4546-b99d-dc41f97af10e - Linux @@ -15,10 +11,5 @@ - - - 8081 - -