|
| 1 | +// Copyright (c) Cloud Native Foundation. |
| 2 | +// Licensed under the Apache 2.0 license. |
| 3 | +// See LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +using CloudNative.CloudEvents; |
| 6 | +using CloudNative.CloudEvents.Http; |
| 7 | +using CloudNative.CloudEvents.SystemTextJson; |
| 8 | +using CloudNative.CloudEvents.AspNetCore; |
| 9 | +using System.Text.Json.Serialization; |
| 10 | +using System.Text.Json; |
| 11 | +using System.Text; |
| 12 | + |
| 13 | +var builder = WebApplication.CreateBuilder(args); |
| 14 | +var app = builder.Build(); |
| 15 | +var formatter = new JsonEventFormatter<Message>(MyJsonContext.Default); |
| 16 | + |
| 17 | +app.MapPost("/api/events/receive/", async (HttpRequest request) => |
| 18 | +{ |
| 19 | + var cloudEvent = await request.ToCloudEventAsync(formatter); |
| 20 | + using var ms = new MemoryStream(); |
| 21 | + using var writer = new Utf8JsonWriter(ms, new() { Indented = true }); |
| 22 | + writer.WriteStartObject(); |
| 23 | + foreach (var (attribute, value) in cloudEvent.GetPopulatedAttributes()) |
| 24 | + writer.WriteString(attribute.Name, attribute.Format(value)); |
| 25 | + writer.WriteEndObject(); |
| 26 | + await writer.FlushAsync(); |
| 27 | + var attributeMap = Encoding.UTF8.GetString(ms.ToArray()); |
| 28 | + return Results.Text($"Received event with ID {cloudEvent.Id}, attributes: {attributeMap}"); |
| 29 | +}); |
| 30 | + |
| 31 | +app.MapPost("/api/events/receive2/", (Event e) => Results.Json(e.CloudEvent.Data, MyJsonContext.Default)); |
| 32 | + |
| 33 | +app.MapPost("/api/events/receive3/", (Message message) => Results.Json(message, MyJsonContext.Default)); |
| 34 | + |
| 35 | +app.MapGet("/api/events/generate/", () => |
| 36 | +{ |
| 37 | + var evt = new CloudEvent |
| 38 | + { |
| 39 | + Type = "CloudNative.CloudEvents.MinApiSample", |
| 40 | + Source = new Uri("https://github.com/cloudevents/sdk-csharp"), |
| 41 | + Time = DateTimeOffset.Now, |
| 42 | + DataContentType = "application/json", |
| 43 | + Id = Guid.NewGuid().ToString(), |
| 44 | + Data = new Message("C#", Environment.Version.ToString()) |
| 45 | + }; |
| 46 | + // Format the event as the body of the response. This is UTF-8 JSON because of |
| 47 | + // the CloudEventFormatter we're using, but EncodeStructuredModeMessage always |
| 48 | + // returns binary data. We could return the data directly, but for debugging |
| 49 | + // purposes it's useful to have the JSON string. |
| 50 | + var bytes = formatter.EncodeStructuredModeMessage(evt, out var contentType); |
| 51 | + string json = Encoding.UTF8.GetString(bytes.Span); |
| 52 | + // Specify the content type of the response: this is what makes it a CloudEvent. |
| 53 | + // (In "binary mode", the content type is the content type of the data, and headers |
| 54 | + // indicate that it's a CloudEvent.) |
| 55 | + return Results.Content(json, contentType.MediaType, Encoding.UTF8); |
| 56 | +}); |
| 57 | + |
| 58 | +app.Run(); |
| 59 | + |
| 60 | +[JsonSerializable(typeof(Message))] |
| 61 | +internal partial class MyJsonContext : JsonSerializerContext { } |
| 62 | + |
| 63 | +public class Event |
| 64 | +{ |
| 65 | + private readonly static JsonEventFormatter formatter = new JsonEventFormatter<Message>(MyJsonContext.Default); |
| 66 | + // required for receive2 |
| 67 | + public static async ValueTask<Event?> BindAsync(HttpContext context) |
| 68 | + { |
| 69 | + var cloudEvent = await context.Request.ToCloudEventAsync(formatter); |
| 70 | + return new Event { CloudEvent = cloudEvent }; |
| 71 | + } |
| 72 | + public required CloudEvent CloudEvent { get; init; } |
| 73 | +} |
| 74 | + |
| 75 | +record class Message(string Language, string EnvironmentVersion) |
| 76 | +{ |
| 77 | + private readonly static JsonEventFormatter formatter = new JsonEventFormatter<Message>(MyJsonContext.Default); |
| 78 | + // required for receive3 |
| 79 | + public static async ValueTask<Message?> BindAsync(HttpContext context) |
| 80 | + { |
| 81 | + var cloudEvent = await context.Request.ToCloudEventAsync(formatter); |
| 82 | + return cloudEvent.Data is Message message ? message : null; |
| 83 | + } |
| 84 | +} |
| 85 | + |
0 commit comments