Class WeatherController
- Namespace
- SimpleWeather
- Assembly
- SimpleWeather.dll
Provides high level weather APIs backed by the OpenWeather One Call endpoints.
public class WeatherController : IDisposable
- Inheritance
-
WeatherController
- Implements
- Inherited Members
Constructors
WeatherController(string?)
Initializes a new instance of the WeatherController class.
public WeatherController(string? apiKey = null)
Parameters
apiKeystringOptional OpenWeather API key used for authenticated requests.
WeatherController(string?, OpenWeatherService)
Creates a client for an explicitly selected service, or automatic fallback.
public WeatherController(string? apiKey, OpenWeatherService service)
Parameters
apiKeystringThe account key. The same key can be used by controllers selecting different services.
serviceOpenWeatherServiceService selection. Explicit selections never fall back to another service.
Properties
Service
The service selected for this controller. Other controllers using the same key are independent.
public OpenWeatherService Service { get; }
Property Value
- OpenWeatherService
The service selected for this controller. Other controllers using the same key are independent.
Methods
Dispose()
Disposes the underlying HttpClient instance and suppresses finalization.
public void Dispose()
GetCurrentWeatherAsync(LatLong, string, CancellationToken)
Asynchronously gets the current weather for the specified coordinates.
public Task<CurrentWeather> GetCurrentWeatherAsync(LatLong coordinates, string units = "metric", CancellationToken cancellationToken = default)
Parameters
coordinatesLatLongThe coordinates of the location
unitsstringUnits of measurement. Standard, metric, and imperial units are available. Default is "metric"
cancellationTokenCancellationTokenToken used to cancel the HTTP request.
Returns
- Task<CurrentWeather>
A task representing the asynchronous operation whose result is a CurrentWeather for the specified coordinates.
GetCurrentWeatherAsync(double, double, string, CancellationToken)
Returns an object that contains all the relevant data for the current weather at the specified latitude and longitude.
public Task<CurrentWeather> GetCurrentWeatherAsync(double lat, double lon, string units = "metric", CancellationToken cancellationToken = default)
Parameters
latdoubleLatitude of the location
londoubleLongitude of the location
unitsstringUnits of measurement. Standard, metric, and imperial units are available. Default is "metric"
cancellationTokenCancellationTokenToken used to cancel the HTTP request.
Returns
- Task<CurrentWeather>
A CurrentWeather object for the specified coordinates
GetCurrentWeatherAsync(string, string?, string?, string, CancellationToken)
Returns an object that contains all the relevant data for the current weather in a given city.
public Task<CurrentWeather> GetCurrentWeatherAsync(string cityName, string? stateCode = null, string? countryCode = null, string units = "metric", CancellationToken cancellationToken = default)
Parameters
cityNamestringThe name of the city
stateCodestringThe state code (optional, for US cities)
countryCodestringThe country code (optional, e.g. "US", "GB")
unitsstringUnits of measurement. Standard, metric, and imperial units are available. Default is "metric"
cancellationTokenCancellationTokenToken used to cancel the HTTP request.
Returns
- Task<CurrentWeather>
A CurrentWeather object for the specified city
GetCurrentWeatherByPostCodeAsync(string, string, string, CancellationToken)
Returns an object that contains all the relevant data for the current weather at the specified postal/zip code and country code.
public Task<CurrentWeather> GetCurrentWeatherByPostCodeAsync(string postCode, string countryCode, string units = "metric", CancellationToken cancellationToken = default)
Parameters
postCodestringThe postal or zip code
countryCodestringThe country code (e.g. "US", "GB")
unitsstringUnits of measurement. Standard, metric, and imperial units are available. Default is "metric"
cancellationTokenCancellationTokenToken used to cancel the HTTP request.
Returns
- Task<CurrentWeather>
A CurrentWeather object for the specified postal/zip code
GetWeatherForecastAsync(LatLong, string, CancellationToken)
Asynchronously gets the weather forecast for the specified coordinates.
public Task<WeatherForecast> GetWeatherForecastAsync(LatLong coordinates, string units = "metric", CancellationToken cancellationToken = default)
Parameters
coordinatesLatLongThe coordinates of the location
unitsstringUnits of measurement. Standard, metric, and imperial units are available. Default is "metric"
cancellationTokenCancellationTokenToken used to cancel the HTTP request.
Returns
- Task<WeatherForecast>
A task representing the asynchronous operation whose result is a WeatherForecast for the specified coordinates.
GetWeatherForecastAsync(double, double, bool, string, CancellationToken)
Gets a coordinate forecast, optionally omitting hourly data and its One Call 4.0 requests.
public Task<WeatherForecast> GetWeatherForecastAsync(double lat, double lon, bool includeHourly, string units = "metric", CancellationToken cancellationToken = default)
Parameters
latdoubleThe latitude in decimal degrees.
londoubleThe longitude in decimal degrees.
includeHourlyboolWhether to include hourly forecast data and make any additional requests needed for it.
unitsstringThe OpenWeather units setting, such as metric or imperial.
cancellationTokenCancellationTokenA token used to cancel the operation.
Returns
- Task<WeatherForecast>
A task containing the forecast for the requested location.
GetWeatherForecastAsync(double, double, string, CancellationToken)
Returns an object that contains all the relevant weather forecast data for a given latitude and longitude.
public Task<WeatherForecast> GetWeatherForecastAsync(double lat, double lon, string units = "metric", CancellationToken cancellationToken = default)
Parameters
latdoubleLatitude of the location
londoubleLongitude of the location
unitsstringUnits of measurement. Standard, metric, and imperial units are available. Default is "metric"
cancellationTokenCancellationTokenToken used to cancel the HTTP request.
Returns
- Task<WeatherForecast>
A WeatherForecast object for the specified coordinates
GetWeatherForecastAsync(string, bool, string?, string?, string, CancellationToken)
Gets a city forecast, optionally omitting hourly data and its One Call 4.0 requests.
public Task<WeatherForecast> GetWeatherForecastAsync(string cityName, bool includeHourly, string? stateCode = null, string? countryCode = null, string units = "metric", CancellationToken cancellationToken = default)
Parameters
cityNamestringThe city name to geocode.
includeHourlyboolWhether to include hourly forecast data and make any additional requests needed for it.
stateCodestringThe optional state code used to disambiguate the city.
countryCodestringThe optional country code used to disambiguate the city.
unitsstringThe OpenWeather units setting, such as metric or imperial.
cancellationTokenCancellationTokenA token used to cancel the operation.
Returns
- Task<WeatherForecast>
A task containing the forecast for the requested location.
GetWeatherForecastAsync(string, string?, string?, string, CancellationToken)
Returns an object that contains all the relevant weather forecast data for a given city.
public Task<WeatherForecast> GetWeatherForecastAsync(string cityName, string? stateCode = null, string? countryCode = null, string units = "metric", CancellationToken cancellationToken = default)
Parameters
cityNamestringThe name of the city
stateCodestringThe state code (optional, for US cities)
countryCodestringThe country code (optional, e.g. "US", "GB")
unitsstringUnits of measurement. Standard, metric, and imperial units are available. Default is "metric"
cancellationTokenCancellationTokenToken used to cancel the HTTP request.
Returns
- Task<WeatherForecast>
A WeatherForecast object for the specified city
GetWeatherForecastByPostCodeAsync(string, string, string, CancellationToken)
Returns an object that contains all the relevant weather forecast data for the specified postal/zip code and country code.
public Task<WeatherForecast> GetWeatherForecastByPostCodeAsync(string postCode, string countryCode, string units = "metric", CancellationToken cancellationToken = default)
Parameters
postCodestringThe postal or zip code
countryCodestringThe country code (e.g. "US", "GB")
unitsstringUnits of measurement. Standard, metric, and imperial units are available. Default is "metric"
cancellationTokenCancellationTokenToken used to cancel the HTTP request.
Returns
- Task<WeatherForecast>
A WeatherForecast object for the specified postal/zip code
GetWeatherSnapshotAsync(LatLong, bool, string, CancellationToken)
Retrieves current conditions and forecast together. Prefers existing One Call 3.0 access, then tries 4.0, then the free endpoints when subscription access is denied.
public Task<WeatherSnapshot> GetWeatherSnapshotAsync(LatLong coordinates, bool includeHourly = true, string units = "metric", CancellationToken cancellationToken = default)
Parameters
coordinatesLatLongThe location to query.
includeHourlyboolWhether to request hourly forecasts. Set false to avoid hourly pagination charges in 4.0.
unitsstringOpenWeather units: metric, imperial or standard.
cancellationTokenCancellationTokenCancels requests, including pagination.
Returns
- Task<WeatherSnapshot>
Current weather and forecast. The 3.0 route uses one request; 4.0 normally uses two without hourly data or five with it, plus the denied 3.0 probe.
ProbeCapabilitiesAsync(CancellationToken)
Probes the available capabilities of the OpenWeather API for the configured API key.
public Task<OpenWeatherCapabilities> ProbeCapabilitiesAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenToken used to cancel the HTTP request.
Returns
- Task<OpenWeatherCapabilities>
A task representing the asynchronous operation whose result is an OpenWeatherCapabilities instance indicating the available capabilities.