Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Samples] Add more detailed forecasts from WeatherTools #134

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
9 changes: 6 additions & 3 deletions samples/QuickstartWeatherServer/Tools/WeatherTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ public static async Task<string> GetForecast(
[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 properties = jsonDocument.RootElement.GetProperty("properties");
var forecastUrl = properties.GetProperty("forecast").GetString();

using var forecastDocument = await client.ReadJsonDocumentAsync(forecastUrl ?? $"/{properties.GetProperty("gridId")}/{properties.GetProperty("gridX")},{properties.GetProperty("gridY")}/forecast");
var periods = forecastDocument.RootElement.GetProperty("properties").GetProperty("periods").EnumerateArray();

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