Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing event grid client package #16

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ You can specify all necessary event details you need.</Description>
<PackageIcon>icon-logo.png</PackageIcon>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.EventGrid" Version="3.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.5" />
</ItemGroup>
<ItemGroup>
Expand Down
44 changes: 25 additions & 19 deletions AzureFunctions.EventGrid/EventGridAsyncCollector.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.EventGrid;
using Microsoft.Azure.EventGrid.Models;
using Microsoft.Azure.WebJobs;
using Newtonsoft.Json;

namespace AzureFunctions.EventGrid
{
public class EventGridAsyncCollector : IAsyncCollector<Event>
{
private readonly string topicHostname;

private readonly string topicEndPoint;
private readonly string topicKey;

private readonly IList<Event> eventCollection;
private readonly TopicCredentials topicCredentials;
private readonly EventGridAttribute attribute;

private static HttpClient httpClient;


public EventGridAsyncCollector(EventGridAttribute attribute)
{
this.eventCollection = new List<Event>();
string topicEndpoint = attribute.TopicEndpoint;
string topicKey = attribute.TopicKey;
this.topicEndPoint = attribute.TopicEndpoint;
this.topicKey = attribute.TopicKey;
this.attribute = attribute;

this.topicHostname = new Uri(topicEndpoint).Host;
this.topicCredentials = new TopicCredentials(topicKey);
}

/// <summary>
Expand All @@ -34,6 +38,11 @@ public EventGridAsyncCollector(EventGridAttribute attribute)
/// <returns><see cref="Task.CompletedTask"/>.</returns>
public Task AddAsync(Event item, CancellationToken cancellationToken = new CancellationToken())
{
if (httpClient == null)
{
httpClient = attribute.HttpClient ?? new HttpClient();
}

this.eventCollection.Add(item);

return Task.CompletedTask;
Expand All @@ -48,22 +57,19 @@ public EventGridAsyncCollector(EventGridAttribute attribute)
var eventGridEventCollection = CreateEventGridEventCollection();
if (eventGridEventCollection.Any())
{
using (var eventGridClient = new EventGridClient(topicCredentials))
{
await eventGridClient.PublishEventsAsync(
topicHostname,
eventGridEventCollection,
cancellationToken);
}
HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(eventGridEventCollection), Encoding.UTF8, "application/json");
httpContent.Headers.Add("aeg-sas-key", topicKey);
await httpClient.PostAsync(topicEndPoint, httpContent, cancellationToken);
}
}

private IList<EventGridEvent> CreateEventGridEventCollection()
private IList<EventGridModel> CreateEventGridEventCollection()
{
var eventGridEventCollection = new List<EventGridEvent>();
var eventGridEventCollection = new List<EventGridModel>();
foreach (var @event in eventCollection)
{
var eventGridEvent = new EventGridEvent(

var eventGridEvent = new EventGridModel(
id: Guid.NewGuid().ToString("N"),
subject: @event.Subject,
dataVersion: @event.DataVersion,
Expand Down
6 changes: 6 additions & 0 deletions AzureFunctions.EventGrid/EventGridAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net.Http;
using Microsoft.Azure.WebJobs.Description;

namespace AzureFunctions.EventGrid
Expand All @@ -20,5 +21,10 @@ public class EventGridAttribute : Attribute
/// </summary>
[AppSetting]
public string TopicKey { get; set; }

/// <summary>
/// If you have already instantiated an <seealso cref="System.Net.Http.HttpClient"/>, pass it to this property.
/// </summary>
public HttpClient HttpClient { get; set; }
}
}
27 changes: 27 additions & 0 deletions AzureFunctions.EventGrid/EventGridModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace AzureFunctions.EventGrid
{
internal class EventGridModel :Event
{
//
// Summary:
// Gets or sets an unique identifier for the event.
public string Id { get; set; }

//
// Summary:
// Gets or sets the time (in UTC) the event was generated.
public DateTime EventTime { get; set; }

public EventGridModel(string id,string subject, string dataVersion, string eventType, object data,DateTime eventTime)
{
this.Id = id;
this.Subject = subject;
this.DataVersion = dataVersion;
this.EventType = eventType;
this.Data = data;
this.EventTime = eventTime;
}
}
}
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,15 @@ private class MyCustomEvent
```

The publishing of the events will be executed after the Azure Function is finished,
in the `FlushAsync` method of the `IAsyncCollector<T>`.
in the `FlushAsync` method of the `IAsyncCollector<T>`.

In order for this to work you need a `local.settings.json` file with the following values. For `EventGridBindingSampleTopicEndpoint` setting, you have to provide the endpoint along with the [api-version](https://docs.microsoft.com/en-us/rest/api/eventgrid/dataplane/publishevents/publishevents) header.

```
{
"Values": {
"EventGridBindingSampleTopicEndpoint": "https://{topicHostname}/api/events?api-version=2018-01-01",
"EventGridBindingSampleTopicKey": "{Topic Key}"
}
}
```