Skip to content

Latest commit

 

History

History
155 lines (103 loc) · 4.5 KB

README.md

File metadata and controls

155 lines (103 loc) · 4.5 KB

StromGedacht.NET

dotnet nuget version nuget downloads build codecov GitHub license

C# client for the StromGedacht API

Installation

Available on NuGet.

dotnet add package StromGedacht.NET

or

PM> Install-Package StromGedacht.NET

Usage

The client can provide the region state at the current time or all states for a given time period.

The period may extend a maximum of 2 days into the future and 4 days into the past.

Each time you make a request, you will need to provide the zip code of the region for which you want to request the state.

Initialization

First create an instance of StromGedachtClient by passing an instance of HttpClient to its constructor.

var httpClient = new HttpClient();

var client = new StromGedachtClient(httpClient);

Get current state

You can fetch the current state of a region by calling the Now / NowAsync methods and passing the zip code of the region.

var state = client.Now("70173");
var state = await client.NowAsync("70173");

If the api returns an error, this method returns null. This could happen if the zip code is invalid / not supported.

Get states for time period

You can fetch all states of a region for a specific time period by calling the States / StatesAsync methods and passing the zip code of the region, the start time and end time.

Start and end time can be two dates:

var from = new DateTimeOffset(2023, 1, 1, 0, 0, 0, TimeSpan.FromHours(2));
var to = new DateTimeOffset(2023, 1, 3, 23, 59, 59, TimeSpan.FromHours(2));

var states = client.States("70173", from, to);
var from = new DateTimeOffset(2023, 1, 1, 0, 0, 0, TimeSpan.FromHours(2));
var to = new DateTimeOffset(2023, 1, 3, 23, 59, 59, TimeSpan.FromHours(2));

var states = await client.StatesAsync("70173", from, to);

or the hours relative to this moment:

var hoursInPast = 24;
var hoursInFuture  = 48;

var states = client.States("70173", hoursInPast, hoursInFuture);
var hoursInPast = 24;
var hoursInFuture  = 48;

var states = await client.StatesAsync("70173", hoursInPast, hoursInFuture);

If the api returns an error, this method returns an empty list. This could happen if the zip code is invalid / not supported or the supported period is exceeded.

Get forecast

You can fetch the forecast of a region for a specific time period by calling the Forecast / ForecastAsync methods and passing the zip code of the region, the start time and end time.

var from = new DateTimeOffset(2023, 1, 1, 0, 0, 0, TimeSpan.FromHours(2));
var to = new DateTimeOffset(2023, 1, 3, 23, 59, 59, TimeSpan.FromHours(2));

var forecast = client.Forecast("70173", from, to);
var from = new DateTimeOffset(2023, 1, 1, 0, 0, 0, TimeSpan.FromHours(2));
var to = new DateTimeOffset(2023, 1, 3, 23, 59, 59, TimeSpan.FromHours(2));

var forecast = await client.ForecastAsync("70173", from, to);

Dependency Injection

You can register the StromGedachtClient in your Startup with a typed HttpClient.

builder.Services.AddHttpClient<StromGedachtClient>();

Then inject the client wherever you like. E.g. in a controller:

[Route("Home")]
[ApiController]
public class HomeController : ControllerBase
{
    private readonly StromGedachtClient _client;

    public HomeController(StromGedachtClient client)
    {
        _client = client;
    }
}

API rate limits

The api is limited to about 6 requests per minute.

Related

Here are some related projects:

Shoutout

The used API is provided by StromGedacht, TransnetBW GmbH.