WeatherAPI.NET 0.1.1-alpha

This is a prerelease version of WeatherAPI.NET.
Install-Package WeatherAPI.NET -Version 0.1.1-alpha
dotnet add package WeatherAPI.NET --version 0.1.1-alpha
<PackageReference Include="WeatherAPI.NET" Version="0.1.1-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WeatherAPI.NET --version 0.1.1-alpha
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: WeatherAPI.NET, 0.1.1-alpha"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install WeatherAPI.NET as a Cake Addin
#addin nuget:?package=WeatherAPI.NET&version=0.1.1-alpha&prerelease

// Install WeatherAPI.NET as a Cake Tool
#tool nuget:?package=WeatherAPI.NET&version=0.1.1-alpha&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

License: MIT GitHub forks

WeatherAPI.NET

Unofficial .NET API client for the weather API available at weatherapi.com.


Getting started

API key

You need an API key to be able to authenticate with the API. You can either sign up for, or log in to WeatherAPI to get your API key.

Creating the client

WeatherAPIClient accepts two constructor parameters: your API key, and a custom base URI for the API. A valid API key must be provided, but specifying a base URI is optional.

string apiKey = "your API key";

WeatherAPIClient weatherApiClient = new WeatherAPIClient(apiKey);

Using the API

API routes are separated out into category classes, referred to as operations, within the client. These classes contain all routes associated with the category, as well as overflow methods providing different request configurations.

Where required, or where available, you'll have the option to provide a request object to the method. The type of request object required is determined by the method you are trying to invoke, but all request objects adopt a fluent API style configuration layout, for example:

RequestEntity request = new RequestEntity()
    .WithCityName("Paris")
    .WithLanguage("fr");

Different request objects exist to provide extra configuration for the category of request you're trying to make. For example, you can request air quality data with the realtime routes:

RealtimeRequestEntity request = new RealtimeRequestEntity()
    .WithCityName("Paris")
    .WithLanguage("fr")
    .WithAirQualityData(true);

Features

Sample projects are available for all primary features. Please see the README to get started with the sample projects.


Extending

The client has been designed to make extending the base functionality as easy, and as flexible, as possible. See the extending sample project for a full example.

Custom response entities

Providing custom entities allows you to inject an object type directly into the deserialization process. This means that there's no hassle serializing and deserializing the standard response objects to get what you want.

// Custom astronomy response entity.
public class CustomAstronomyResponseEntity : AstronomyResponseEntity
{
    private readonly string _message = "I'm a message from a custom entity!";

    public void DoSomethingCustom()
    {
        Console.WriteLine(_message);
    }
}

// Your method to request the custom entity.
public async Task GetCustomAstronomyAsync()
{
    CustomAstronomyResponseEntity customAstronomy = await weatherApiClient.Astronomy.GetAstronomyAsync<CustomAstronomyResponseEntity>();
}

Custom operation classes

To build on top of the existing client's request functionality, you can extend operation classes too. Extending operations requires an extended, custom API client.

// Custom API client.
public class CustomWeatherAPIClient : WeatherAPIClient
{
    protected override IAstronomyOperations ConstructAstronomyOperations()
    {
        return new CustomAstronomyOperations(this);
    }

    public CustomWeatherAPIClient(string apiKey)
        : base(apiKey, null)
    {
    }
}

// Custom astronomy operations.
public class CustomAstronomyOperations : AstronomyOperations
{
    // Overflow methods funnel down to the "most advanced" variant.
    // This means that calls to the "lower level" overflow methods will also invoke this custom logic.
    public override async Task<TAstronomyResponseEntity> GetAstronomyAsync<TAstronomyResponseEntity>(RequestEntity request, CancellationToken cancellationToken = default)
    {
        var astronomyResponseEntity = await base.GetAstronomyAsync<TAstronomyResponseEntity>(request, cancellationToken);

        if (astronomyResponseEntity is CustomAstronomyResponseEntity customAstronomyEntity)
            customAstronomyEntity.DoSomethingCustom();

        return astronomyResponseEntity;
    }

    public CustomAstronomyOperations(IApiRequestor apiRequestor)
        : base(apiRequestor)
    {
    }
}

NuGet packages (1)

Showing the top 1 NuGet packages that depend on WeatherAPI.NET:

Package Downloads
WeatherAPI.NET.Extensions.NodaTime

NodaTime extensions to the unofficial .NET API client for the weather API available at weatherapi.com.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.1-alpha 98 4/15/2021
0.1.0-alpha 439 4/14/2021