C# client for the StromGedacht API
Available on NuGet.
dotnet add package StromGedacht.NET
or
PM> Install-Package StromGedacht.NET
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.
First create an instance of StromGedachtClient
by passing an instance of HttpClient
to its constructor.
var httpClient = new HttpClient();
var client = new StromGedachtClient(httpClient);
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.
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.
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);
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;
}
}
The api is limited to about 6 requests per minute.
Here are some related projects:
- ts-stromgedacht: TypeScript version of this library
The used API is provided by StromGedacht, TransnetBW GmbH.