Fix api.weather.gov/points response handling in WeatherTools (#134)

This commit is contained in:
Esteban Solano Granados 2025-04-07 23:39:10 -06:00 • committed by GitHub
commit 8bd9ee81c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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()}
"""));
}
}
}