An asynchronous .NET Standard 2.0 library that allows you to fetch & cache current weather readings from the OpenWeather API, with built-in resiliency that can extend the cache lifetime in case the API is unreachable.
Supports .NET Framework 4.6.1 or later, .NET Core 2.0 or later, and .NET 5.0 or later.
The recommended means is to use NuGet, but you could also download the source code from here.
If the time elapsed since the last fetch for the given location exceeds the cache period (i.e. a new API call is required) but is within the resiliency period (i.e. the previous readings are still available in the cache), the API reported measured time in the cache value is sometimes more recent than the latest value fetched from the API. There are three modes to tackle this issue:
With FetchMode.AlwaysUseLastMeasured, the value still available in the cache is returned. The implication is that if you immediately request another reading it will also hit the API again. IMPORTANT: Frequent calls may impact your API usage.
With FetchMode.AlwaysUseLastMeasuredButExtendCache (default, recommended), the value still available in the cache is returned but in order to protect impact on your API usage, this setting updates the cache value's fetched date and extends the cache lifetime.
With FetchMode.AlwaysUseLastFetchedValue, the last fetched API result is returned anyway, even though it is being reported to be older by the API.
In your Startup.cs (ConfigureServices):
services.AddOpenWeatherMapCache("[API KEY]", 9_500, FetchMode.AlwaysUseLastMeasuredButExtendCache, 300_000);
Then you can inject IOpenWeatherMapCache.
Create your own instance:
var openWeatherMapCache = new OpenWeatherMapCache("[API KEY]", 9_500, FetchMode.AlwaysUseLastMeasuredButExtendCache, 300_000);
var locationQuery = new OpenWeatherMap.Cache.Models.Location(47.6371, -122.1237);
var readings = await openWeatherMapCache.GetReadingsAsync(locationQuery);
if (readings.IsSuccessful)
{
...
}
else
{
var apiErrorCode = readings.Exception?.ApiErrorCode;
var apiErrorMessage = readings.Exception?.ApiErrorMessage;
}
or by zip code (post code) and country code:
var locationQuery = new OpenWeatherMap.Cache.Models.ZipCode("94040", "us");
var readings = await openWeatherMapCache.GetReadingsAsync(locationQuery);
if (readings.IsSuccessful)
{
...
}
else
{
var apiErrorCode = readings.Exception?.ApiErrorCode;
var apiErrorMessage = readings.Exception?.ApiErrorMessage;
}
var locationQuery = new OpenWeatherMap.Cache.Models.Location(47.6371, -122.1237);
var readings = openWeatherMapCache.GetReadings(locationQuery);
if (readings.IsSuccessful)
{
...
}
else
{
var apiErrorCode = readings.Exception?.ApiErrorCode;
var apiErrorMessage = readings.Exception?.ApiErrorMessage;
}
or by zip code (post code) and country code:
var locationQuery = new OpenWeatherMap.Cache.Models.ZipCode("94040", "us");
var readings = openWeatherMapCache.GetReadings(locationQuery);
if (readings.IsSuccessful)
{
...
}
else
{
var apiErrorCode = readings.Exception?.ApiErrorCode;
var apiErrorMessage = readings.Exception?.ApiErrorMessage;
}