diff --git a/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs b/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs index 9092390e..316e8cf5 100644 --- a/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs +++ b/samples/AspireWithDapr/AspireWithDapr.Web/WeatherApiClient.cs @@ -1,12 +1,34 @@ using Dapr.Client; + namespace AspireWithDapr.Web; + public class WeatherApiClient(DaprClient daprClient) { + const string stateStore = "statestore"; + const string stateTTL = "120"; + const string apiAppId = "api"; + public async Task GetWeatherAsync() { - return await daprClient.InvokeMethodAsync(HttpMethod.Get, "api", "weatherforecast"); + + // Get the weather from the state store if it exists + var weatherData = await daprClient.GetStateAsync(stateStore, "weather"); + + if (weatherData is null) + { + // If it doesn't exist, get it from the weather service + weatherData = await daprClient.InvokeMethodAsync(HttpMethod.Get, apiAppId, "weatherforecast"); + + await daprClient.SaveStateAsync(stateStore, "weather", weatherData, metadata: new Dictionary() { + { + "ttlInSeconds", stateTTL + } + }); + } + + return weatherData; } }