Skip to content

Commit 8bd9ee8

Browse files
authored
Fix api.weather.gov/points response handling in WeatherTools (#134)
1 parent 3b83ad4 commit 8bd9ee8

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

samples/QuickstartWeatherServer/Tools/WeatherTools.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ public static async Task<string> GetForecast(
4242
[Description("Longitude of the location.")] double longitude)
4343
{
4444
using var jsonDocument = await client.ReadJsonDocumentAsync($"/points/{latitude},{longitude}");
45-
var jsonElement = jsonDocument.RootElement;
46-
var periods = jsonElement.GetProperty("properties").GetProperty("periods").EnumerateArray();
45+
var forecastUrl = jsonDocument.RootElement.GetProperty("properties").GetProperty("forecast").GetString()
46+
?? throw new Exception($"No forecast URL provided by {client.BaseAddress}points/{latitude},{longitude}");
47+
48+
using var forecastDocument = await client.ReadJsonDocumentAsync(forecastUrl);
49+
var periods = forecastDocument.RootElement.GetProperty("properties").GetProperty("periods").EnumerateArray();
4750

4851
return string.Join("\n---\n", periods.Select(period => $"""
4952
{period.GetProperty("name").GetString()}
@@ -52,4 +55,4 @@ public static async Task<string> GetForecast(
5255
Forecast: {period.GetProperty("detailedForecast").GetString()}
5356
"""));
5457
}
55-
}
58+
}

0 commit comments

Comments
 (0)