@@ -1502,8 +1502,9 @@ public static class WeatherTools
1502
1502
HttpClient client ,
1503
1503
[Description (" The US state to get alerts for." )] string state )
1504
1504
{
1505
- var stateData = await client .GetFromJsonAsync <JsonElement >($" /alerts/active/area/{state }" );
1506
- var alerts = stateData .GetProperty (" features" ).EnumerateArray ();
1505
+ using var jsonDocument = await client .ReadJsonDocumentAsync ($" /alerts/active/area/{state }" );
1506
+ var jsonElement = jsonDocument .RootElement ;
1507
+ var alerts = jsonElement .GetProperty (" features" ).EnumerateArray ();
1507
1508
1508
1509
if (! alerts .Any ())
1509
1510
{
@@ -1529,16 +1530,13 @@ public static class WeatherTools
1529
1530
[Description (" Latitude of the location." )] double latitude ,
1530
1531
[Description (" Longitude of the location." )] double longitude )
1531
1532
{
1532
- var pointData = await client .GetFromJsonAsync <JsonElement >($" /points/{latitude },{longitude }" );
1533
- var forecastUrl = pointData .GetProperty (" properties" ).GetProperty (" forecast" ).GetString ();
1534
-
1535
- if (string .IsNullOrEmpty (forecastUrl ))
1536
- {
1537
- return " Forecast URL not found." ;
1538
- }
1539
-
1540
- var forecastData = await client .GetFromJsonAsync <JsonElement >(forecastUrl );
1541
- var periods = forecastData .GetProperty (" properties" ).GetProperty (" periods" ).EnumerateArray ();
1533
+ var pointUrl = string .Create (CultureInfo .InvariantCulture , $" /points/{latitude },{longitude }" );
1534
+ using var jsonDocument = await client .ReadJsonDocumentAsync (pointUrl );
1535
+ var forecastUrl = jsonDocument .RootElement .GetProperty (" properties" ).GetProperty (" forecast" ).GetString ()
1536
+ ?? throw new Exception ($" No forecast URL provided by {client .BaseAddress }points/{latitude },{longitude }" );
1537
+
1538
+ using var forecastDocument = await client .ReadJsonDocumentAsync (forecastUrl );
1539
+ var periods = forecastDocument .RootElement .GetProperty (" properties" ).GetProperty (" periods" ).EnumerateArray ();
1542
1540
1543
1541
return string .Join (" \n ---\n " , periods .Select (period => $"""
1544
1542
{period .GetProperty (" name" ).GetString ()}
0 commit comments