Skip to content

.NET Core client library for the OpenElectricity API

License

Notifications You must be signed in to change notification settings

TheOneHoff/OpenElectricity.Sdk.Core

Repository files navigation

OpenElectricity.Sdk.Core

.NET Core client library for the OpenElectricity API

Nuget package is available at nuget.org/packages/OpenElectricity.Sdk.Core

Note

This project is not affiliated with OpenElectricity

Warning

The v4 OpenElectricity API is currently under active development.

Documentation

To obtain an API key visit platform.openelectricity.org.au

For documentation visit docs.openelectricity.org.au

The officially supported Python client is available at github.com/openelectricity/openelectricity-python

The officially supported Typescript client is available at github.com/openelectricity/openelectricity-typescript

Features

  • Full C# support for all publicly available HTTP routes
  • Comprehensive class and enum definitions
  • Full async support for requests
  • Dependency injection support

Installation

Dotnet CLI

dotnet add package OpenElectricity.Sdk.Core

Quick Start

Generate an API key here platform.openelectricity.org.au

Basic Usage

Note

Only one client should be created per application to avoid port exhaustion learn.microsoft.com/httpclient-guidelines

Create a new client and pass in your API key

Warning

Ensure that you do not hard code your API key. Use a secure provider to pass the key into the application

using OpenElectricity.Sdk.Client;

namespace NewProject
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            // Pass your API key into the options
            var options = new OpenElectricityOptions()
            {
                ApiKey = "[YOUR API KEY HERE]"
            };

            // Initialize the client
            var client = new OpenElectricityClient(options);

            // Use the client
            var facilities = await client.GetFacilitiesAsync();
        }
    }
}

Dependency injection

Create an appsettings.json with the following configuration

{
  "OpenElectricityOptions": {
    "ApiKey": "[YOUR API KEY HERE]",
  }
}

(Optional) Use user secrets to pass the token to the configuration provider

# Intialize your user secrets
dotnet user-secrets init

# Set the secret
dotnet user-secrets set "OpenElectricityOptions:ApiKey" "[YOUR API KEY HERE]"

Create your class that uses the OpenElectricityClient

using OpenElectricity.Sdk.Client;

namespace NewProject
{
    public class NewClass(OpenElectricityClient openElectricityClient)
    {
        readonly OpenElectricityClient _client = openElectricityClient;

        public async Task DoSomethingAsync(CancellationToken cancellationToken = default)
        {
            var facilities = await _client.GetFacilitiesAsync(cancellationToken: cancellationToken);
        }
    }
}

Setup dependency injection in Program.cs

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenElectricity.Sdk.Client;

namespace NewProject
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            // Create the DI builder
            var builder = Host.CreateApplicationBuilder(args);

            // Register the client in DI
            builder.Services.UseOpenElectricityClient(builder.Configuration);

            // Register your class in DI
            builder.Services.AddScoped<NewClass>();

            // Build the application
            var app = builder.Build();

            // Use your class
            var scope = app.Services.CreateScope();
            var newClass = scope.ServiceProvider.GetService<NewClass>();
            if (newClass is null) return;

            await newClass.DoSomethingAsync();
            return;
        }
    }
}

Development

Dependencies

Workflow

# Install project dependencies
dotnet restore

# Build
dotnet build

# Run tests
dotnet test

# NuGet pack
dotnet pack

License

MIT

About

.NET Core client library for the OpenElectricity API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages