/OpenWeatherMap.NetClient

A simple, asynchronous .NET client to fetch weather information from the OpenWeatherMap APIs

Primary LanguageC#MIT LicenseMIT

OpenWeatherMap.NetClient

Nuget Nuget

A simple, asynchronous .NET client to fetch weather information from the OpenWeatherMap APIs.

Support for response caching and retries is built in.
Numerical values are parsed to and returned as units from Units.NET (where applicable) to ease the conversion between different measurement systems and avoid unit confusion.

Supported APIs:

  • Current Weather - Current weather data for any location on Earth including over 200,000 cities.
  • Geocoding - Tool to ease the search for locations while working with geographic names and coordinates.
  • Air Pollution - Provides current, forecast and historical air pollution data for any coordinates on the globe.
  • Basic weather maps - Provides many kinds of weather maps including Precipitation, Clouds, Pressure, Temperature, Wind.
  • 3-hour Forecast 5 days - Provides 5 days weather forecast data with 3-hour steps.

Installation

OpenWeatherMap.NetClient is available from NuGet

Usage

Creating a client is as simple as just passing in your Api Key to the constructor.

var client = new OpenWeatherMapClient("[API_KEY]");

// querying the current weather for a location based on name
var weather = await client.CurrentWeather.QueryAsync("Linz,AT");
Console.Out.WriteLine($"The current weather in Linz is '{weather.WeatherDescription}' " +
                      $"at {weather.Temperature.DegreesCelsius}°C");

// querying the current air pollution for geographical coordinates
var airPollution = await client.AirPollution.QueryCurrentAsync(48.3059, 14.2862);
Console.Out.WriteLine($"The current air quality index is '{airPollution.AirQualityIndex.ToString()}'");

// getting the global temperature map (zoom 0, tile 0/0)
var map = await client.BasicWeatherMaps.GetMapAsync(BasicWeatherMapLayer.Temperature, 0, 0, 0);
await File.WriteAllBytesAsync("C:\\ [...] \\temperature_map.png", map);

// querying the next 2 segments of the weather forecast
var response = await client.Forecast5Days.QueryAsync("Vienna, AT", 2);
var forecast = response.Forecast.ToArray();
Console.Out.WriteLine($"At {forecast[1].ForecastTimeStamp.ToShortTimeString()} " +
                      $"the weather in Vienna will be '{forecast[1].WeatherCondition}'");

Configuration

Setting Default value
Culture "en" Language to get textual outputs in
CacheDuration TimeSpan.Zero Duration the API responses will be cached
RetryCount 1 How often to retry on timeout or API error
RetryWaitDurationProvider _ => TimeSpan.Zero Duration to wait between retries

You can modify the default configuration of the client by additionally passing OpenWeatherMapOptions

var client = new OpenWeatherMapClient("[API_KEY]", new OpenWeatherMapOptions
{
  Culture = new CultureInfo("en"),
  CacheDuration = TimeSpan.FromMinutes(10),
  RetryCount = 3,
  RetryWaitDurationProvider = attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt)) // exponential back-off
});

ASP.NET

Simply call the provided extension method to register the service in your Startup.cs

// with default configuration
services.AddOpenWeatherMap("[API KEY]");

// or with custom client configuration
services.AddOpenWeatherMap("[API KEY]", new OpenWeatherMapOptions
{
  CacheDuration = TimeSpan.FromMinutes(2)
});

Dependencies

  • Refit - REST request handling
  • Polly - retries and caching
  • Units.NET - units for numerical weather values