From 8bd9ee81c0135ca415516c41aa217642c01f57a4 Mon Sep 17 00:00:00 2001 From: Esteban Solano Granados Date: Mon, 7 Apr 2025 23:39:10 -0600 Subject: [PATCH] Fix api.weather.gov/points response handling in WeatherTools (#134) --- samples/QuickstartWeatherServer/Tools/WeatherTools.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/samples/QuickstartWeatherServer/Tools/WeatherTools.cs b/samples/QuickstartWeatherServer/Tools/WeatherTools.cs index 9f04ff2..5554895 100644 --- a/samples/QuickstartWeatherServer/Tools/WeatherTools.cs +++ b/samples/QuickstartWeatherServer/Tools/WeatherTools.cs @@ -42,8 +42,11 @@ public sealed class WeatherTools [Description("Longitude of the location.")] double longitude) { using var jsonDocument = await client.ReadJsonDocumentAsync($"/points/{latitude},{longitude}"); - var jsonElement = jsonDocument.RootElement; - var periods = jsonElement.GetProperty("properties").GetProperty("periods").EnumerateArray(); + var forecastUrl = jsonDocument.RootElement.GetProperty("properties").GetProperty("forecast").GetString() + ?? throw new Exception($"No forecast URL provided by {client.BaseAddress}points/{latitude},{longitude}"); + + using var forecastDocument = await client.ReadJsonDocumentAsync(forecastUrl); + var periods = forecastDocument.RootElement.GetProperty("properties").GetProperty("periods").EnumerateArray(); return string.Join("\n---\n", periods.Select(period => $""" {period.GetProperty("name").GetString()} @@ -52,4 +55,4 @@ public sealed class WeatherTools Forecast: {period.GetProperty("detailedForecast").GetString()} """)); } -} \ No newline at end of file +}