public async Task <IActionResult> PostAsync([FromBody] ToDo payload) { var contentType = "application/json"; var ce = CloudEventFactory.Create(contentType, payload); ce.EventType = "org.aliencube.ToDos.OnToDoCreated"; ce.EventTypeVersion = "1.0"; ce.Source = "/subscriptions/[SUBSCRIPTION_ID]/resourceGroups/[RESOURCE_GROUP_NAME]/providers/microsoft.eventgrid/topics/[TOPIC_NAME]#[EVENT_SUBJECT_NAME]"; ce.EventId = Guid.NewGuid().ToString(); ce.EventTime = DateTimeOffset.UtcNow; ce.ContentType = "application/json"; var requestUri = "[EVENT_GRID_TOPIC_ENDPOINT_URL]"; using (var client = new HttpClient()) using (var content = CloudEventContentFactory.Create(ce)) { content.Headers.Add("AEG-SAS-KEY", "EVENT_GRID_TOPIC_ACCESS_TOKEN"); var response = await client.PostAsync(requestUri, content).ConfigureAwait(false); return(new OkObjectResult(response.ReasonPhrase)); } }
public static void Main(string[] args) { var todo = new ToDo() { Title = "sample todo" }; var contentType = "application/json"; var ce = CloudEventFactory.Create(contentType, todo); ce.EventType = "org.aliencube.ToDos.OnToDoCreated"; ce.EventTypeVersion = "1.0"; ce.Source = (new Uri("http://localhost")).ToString(); ce.EventId = Guid.NewGuid().ToString(); ce.EventTime = DateTimeOffset.UtcNow; var requestUri = "http://localhost:5604/api/events"; using (var client = new HttpClient()) using (var content = CloudEventContentFactory.Create(ce)) { var body = GetResponseAsync(client, requestUri, content).Result; Console.WriteLine(body); } }
public void Given_ContentTypeOfBinary_And_Data_Should_ReturnResult() { var data = new FakeData() { FakeProperty = "hello world" }; var serialised = JsonConvert.SerializeObject(data); var binarified = Encoding.UTF8.GetBytes(serialised); var ev = CloudEventFactory.Create("application/octet-stream", binarified); ev.Should().BeAssignableTo <BinaryEvent>(); }
public void Given_ContentTypeOfObject_And_Data_Should_ReturnResult() { var data = new FakeData() { FakeProperty = "hello world" }; var ev = CloudEventFactory.Create("text/json", data); ev.Should().BeAssignableTo <ObjectEvent <FakeData> >(); ev = CloudEventFactory.Create("application/json", data); ev.Should().BeAssignableTo <ObjectEvent <FakeData> >(); ev = CloudEventFactory.Create("application/json-seq", data); ev.Should().BeAssignableTo <ObjectEvent <FakeData> >(); ev = CloudEventFactory.Create("application/cloudevents+json", data); ev.Should().BeAssignableTo <ObjectEvent <FakeData> >(); ev = CloudEventFactory.Create("application/geo+json-seq", data); ev.Should().BeAssignableTo <ObjectEvent <FakeData> >(); }
public void ParseFromRawNativeEvent_ParseRawEvent_ReturnsRawEvent() { // Arrange var expectedContentType = new ContentType("application/json"); var expectedSource = "http://kubernetes/"; var rawKubernetesEvent = KubernetesEventSamples.GetRawContainerStartedEvent(); var kubernetesEvent = new KubernetesEvent(KubernetesEventType.Raw, rawKubernetesEvent); var mockedKubernetesEventParser = CreateMockedKubernetesEventParser(kubernetesEvent); var cloudEventFactory = new CloudEventFactory(mockedKubernetesEventParser.Object); // Act var cloudEvent = cloudEventFactory.CreateFromRawKubernetesEvent(rawKubernetesEvent); // Assert Assert.NotNull(cloudEvent); Assert.Equal(kubernetesEvent.Type.GetDescription(), cloudEvent.Type); Assert.Equal(kubernetesEvent.Payload, cloudEvent.Data); Assert.Equal(expectedContentType, cloudEvent.DataContentType); Assert.NotNull(cloudEvent.Source); Assert.Equal(expectedSource, cloudEvent.Source.AbsoluteUri); Assert.Equal(CloudEventsSpecVersion.V1_0, cloudEvent.SpecVersion); mockedKubernetesEventParser.Verify(parser => parser.ParseFromRawNativeEvent(It.IsAny <string>()), Times.Once); }
public void Given_ContentTypeOfString_And_Data_Should_ReturnResult() { var ev = CloudEventFactory.Create("text/plain", "hello world"); ev.Should().BeAssignableTo <StringEvent>(); }