Skip to content

.NET SDK for Novu - The open-source notification infrastructure for engineers. πŸš€

License

Notifications You must be signed in to change notification settings

novuhq/novu-dotnet

Repository files navigation

novu-dotnet

NuGet NuGet Deploy to Nuget

.NET SDK for Novu - The open-source notification infrastructure for engineers. πŸš€

novu-dotnet targets .NET Standard 2.0 and is compatible with .NET Core 2.0+ and .NET Framework 4.6.1+.

Features

  • Bindings against most API endpoints
    • Events, subscribers, notifications, integrations, layouts, topics, workflows, workflow groups, messages, execution details
    • Not Implemented: environments, inbound parse, changes
  • Bootstrap each services as part of services provider or directly as a singleton class (setting injectable)
  • A Sync service that will mirror an environment based a set of templates (layouts, integrations, workflow groups, workflows)

WARNING: 0.3.0 has breaking changes and the tests should be relied on for understanding the client libraries

Dependencies

dotnet novu novu api package Notes
0.2.2 <= 0.17 Singleton client with Refit's use of RestService
0.3.0 >= 0.18 0.3.0 is not compatible with 0.2.2 and requires upgrade to code. Also 0.18 introduced a breaking change only found in 0.3.0. All 0.2.2 must be upgraded if used against the production system. HttpClient can now be used and injected.
0.3.1 >= 0.18 Failed release. You will not find this release on Nuget.
0.3.2 >= 0.18 [BREAKING} Obsolete Notification Templates has been removed. Service registration separation of single client and each client. Novu.Extension and Novu.Sync released as packages.

Installation

dotnet add package Novu

Configuration

Direct instantiation

using Novu.DTO;
using Novu.Models;
using Novu;

 var novuConfiguration = new NovuClientConfiguration
{
    // Defaults to https://api.novu.co/v1
    Url = "https://novu-api.my-domain.com/v1",
    ApiKey = "12345",
};

var novu = new NovuClient(novuConfiguration);

// Note: this client exposes all endpoints as methods but uses RestService
var subscribers = await novu.Subscriber.Get();

Dependency Injection

Configure via settings

{
  "Novu": {
    "Url": "http://localhost:3000/v1",
    "ApiKey": "e36b820fcc9a68a83db6c79c30f1a461"
  }
}

Setup Injection via extension methods

public static IServiceCollection RegisterNotificationSetupServices(
    this IServiceCollection services,
    IConfiguration configuration)
{
    // registers all clients with novu config from appsetting.json
    // the services inject HttpClient
    return services
        .RegisterNovuClients(configuration)
        // here as an example that the registered services are injected into local service
        .AddTransient<NovuNotificationService>();
}

Write your consuming code with the injected clients

// then instantiate via injection
public class NovuNotificationService
{
    private readonly IEventClient _event;

    public NovuSyncService(IEventClient @event)
    {
        _event = @event;
    }

    public async Task Trigger(string subscriberId){
       var trigger = await Event.Trigger(
            new EventCreateData
            {
                EventName = 'event-name',
                To = { SubscriberId =subscriberId },
                Payload = new Payload("Bogus payload"),
            });
    }

    public record Payload(string Message)
    {
        [JsonProperty("message")] public string Message { get; set; }
    }
}

Examples

Usage of the library is best understood by looking at the tests.

  • Integration Tests: these show the minimal dependencies required to do one primary request (create, update, delete)
  • Acceptance Tests: these show a sequence of actions to complete a business process in an environment

Repository Overview

Novu is the main SDK with Novu.Tests housing all unit tests. Novu.Extensions is required for DI and Novu.Sync if your are looking for mirroring environments.

novu-dotnet

The key folders to look into:

  • DTO directory holds all objects needed to use the clients
  • Interfaces directory holds all interfaces that are intended to outline how a class should be structured
  • Models directory holds various models that are sub-resources inside the DTOs

Major changes

Github issues closed

0.3.1

  • #57
  • #58
  • #59
  • #60
  • #55

0.3.0

  • #19
  • #20
  • #21
  • #34
  • #48
  • #49
  • #50
  • #47
  • #45

About

.NET SDK for Novu - The open-source notification infrastructure for engineers. πŸš€

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published