public async Task CanPublishCloudEventToDomain() { EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.CloudEventDomainHost), new AzureKeyCredential(TestEnvironment.CloudEventDomainKey), options)); #region Snippet:CloudNativePublishToDomain CloudEvent cloudEvent = new CloudEvent { Type = "record", // Event Grid does not allow absolute URIs as the domain topic Source = new Uri("test", UriKind.Relative), #if SNIPPET Id = "eventId", Time = DateTimeOffset.Now, #else Id = Recording.Random.NewGuid().ToString(), Time = Recording.Now, #endif Data = new TestPayload("name", 0) }; await client.SendCloudNativeCloudEventAsync(cloudEvent); #endregion }
public async Task AuthenticateWithSasToken() { string topicEndpoint = TestEnvironment.TopicHost; string topicAccessKey = TestEnvironment.TopicKey; // Create the publisher client using an AzureKeyCredential // Custom topic should be configured to accept events of the Event Grid schema #region Snippet:GenerateSas var builder = new EventGridSasBuilder(new Uri(topicEndpoint), DateTimeOffset.Now.AddHours(1)); var keyCredential = new AzureKeyCredential(topicAccessKey); string sasToken = builder.GenerateSas(keyCredential); #endregion #region Snippet:AuthenticateWithSas var sasCredential = new AzureSasCredential(sasToken); EventGridPublisherClient client = new EventGridPublisherClient( new Uri(topicEndpoint), sasCredential); #endregion // Add EventGridEvents to a list to publish to the topic List <EventGridEvent> eventsList = new List <EventGridEvent> { new EventGridEvent( "ExampleEventSubject", "Example.EventType", "1.0", "This is the event data") }; // Send the events await client.SendEventsAsync(eventsList); }
public async Task CanPublishCloudEventWithExtensionAttributes() { EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.CloudEventTopicHost), new AzureKeyCredential(TestEnvironment.CloudEventTopicKey), options)); List <CloudEvent> eventsList = new List <CloudEvent>(); for (int i = 0; i < 10; i++) { CloudEvent cloudEvent = new CloudEvent( "record", "Microsoft.MockPublisher.TestEvent") { Id = Recording.Random.NewGuid().ToString(), Subject = $"Subject-{i}", Time = Recording.Now }; cloudEvent.Data = "hello"; cloudEvent.ExtensionAttributes.Add("testattribute1", "test"); cloudEvent.ExtensionAttributes.Add("testattribute2", new TestPayload("name", i)); eventsList.Add(cloudEvent); } await client.PublishCloudEventsAsync(eventsList); }
public async Task CustomizeSerializedJSONPropertiesToCamelCase() { EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions()); var serializer = new JsonObjectSerializer( new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.CustomEventTopicHost), new AzureKeyCredential(TestEnvironment.CustomEventTopicKey), options)); List <BinaryData> eventsList = new List <BinaryData>(); for (int i = 0; i < 10; i++) { eventsList.Add(serializer.Serialize( new TestEvent() { DataVersion = "1.0", EventTime = Recording.Now, EventType = "Microsoft.MockPublisher.TestEvent", Id = Recording.Random.NewGuid().ToString(), Subject = $"Subject-{i}", Topic = $"Topic-{i}" })); } await client.SendEventsAsync(eventsList); }
public async Task CanPublishCloudEventToDomain() { EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.CloudEventDomainHost), new AzureKeyCredential(TestEnvironment.CloudEventDomainKey), options)); #region Snippet:SendCloudEventsToDomain List <CloudEvent> eventsList = new List <CloudEvent>(); for (int i = 0; i < 10; i++) { CloudEvent cloudEvent = new CloudEvent( // the source is mapped to the domain topic $"Subject-{i}", "Microsoft.MockPublisher.TestEvent", "hello") { #if SNIPPET Id = $"event-{i}", Time = DateTimeOffset.Now #else Id = Recording.Random.NewGuid().ToString(), Time = Recording.Now #endif }; eventsList.Add(cloudEvent); } await client.SendEventsAsync(eventsList); #endregion }
public async Task CanPublishCloudEventWithCustomObjectPayloadAndCustomSerializer() { EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.CloudEventTopicHost), new AzureKeyCredential(TestEnvironment.CloudEventTopicKey), options)); var serializer = new JsonObjectSerializer( new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); List <CloudEvent> eventsList = new List <CloudEvent>(); for (int i = 0; i < 10; i++) { CloudEvent cloudEvent = new CloudEvent( "record", "Microsoft.MockPublisher.TestEvent", serializer.Serialize(new TestPayload("name", i)), "application/json", dataFormat: CloudEventDataFormat.Json) { Id = Recording.Random.NewGuid().ToString(), Subject = $"Subject-{i}", Time = Recording.Now }; eventsList.Add(cloudEvent); } await client.SendEventsAsync(eventsList); }
public async Task SerializesExpectedProperties_BaseType() { var mockTransport = CreateMockTransport(); var options = new EventGridPublisherClientOptions { Transport = mockTransport }; EventGridPublisherClient client = new EventGridPublisherClient( new Uri("http://localHost"), new AzureKeyCredential("fakeKey"), options); var cloudEvent = new CloudEvent( "record", "Microsoft.MockPublisher.TestEvent", new DerivedTestPayload { Name = "name", Age = 10, DerivedProperty = 5 }, typeof(TestPayload)); Assert.IsNull(cloudEvent.Data.ToObjectFromJson <DerivedTestPayload>().DerivedProperty); List <CloudEvent> eventsList = new List <CloudEvent>() { cloudEvent }; await client.SendEventsAsync(eventsList); Assert.IsNull(cloudEvent.Data.ToObjectFromJson <DerivedTestPayload>().DerivedProperty); }
public async Task CanPublishEventToDomain() { EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.DomainHost), new AzureKeyCredential(TestEnvironment.DomainKey), options)); List <EventGridEvent> eventsList = new List <EventGridEvent>(); for (int i = 0; i < 10; i++) { EventGridEvent newEGEvent = new EventGridEvent( $"Subject-{i}", "hello", "Microsoft.MockPublisher.TestEvent", "1.0") { Id = Recording.Random.NewGuid().ToString(), EventTime = Recording.Now }; newEGEvent.Topic = $"Topic-{i}"; eventsList.Add(newEGEvent); } await client.PublishEventsAsync(eventsList); }
public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ExecutionContext context, ILogger log) { log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json") .AddUserSecrets <Configuration>() .Build(); var client = new EventGridPublisherClient( new Uri(config["EventGridUrl"]), new AzureKeyCredential(config["EventGridKey"])); // Add EventGridEvents to a list to publish to the topic EventGridEvent egEvent = new EventGridEvent( "ExampleEventSubject", "Example.EventType", "1.0", "This is the event data"); // Send the event client.SendEvent(egEvent); }
public async Task CanPublishEventWithCustomObjectPayload() { EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.TopicHost), new AzureKeyCredential(TestEnvironment.TopicKey), options)); List <EventGridEvent> eventsList = new List <EventGridEvent>(); for (int i = 0; i < 10; i++) { eventsList.Add( new EventGridEvent( $"Subject-{i}", new TestPayload("name", i), "Microsoft.MockPublisher.TestEvent", "1.0") { Id = Recording.Random.NewGuid().ToString(), EventTime = Recording.Now }); } await client.PublishEventsAsync(eventsList); }
public async Task SendAsync(T message, MetaData metaData, CancellationToken cancellationToken = default) { EventGridPublisherClient client = new EventGridPublisherClient(new Uri(_domainEndpoint), new AzureKeyCredential(_domainKey)); var data = new BinaryData(JsonSerializer.Serialize(new Message <T> { Data = message, MetaData = metaData, })); var events = new List <EventGridEvent>() { new EventGridEvent("TEST", typeof(T).FullName, "1.0", data) { Id = Guid.NewGuid().ToString(), EventType = typeof(T).FullName, Topic = _topic, EventTime = DateTime.UtcNow, Subject = "TEST", DataVersion = "1.0", }, }; await client.SendEventsAsync(events, cancellationToken); }
public async Task SendEventGridEventsToTopic() { string topicEndpoint = TestEnvironment.TopicHost; string topicAccessKey = TestEnvironment.TopicKey; // Create the publisher client using an AzureKeyCredential // Custom topic should be configured to accept events of the Event Grid schema #region Snippet:CreateClient EventGridPublisherClient client = new EventGridPublisherClient( new Uri(topicEndpoint), new AzureKeyCredential(topicAccessKey)); #endregion #region Snippet:SendEGEventsToTopic // Add EventGridEvents to a list to publish to the topic List <EventGridEvent> eventsList = new List <EventGridEvent> { new EventGridEvent( "This is the event data", "ExampleEventSubject", "Example.EventType", "1.0") }; // Send the events await client.SendEventsAsync(eventsList); #endregion }
public async Task SerializesExpectedProperties_DerivedType() { var mockTransport = new MockTransport(new MockResponse(200)); var options = new EventGridPublisherClientOptions { Transport = mockTransport }; EventGridPublisherClient client = new EventGridPublisherClient( new Uri("http://localHost"), new AzureKeyCredential("fakeKey"), options); var egEvent = new EventGridEvent( "record", "Microsoft.MockPublisher.TestEvent", "TestPayload", new DerivedTestPayload { Name = "name", Age = 10, DerivedProperty = 5 }); Assert.AreEqual(5, egEvent.Data.ToObjectFromJson <DerivedTestPayload>().DerivedProperty); List <EventGridEvent> eventsList = new List <EventGridEvent>() { egEvent }; await client.SendEventsAsync(eventsList); egEvent = DeserializeRequest(mockTransport.SingleRequest).First(); Assert.AreEqual(5, egEvent.Data.ToObjectFromJson <DerivedTestPayload>().DerivedProperty); }
public async Task CanPublishCloudEvent() { EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.CloudEventTopicHost), new AzureKeyCredential(TestEnvironment.CloudEventTopicKey), options)); List <CloudEvent> eventsList = new List <CloudEvent>(); for (int i = 0; i < 10; i++) { eventsList.Add( new CloudEvent( "record", new Uri("http://localHost"), Recording.Random.NewGuid().ToString(), Recording.Now.DateTime) { Data = new TestPayload("name", i) } ); } await client.SendCloudEventsAsync(eventsList); }
public async Task RespectsPortFromUriSendingEventGridEvents() { var mockTransport = new MockTransport((request) => { Assert.AreEqual(100, request.Uri.Port); return(new MockResponse(200)); }); var options = new EventGridPublisherClientOptions { Transport = mockTransport }; EventGridPublisherClient client = new EventGridPublisherClient( new Uri("https://contoso.com:100/api/events"), new AzureKeyCredential("fakeKey"), options); var egEvent = new EventGridEvent( "record", "Microsoft.MockPublisher.TestEvent", "TestPayload", new TestPayload { Name = "name", Age = 10, }); List <EventGridEvent> eventsList = new List <EventGridEvent>() { egEvent }; await client.SendEventsAsync(eventsList); }
public async Task CanPublishCloudEventWithRawJsonData() { EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.CloudEventTopicHost), new AzureKeyCredential(TestEnvironment.CloudEventTopicKey), options)); List <CloudEvent> eventsList = new List <CloudEvent>(); for (int i = 0; i < 10; i++) { CloudEvent cloudEvent = new CloudEvent( "record", "Microsoft.MockPublisher.TestEvent", JsonDocument.Parse("{\"property1\": \"abc\", \"property2\": 123}").RootElement) { Id = Recording.Random.NewGuid().ToString(), Subject = $"Subject-{i}", Time = Recording.Now }; eventsList.Add(cloudEvent); } await client.SendEventsAsync(eventsList); }
public async Task CanPublishEventWithNonJsonSerializedBinaryData() { EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.TopicHost), new AzureKeyCredential(TestEnvironment.TopicKey), options)); List <EventGridEvent> eventsList = new List <EventGridEvent>(); for (int i = 0; i < 10; i++) { eventsList.Add( new EventGridEvent( new BinaryData("data"), $"Subject-{i}", "Microsoft.MockPublisher.TestEvent", "1.0") { Id = Recording.Random.NewGuid().ToString(), EventTime = Recording.Now }); } await client.SendEventsAsync(eventsList); }
public async Task DoesNotSetCredentialForSystemPublisher() { var mockTransport = new MockTransport((request) => { Assert.IsFalse(request.Headers.TryGetValue(Constants.SasKeyName, out var _)); return(new MockResponse(200)); }); var options = new EventGridPublisherClientOptions { Transport = mockTransport }; EventGridPublisherClient client = new EventGridPublisherClient( new Uri("https://contoso.com:100/api/events"), new AzureKeyCredential(EventGridKeyCredentialPolicy.SystemPublisherKey), options); var cloudEvent = new CloudEvent( "record", "Microsoft.MockPublisher.TestEvent", null); List <CloudEvent> eventsList = new List <CloudEvent>() { cloudEvent }; await client.SendEventsAsync(eventsList); }
public async Task SendEventsToDomain() { string domainEndpoint = TestEnvironment.DomainHost; string domainAccessKey = TestEnvironment.DomainKey; #region Snippet:CreateDomainClient // Create the publisher client using an AzureKeyCredential // Domain should be configured to accept events of the Event Grid schema EventGridPublisherClient client = new EventGridPublisherClient( new Uri(domainEndpoint), new AzureKeyCredential(domainAccessKey)); #endregion #region Snippet:SendEventsToDomain // Add EventGridEvents to a list to publish to the domain // Don't forget to specify the topic you want the event to be delivered to! List <EventGridEvent> eventsList = new List <EventGridEvent> { new EventGridEvent( "This is the event data", "ExampleEventSubject", "Example.EventType", "1.0") { Topic = "MyTopic" } }; // Send the events await client.SendEventsAsync(eventsList); #endregion }
public async Task CanPublishCloudEvent() { EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.CloudEventTopicHost), new AzureKeyCredential(TestEnvironment.CloudEventTopicKey), options)); List <CloudEvent> eventsList = new List <CloudEvent>(); for (int i = 0; i < 10; i++) { eventsList.Add( new CloudEvent( "record", "Microsoft.MockPublisher.TestEvent") { Id = Recording.Random.NewGuid().ToString(), Subject = $"Subject-{i}", Time = Recording.Now }); } await client.PublishCloudEventsAsync(eventsList); }
public async Task SendCloudEventsToTopic() { string topicEndpoint = TestEnvironment.CloudEventTopicHost; string topicAccessKey = TestEnvironment.CloudEventTopicKey; // Create the publisher client using an AzureKeyCredential // Custom topic should be configured to accept events of the CloudEvents 1.0 schema #region Snippet:CreateClientWithOptions EventGridPublisherClient client = new EventGridPublisherClient( new Uri(topicEndpoint), new AzureKeyCredential(topicAccessKey)); #endregion #region Snippet:SendCloudEventsToTopic // Example of a custom ObjectSerializer used to serialize the event payload to JSON var myCustomDataSerializer = new JsonObjectSerializer( new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); // Add CloudEvents to a list to publish to the topic List <CloudEvent> eventsList = new List <CloudEvent> { // CloudEvent with custom model serialized to JSON new CloudEvent( "/cloudevents/example/source", "Example.EventType", new CustomModel() { A = 5, B = true }), // CloudEvent with custom model serialized to JSON using a custom serializer new CloudEvent( "/cloudevents/example/source", "Example.EventType", myCustomDataSerializer.Serialize(new CustomModel() { A = 5, B = true }), "application/json"), // CloudEvents also supports sending binary-valued data new CloudEvent( "/cloudevents/example/binarydata", "Example.EventType", new BinaryData(Encoding.UTF8.GetBytes("This is treated as binary data")), "application/octet-stream") }; // Send the events await client.SendEventsAsync(eventsList); #endregion }
public async Task CanPublishCloudEventWithBinaryData() { EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.CloudEventTopicHost), new AzureKeyCredential(TestEnvironment.CloudEventTopicKey), options)); List <CloudEvent> eventsList = new List <CloudEvent>(); for (int i = 0; i < 5; i++) { CloudEvent cloudEvent = new CloudEvent( "record", "Microsoft.MockPublisher.TestEvent") { Id = Recording.Random.NewGuid().ToString(), Subject = $"Subject-{i}", Time = Recording.Now }; // testing byte[] cloudEvent.Data = Encoding.UTF8.GetBytes("data"); eventsList.Add(cloudEvent); } for (int i = 0; i < 5; i++) { CloudEvent cloudEvent = new CloudEvent( "record", "Microsoft.MockPublisher.TestEvent") { Id = Recording.Random.NewGuid().ToString(), Subject = $"Subject-{i}", Time = Recording.Now }; // testing ReadOnlyMemory<byte> cloudEvent.Data = new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes("data")); eventsList.Add(cloudEvent); } for (int i = 0; i < 5; i++) { CloudEvent cloudEvent = new CloudEvent( "record", "Microsoft.MockPublisher.TestEvent") { Id = Recording.Random.NewGuid().ToString(), Subject = $"Subject-{i}", Time = Recording.Now }; // testing IEnumerable<byte> cloudEvent.Data = Enumerable.Repeat((byte)1, 1); eventsList.Add(cloudEvent); } await client.PublishCloudEventsAsync(eventsList); }
public async Task CanPublishEvent() { EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.TopicHost), new AzureKeyCredential(TestEnvironment.TopicKey), options)); await client.SendEventsAsync(GetEventsList()); }
public async Task CanPublishCustomEvent() { EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions()); EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.CustomEventTopicHost), new AzureKeyCredential(TestEnvironment.CustomEventTopicKey), options)); await client.PublishCustomEventsAsync(GetCustomEventsList()); }
public async Task GivenValidEvent_WhenUriContainsNonStandardPort_ThenItShouldBeAccepted() { var client = new EventGridPublisherClient( new Uri("https://localhost:60101/api/events"), new AzureKeyCredential("TheLocal+DevelopmentKey=")); var response = await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", new { Id = 1, Foo = "Bar" })); response.Status.ShouldBe((int)HttpStatusCode.OK); }
public HomeController( ILogger <HomeController> logger, BlobServiceClient blobServiceClient, IAzureClientFactory <EventHubProducerClient> clientFactory, EventGridPublisherClient publisherClient) { _logger = logger; _blobContainerClient = blobServiceClient.GetBlobContainerClient("uploads"); _uploadsProducer = clientFactory.CreateClient("Uploads"); _publisherClient = publisherClient; }
public ITopic Create(string name) { if (name == "mock") { return(new MockTopic()); } EventGridPublisherClient client = _clientFactory.CreateClient(name); return(new EventGridTopic(client)); }
public void CannotPublishCustomEventMissingApiEventsPathFromUri() { EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions()); Uri host = new UriBuilder("https", new Uri(TestEnvironment.TopicHost).Host).Uri; EventGridPublisherClient client = InstrumentClient( new EventGridPublisherClient( host, new AzureKeyCredential(TestEnvironment.TopicKey), options)); Assert.ThrowsAsync <RequestFailedException>(async() => await client.SendEventAsync(new BinaryData(jsonSerializable: "data"))); }
public void CannotPublishCustomEventWithNonJsonData() { EventGridPublisherClient client = new EventGridPublisherClient( new Uri("http://localHost"), new AzureKeyCredential("fakeKey")); var customEvent = new BinaryData(new byte[] { 1, 2, 3, 4 }); Assert.That( async() => await client.SendEventAsync(customEvent), Throws.InstanceOf <JsonException>()); }
public async Task CanPublishEventUsingSAS() { string sasToken = EventGridPublisherClient.BuildSharedAccessSignature( new Uri(TestEnvironment.TopicHost), DateTimeOffset.UtcNow.AddMinutes(60), new AzureKeyCredential(TestEnvironment.TopicKey)); EventGridPublisherClient sasTokenClient = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.TopicHost), new EventGridSharedAccessSignatureCredential(sasToken), Recording.InstrumentClientOptions(new EventGridPublisherClientOptions()))); await sasTokenClient.PublishEventsAsync(GetEventsList()); }