Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/derstimmler/stromgedacht.net
C# client for the StromGedacht API
https://github.com/derstimmler/stromgedacht.net
api csharp dotnet free library mit nuget package power stromgedacht
Last synced: about 1 month ago
JSON representation
C# client for the StromGedacht API
- Host: GitHub
- URL: https://github.com/derstimmler/stromgedacht.net
- Owner: DerStimmler
- License: mit
- Created: 2023-05-18T21:43:00.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-17T20:42:19.000Z (about 1 year ago)
- Last Synced: 2024-04-27T07:29:04.590Z (9 months ago)
- Topics: api, csharp, dotnet, free, library, mit, nuget, package, power, stromgedacht
- Language: C#
- Homepage: https://github.com/DerStimmler/StromGedacht.NET
- Size: 47.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# StromGedacht.NET
[![dotnet](https://img.shields.io/badge/platform-.NET-blue)](https://www.nuget.org/packages/StromGedacht.NET/)
[![nuget version](https://img.shields.io/nuget/v/StromGedacht.NET)](https://www.nuget.org/packages/StromGedacht.NET/)
[![nuget downloads](https://img.shields.io/nuget/dt/StromGedacht.NET)](https://www.nuget.org/packages/StromGedacht.NET/)
![build](https://github.com/DerStimmler/StromGedacht.NET/actions/workflows/build.yml/badge.svg)
[![codecov](https://codecov.io/gh/DerStimmler/StromGedacht.NET/branch/main/graph/badge.svg?token=8CCFM34SNC)](https://codecov.io/gh/DerStimmler/StromGedacht.NET)
[![GitHub license](https://img.shields.io/github/license/DerStimmler/StromGedacht.NET)](https://github.com/DerStimmler/StromGedacht.NET/blob/master/LICENSE.md)C# client for the [StromGedacht](https://www.stromgedacht.de/) API
## Installation
Available on [NuGet](https://www.nuget.org/packages/StromGedacht.NET/).
```bash
dotnet add package StromGedacht.NET
```or
```powershell
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.
```csharp
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.```csharp
var state = client.Now("70173");
``````csharp
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:
```csharp
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);
``````csharp
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:
```csharp
var hoursInPast = 24;
var hoursInFuture = 48;var states = client.States("70173", hoursInPast, hoursInFuture);
``````csharp
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.```csharp
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);
``````csharp
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`.
```csharp
builder.Services.AddHttpClient();
```Then inject the client wherever you like. E.g. in a controller:
```csharp
[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:
- [ts-stromgedacht](https://github.com/DerStimmler/ts-stromgedacht): TypeScript version of this library
## Shoutout
The used API is provided by [StromGedacht](https://www.stromgedacht.de), TransnetBW GmbH.