Table of Contents

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

apiKey string

Optional 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

apiKey string

The account key. The same key can be used by controllers selecting different services.

service OpenWeatherService

Service 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

coordinates LatLong

The coordinates of the location

units string

Units of measurement. Standard, metric, and imperial units are available. Default is "metric"

cancellationToken CancellationToken

Token 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

lat double

Latitude of the location

lon double

Longitude of the location

units string

Units of measurement. Standard, metric, and imperial units are available. Default is "metric"

cancellationToken CancellationToken

Token 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

cityName string

The name of the city

stateCode string

The state code (optional, for US cities)

countryCode string

The country code (optional, e.g. "US", "GB")

units string

Units of measurement. Standard, metric, and imperial units are available. Default is "metric"

cancellationToken CancellationToken

Token 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

postCode string

The postal or zip code

countryCode string

The country code (e.g. "US", "GB")

units string

Units of measurement. Standard, metric, and imperial units are available. Default is "metric"

cancellationToken CancellationToken

Token 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

coordinates LatLong

The coordinates of the location

units string

Units of measurement. Standard, metric, and imperial units are available. Default is "metric"

cancellationToken CancellationToken

Token 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

lat double

The latitude in decimal degrees.

lon double

The longitude in decimal degrees.

includeHourly bool

Whether to include hourly forecast data and make any additional requests needed for it.

units string

The OpenWeather units setting, such as metric or imperial.

cancellationToken CancellationToken

A 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

lat double

Latitude of the location

lon double

Longitude of the location

units string

Units of measurement. Standard, metric, and imperial units are available. Default is "metric"

cancellationToken CancellationToken

Token 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

cityName string

The city name to geocode.

includeHourly bool

Whether to include hourly forecast data and make any additional requests needed for it.

stateCode string

The optional state code used to disambiguate the city.

countryCode string

The optional country code used to disambiguate the city.

units string

The OpenWeather units setting, such as metric or imperial.

cancellationToken CancellationToken

A 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

cityName string

The name of the city

stateCode string

The state code (optional, for US cities)

countryCode string

The country code (optional, e.g. "US", "GB")

units string

Units of measurement. Standard, metric, and imperial units are available. Default is "metric"

cancellationToken CancellationToken

Token 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

postCode string

The postal or zip code

countryCode string

The country code (e.g. "US", "GB")

units string

Units of measurement. Standard, metric, and imperial units are available. Default is "metric"

cancellationToken CancellationToken

Token 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

coordinates LatLong

The location to query.

includeHourly bool

Whether to request hourly forecasts. Set false to avoid hourly pagination charges in 4.0.

units string

OpenWeather units: metric, imperial or standard.

cancellationToken CancellationToken

Cancels 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

cancellationToken CancellationToken

Token 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.