Skip to content
Merged
23 changes: 16 additions & 7 deletions samples/QuickstartWeatherServer/Tools/WeatherTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@ public static async Task<string> GetForecast(
[Description("Longitude of the location.")] double longitude)
{
var jsonElement = await client.GetFromJsonAsync<JsonElement>($"/points/{latitude},{longitude}");
var forecastUrl = jsonElement.GetProperty("properties").GetProperty("forecast").GetString();
jsonElement = await client.GetFromJsonAsync<JsonElement>(forecastUrl);
var periods = jsonElement.GetProperty("properties").GetProperty("periods").EnumerateArray();

return string.Join("\n---\n", periods.Select(period => $"""
{period.GetProperty("name").GetString()}
Temperature: {period.GetProperty("temperature").GetInt32()}°F
Wind: {period.GetProperty("windSpeed").GetString()} {period.GetProperty("windDirection").GetString()}
Forecast: {period.GetProperty("detailedForecast").GetString()}
"""));
return periods.Select(period =>
{
return $"""
Name: {period.GetProperty("name").GetString()}
Start Time: {period.GetProperty("startTime").GetString()}
End Time: {period.GetProperty("endTime").GetString()}
Temperature: {period.GetProperty("temperature").GetInt32()}°F
Wind Speed: {period.GetProperty("windSpeed").GetString()}
Wind Direction: {period.GetProperty("windDirection").GetString()}
Short Forecast: {period.GetProperty("shortForecast").GetString()}
Detailed Forecast: {period.GetProperty("detailedForecast").GetString()}
""";
}).Aggregate((current, next) => current + "\n--\n" + next);
}
}
}