OpenWeatherMapAPI.ClientLibrary 2.1.0

dotnet add package OpenWeatherMapAPI.ClientLibrary --version 2.1.0
                    
NuGet\Install-Package OpenWeatherMapAPI.ClientLibrary -Version 2.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenWeatherMapAPI.ClientLibrary" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenWeatherMapAPI.ClientLibrary" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="OpenWeatherMapAPI.ClientLibrary" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenWeatherMapAPI.ClientLibrary --version 2.1.0
                    
#r "nuget: OpenWeatherMapAPI.ClientLibrary, 2.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpenWeatherMapAPI.ClientLibrary@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpenWeatherMapAPI.ClientLibrary&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=OpenWeatherMapAPI.ClientLibrary&version=2.1.0
                    
Install as a Cake Tool

OpenWeatherMapAPI

A wrapper library for OpenWeatherMap API built in .NET Core 3.1 Currently, the package supports the following endpoints:

  • Current Weather Data:
    • By geographic coordinates
  • Air Pollution:
    • By geographic coordinates

And the following API versions were tested:

  • v2.5

Installation

Package Manager:

Install-Package OpenWeatherMapAPI.ClientLibrary

.NET CLI:

dotnet add package OpenWeatherMapAPI.ClientLibrary

Setup

When using dependency injection in .NET Core 3.X, you can register type like so, by registering a type in the ConfigureServices() method. Outside of the DI registration, the OpenWeatherMapAPIClientConfiguration.APIKey is required, and the rest of the config is optional and has default values:

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
	OpenWeatherMapAPIClientConfiguration.APIKey = "MY_API_KEY"
	OpenWeatherMapAPIClientConfiguration.APIURL = "API_URL_TO_USE" // OPTIONAL
	OpenWeatherMapAPIClientConfiguration.APIVersion = "API_VERSION_TO_USE" // OPTIONAL


	services.AddScoped<IOpenWeatherMapAPIClient, OpenWeatherMapAPIClient>();
}

MyClass.cs:

public class MyClass
{
	private readonly IOpenWeatherMapAPIClient _api;

	public MyService(IOpenWeatherMapAPIClient api)
	{
		_api = api;
	}
}

Alternatively, you don't have to inject/instantiate the whole API client and only choose services that you need, by injecting i.e. ICurrentWeatherDataService.

You can control which version of OpenWeatherMap API you're using by providing an optional OpenWeatherMapAPIClientConfiguration.APIVersion parameter to your global Startup config and using OpenWeatherMapAPI.Models.Shared.Constants class (i.e. Constants.Ver2_5), or provide the version yourself. You can do the same with the API URL by providing a OpenWeatherMapAPIClientConfiguration.APIVersion in your config. Similarly, this is optional as the default value is provided automatically.

Usage

The example project that uses the below code can be found under OpenWeatherMapAPI.SystemTests. Feel free to clone and play around!

Quickstart:

OpenWeatherMapAPIClientConfiguration.APIKey = "MY_API_KEY"
var apiClient = new OpenWeatherMapAPIClient();
var currentWeather = apiClient.CurrentWeatherData;

var request = new ByGeographicCoordinatesRequest()
{
	Latitude = 53.7998,
	Longitude = 1.5584
};

var result = await currentWeather.ByGeographicCoordinatesAsync(request);

// {"coord":{"lon":1.5584,"lat":53.7998},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":286.48,"feels_like":286.11,"temp_min":286.48,"temp_max":286.48,"pressure":1015,"humidity":86},"visibility":10000,"wind":{"speed":9.68,"deg":180},"clouds":{"all":100},"dt":1634563421,"sys":{"type":2,"id":2029944,"country":"GB","sunrise":1634538410,"sunset":1634575838},"timezone":0,"id":2650519,"name":"Easington","cod":200}

Testing

Some Integration and System tests are available in the repo, but make sure you update the OpenWeatherMapAPI.TestResources.BaseConstants.APIkey constant.

Development

In order to add a new service to the library, you'll need to:

  • Add a new endpoint constant in OpenWeatherMapAPI.Models.Shared.Constants
  • Add a new folder under OpenWeatherMapAPI.Models with your new service name. In that folder, add a POCO response class
  • For each method you're adding, also add a request class in the above namespace (if not existing yet)
  • Add a folder under OpenWeatherMapAPI.Services with the new service you're adding and add a concrete class and an abstract interface. Derive your class from APIGateway class. Don't call the API yourself - use your service to construct the request object you need to pass on and use the ExecuteGetAsync<T> method to do the calling for you
  • Add your new interface to the OpenWeatherMapAPIClient and IOpenWeatherMapAPIClient as a property
  • Following the existing structure, add any sample requests/responses to OpenWeatherMapAPI.TestResources project, add integration tests in OpenWeatherMapAPI.IntegrationTests project and a logic for system tests in OpenWeatherMapAPI.SystemTests

Deployment

Use tags for versioning. Check the current iteration (tag) and in cmd:

git checkout [test/master]
git pull
git tag v[Major].[Minor].[Patch]-[beta if test branch]
git push origin [version]

Then, push the code to test and merge into master.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 733 10/28/2021
2.1.0-beta 302 10/28/2021
2.0.15-beta 301 10/28/2021
2.0.9-beta 309 10/28/2021
2.0.8-beta 294 10/28/2021
2.0.7-beta 299 10/28/2021
2.0.6-beta 320 10/28/2021
2.0.5-beta 317 10/28/2021
2.0.4-beta 326 10/28/2021
2.0.3-beta 316 10/28/2021
2.0.1-beta 309 10/28/2021
2.0.0 400 10/19/2021
2.0.0-beta 305 10/19/2021
1.1.0 417 10/19/2021
1.1.0-beta 311 10/18/2021
1.0.0 453 10/18/2021