forked from cloudevents/sdk-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloudEvent.cs
224 lines (204 loc) · 10.2 KB
/
CloudEvent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (c) Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.
namespace CloudNative.CloudEvents
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Mime;
/// <summary>
/// Represents a CloudEvent
/// </summary>
public class CloudEvent
{
public const string MediaType = "application/cloudevents";
readonly CloudEventAttributes attributes;
/// <summary>
/// Create a new CloudEvent instance.
/// </summary>
/// <param name="type">'type' of the CloudEvent</param>
/// <param name="source">'source' of the CloudEvent</param>
/// <param name="id">'id' of the CloudEvent</param>
/// <param name="time">'time' of the CloudEvent</param>
/// <param name="extensions">Extensions to be added to this CloudEvents</param>
public CloudEvent(string type, Uri source, string id = null, DateTime? time = null,
params ICloudEventExtension[] extensions) : this(CloudEventsSpecVersion.Default, type, source, id, time, extensions)
{
}
/// <summary>
/// Create a new CloudEvent instance.
/// </summary>
/// <param name="specVersion">CloudEvents specification version</param>
/// <param name="type">'type' of the CloudEvent</param>
/// <param name="source">'source' of the CloudEvent</param>
/// <param name="id">'id' of the CloudEvent</param>
/// <param name="time">'time' of the CloudEvent</param>
/// <param name="extensions">Extensions to be added to this CloudEvents</param>
public CloudEvent(CloudEventsSpecVersion specVersion, string type, Uri source, string id = null, DateTime? time = null,
params ICloudEventExtension[] extensions) : this(specVersion, extensions)
{
Type = type;
Source = source;
Id = id ?? Guid.NewGuid().ToString();
Time = time ?? DateTime.UtcNow;
}
/// <summary>
/// Create a new CloudEvent instance.
/// </summary>
/// <param name="specVersion">CloudEvents specification version</param>
/// <param name="type">'type' of the CloudEvent</param>
/// <param name="source">'source' of the CloudEvent</param>
/// <param name="subject">'subject' of the CloudEvent</param>
/// <param name="id">'id' of the CloudEvent</param>
/// <param name="time">'time' of the CloudEvent</param>
/// <param name="extensions">Extensions to be added to this CloudEvents</param>
public CloudEvent(CloudEventsSpecVersion specVersion, string type, Uri source, string subject, string id = null, DateTime? time = null,
params ICloudEventExtension[] extensions) : this(specVersion, type, source, id, time, extensions)
{
Subject = subject;
}
/// <summary>
/// Create a new CloudEvent instance
/// </summary>
/// <param name="specVersion">CloudEvents specification version</param>
/// <param name="extensions">Extensions to be added to this CloudEvents</param>
public CloudEvent(CloudEventsSpecVersion specVersion, IEnumerable<ICloudEventExtension> extensions)
{
attributes = new CloudEventAttributes(specVersion, extensions);
var extensionMap = new Dictionary<Type, ICloudEventExtension>();
if (extensions != null)
{
foreach (var extension in extensions)
{
extensionMap.Add(extension.GetType(), extension);
extension.Attach(this);
}
}
Extensions = new ReadOnlyDictionary<Type, ICloudEventExtension>(extensionMap);
}
/// <summary>
/// CloudEvent 'datacontenttype' attribute. Content type of the 'data' attribute value.
/// This attribute enables the data attribute to carry any type of content, whereby
/// format and encoding might differ from that of the chosen event format.
/// </summary>
/// <see cref="https://github.com/cloudevents/spec/blob/master/spec.md#contenttype"/>
public ContentType DataContentType
{
get => attributes[CloudEventAttributes.DataContentTypeAttributeName(attributes.SpecVersion)] as ContentType;
set => attributes[CloudEventAttributes.DataContentTypeAttributeName(attributes.SpecVersion)] = value;
}
/// <summary>
/// CloudEvent 'data' content. The event payload. The payload depends on the type
/// and the 'schemaurl'. It is encoded into a media format which is specified by the
/// 'contenttype' attribute (e.g. application/json).
/// </summary>
/// <see cref="https://github.com/cloudevents/spec/blob/master/spec.md#data-1"/>
public object Data
{
get => attributes[CloudEventAttributes.DataAttributeName(attributes.SpecVersion)];
set => attributes[CloudEventAttributes.DataAttributeName(attributes.SpecVersion)] = value;
}
/// <summary>
/// Extensions registered with this event.
/// </summary>
public IReadOnlyDictionary<Type, ICloudEventExtension> Extensions { get; private set; }
/// <summary>
/// CloudEvent 'id' attribute. ID of the event. The semantics of this string are explicitly
/// undefined to ease the implementation of producers. Enables deduplication.
/// </summary>
/// <see cref="https://github.com/cloudevents/spec/blob/master/spec.md#id"/>
public string Id
{
get => attributes[CloudEventAttributes.IdAttributeName(attributes.SpecVersion)] as string;
set => attributes[CloudEventAttributes.IdAttributeName(attributes.SpecVersion)] = value;
}
/// <summary>
/// CloudEvents 'dataschema' attribute. A link to the schema that the data attribute
/// adheres to. Incompatible changes to the schema SHOULD be reflected by a
/// different URI.
/// </summary>
/// <see cref="https://github.com/cloudevents/spec/blob/master/spec.md#dataschema"/>
public Uri DataSchema
{
get => attributes[CloudEventAttributes.DataSchemaAttributeName(attributes.SpecVersion)] as Uri;
set => attributes[CloudEventAttributes.DataSchemaAttributeName(attributes.SpecVersion)] = value;
}
/// <summary>
/// CloudEvents 'source' attribute. This describes the event producer. Often this
/// will include information such as the type of the event source, the
/// organization publishing the event, the process that produced the
/// event, and some unique identifiers.
/// </summary>
/// <see cref="https://github.com/cloudevents/spec/blob/master/spec.md#source"/>
public Uri Source
{
get => attributes[CloudEventAttributes.SourceAttributeName(attributes.SpecVersion)] as Uri;
set => attributes[CloudEventAttributes.SourceAttributeName(attributes.SpecVersion)] = value;
}
/// <summary>
/// CloudEvents 'specversion' attribute. The version of the CloudEvents
/// specification which the event uses. This enables the interpretation of the context.
/// </summary>
/// <see cref="https://github.com/cloudevents/spec/blob/master/spec.md#specversion"/>
public CloudEventsSpecVersion SpecVersion
{
get => attributes.SpecVersion;
set => attributes.SpecVersion = value;
}
/// <summary>
/// CloudEvents 'subject' attribute. This describes the subject of the event in the context
/// of the event producer (identified by source). In publish-subscribe scenarios, a subscriber
/// will typically subscribe to events emitted by a source, but the source identifier alone
/// might not be sufficient as a qualifier for any specific event if the source context has
/// internal sub-structure.
/// </summary>
/// <see cref="https://github.com/cloudevents/spec/blob/master/spec.md#subject"/>
public string Subject
{
get => attributes[CloudEventAttributes.SubjectAttributeName(attributes.SpecVersion)] as string;
set => attributes[CloudEventAttributes.SubjectAttributeName(attributes.SpecVersion)] = value;
}
/// <summary>
/// CloudEvents 'time' attribute. Timestamp of when the event happened.
/// </summary>
/// <see cref="https://github.com/cloudevents/spec/blob/master/spec.md#time"/>
public DateTime? Time
{
get => (DateTime?)attributes[CloudEventAttributes.TimeAttributeName(attributes.SpecVersion)];
set => attributes[CloudEventAttributes.TimeAttributeName(attributes.SpecVersion)] = value;
}
/// <summary>
/// CloudEvents 'type' attribute. Type of occurrence which has happened.
/// Often this attribute is used for routing, observability, policy enforcement, etc.
/// </summary>
/// <see cref="https://github.com/cloudevents/spec/blob/master/spec.md#type"/>
public string Type
{
get => attributes[CloudEventAttributes.TypeAttributeName(attributes.SpecVersion)] as string;
set => attributes[CloudEventAttributes.TypeAttributeName(attributes.SpecVersion)] = value;
}
/// <summary>
/// Use this method to access extensions added to this event.
/// </summary>
/// <typeparam name="T">Type of the extension class</typeparam>
/// <returns>Extension instance if registered</returns>
public T Extension<T>()
{
var key = typeof(T);
if (Extensions.TryGetValue(key, out var extension))
{
return (T)extension;
}
return default(T);
}
/// <summary>
/// Provides direct access to the attribute collection.
/// </summary>
/// <returns>Attribute collection</returns>
public IDictionary<string, object> GetAttributes()
{
return attributes;
}
}
}