Skip to content

Commit

Permalink
using enums to hold schema types
Browse files Browse the repository at this point in the history
  • Loading branch information
ruokun-niu committed Jan 7, 2025
1 parent 3c4d4b2 commit 5a8e957
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
22 changes: 15 additions & 7 deletions reactions/azure/eventgrid-reaction/Services/ChangeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ namespace Drasi.Reactions.EventGrid.Services;
// using CloudNative.CloudEvents;




public class ChangeHandler : IChangeEventHandler
{
private readonly EventGridPublisherClient _publisherClient;
private readonly OutputFormat _format;
private readonly IChangeFormatter _formatter;
private readonly ILogger<ChangeHandler> _logger;

private readonly string _eventGridSchema;
private readonly EventGridSchema _eventGridSchema;

public ChangeHandler(EventGridPublisherClient publisherClient,IConfiguration config, IChangeFormatter formatter, ILogger<ChangeHandler> logger)
{
_publisherClient = publisherClient;
_format = Enum.Parse<OutputFormat>(config.GetValue("format", "packed") ?? "packed", true);
_formatter = formatter;
_logger = logger;
_eventGridSchema = config.GetValue<string>("eventGridSchema").ToLower();
_eventGridSchema = Enum.Parse<EventGridSchema>(config.GetValue<string>("eventGridSchema"));
}

public async Task HandleChange(ChangeEvent evt, object? queryConfig)
Expand All @@ -50,15 +52,15 @@ public async Task HandleChange(ChangeEvent evt, object? queryConfig)
switch(_format)
{
case OutputFormat.Packed:
if (_eventGridSchema == "cloudevents") {
if (_eventGridSchema == EventGridSchema.CloudEvents) {
CloudEvent cloudEvent = new CloudEvent(evt.QueryId, "Drasi.ChangeEvent", _formatter.Format(evt));
var resp = await _publisherClient.SendEventAsync(cloudEvent);
if (resp.IsError)
{
_logger.LogError($"Error sending message to Event Grid: {resp.Content.ToString()}");
throw new Exception($"Error sending message to Event Grid: {resp.Content.ToString()}");
}
} else if (_eventGridSchema == "eventgrid") {
} else if (_eventGridSchema == EventGridSchema.EventGrid) {
EventGridEvent egEvent = new EventGridEvent(evt.QueryId, "Drasi.ChangeEvent", "1", _formatter.Format(evt));
var resp = await _publisherClient.SendEventAsync(egEvent);
if (resp.IsError)
Expand All @@ -71,7 +73,7 @@ public async Task HandleChange(ChangeEvent evt, object? queryConfig)

case OutputFormat.Unpacked:
var formattedResults = _formatter.Format(evt);
if (_eventGridSchema == "eventgrid") {
if (_eventGridSchema == EventGridSchema.EventGrid) {
List<EventGridEvent> events = new List<EventGridEvent>();
foreach (var notification in formattedResults)
{
Expand All @@ -84,7 +86,7 @@ public async Task HandleChange(ChangeEvent evt, object? queryConfig)
_logger.LogError($"Error sending message to Event Grid: {currResp.Content.ToString()}");
throw new Exception($"Error sending message to Event Grid: {currResp.Content.ToString()}");
}
} else if (_eventGridSchema == "cloudevents") {
} else if (_eventGridSchema == EventGridSchema.CloudEvents) {
List<CloudEvent> events = new List<CloudEvent>();
foreach (var notification in formattedResults)
{
Expand All @@ -111,4 +113,10 @@ enum OutputFormat
{
Packed,
Unpacked
}
}

enum EventGridSchema
{
CloudEvents,
EventGrid
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ public class ControlSignalHandler: IControlEventHandler

private readonly ILogger<ControlSignalHandler> _logger;

private readonly string _eventGridSchema;
private readonly EventGridSchema _eventGridSchema;

public ControlSignalHandler(EventGridPublisherClient publisherClient, IConfiguration config, ILogger<ControlSignalHandler> logger)
{
_publisherClient = publisherClient;
_format = Enum.Parse<OutputFormat>(config.GetValue("format", "packed") ?? "packed", true);
_logger = logger;
_eventGridSchema = config.GetValue<string>("eventGridSchema").ToLower();
_eventGridSchema = Enum.Parse<EventGridSchema>(config.GetValue<string>("eventGridSchema") ?? "CloudEvents", true);
}

public async Task HandleControlSignal(ControlEvent evt, object? queryConfig)
{
switch (_format)
{
case OutputFormat.Packed:
if (_eventGridSchema == "cloudevents")
if (_eventGridSchema == EventGridSchema.CloudEvents)
{
CloudEvent egEvent = new CloudEvent(evt.QueryId, "Drasi.ControlEvent", evt);
var resp = await _publisherClient.SendEventAsync(egEvent);
Expand All @@ -56,7 +56,7 @@ public async Task HandleControlSignal(ControlEvent evt, object? queryConfig)
throw new Exception($"Error sending message to Event Grid: {resp.Content.ToString()}");
}
break;
} else if (_eventGridSchema == "eventgrid")
} else if (_eventGridSchema == EventGridSchema.EventGrid)
{
EventGridEvent egEvent = new EventGridEvent(evt.QueryId, "Drasi.ControlEvent", "1", evt);
var resp = await _publisherClient.SendEventAsync(egEvent);
Expand Down Expand Up @@ -84,7 +84,7 @@ public async Task HandleControlSignal(ControlEvent evt, object? queryConfig)
}
};

if (_eventGridSchema == "eventgrid")
if (_eventGridSchema == EventGridSchema.EventGrid)
{
EventGridEvent egEvent = new EventGridEvent(evt.QueryId, "Drasi.ControlSignal", "1", notification);
var resp = await _publisherClient.SendEventAsync(egEvent);
Expand All @@ -94,7 +94,7 @@ public async Task HandleControlSignal(ControlEvent evt, object? queryConfig)
throw new Exception($"Error sending message to Event Grid: {resp.Content.ToString()}");
}
break;
} else if (_eventGridSchema == "cloudevents")
} else if (_eventGridSchema == EventGridSchema.CloudEvents)
{
CloudEvent unpackedEvent = new CloudEvent(evt.QueryId, "Drasi.ControlSignal", notification);
var dzresp = await _publisherClient.SendEventAsync(unpackedEvent);
Expand Down

0 comments on commit 5a8e957

Please sign in to comment.