-
Notifications
You must be signed in to change notification settings - Fork 368
/
Header.cs
85 lines (69 loc) · 2.42 KB
/
Header.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Text.Json.Serialization;
namespace Microsoft.DotNet.Interactive.Jupyter.Messaging;
public class Header
{
[JsonPropertyName("msg_id")]
public string MessageId { get; }
[JsonPropertyName("username")]
public string Username { get; }
[JsonPropertyName("session")]
public string Session { get; }
[JsonPropertyName("date")]
public string Date { get; }
[JsonPropertyName("msg_type")]
public string MessageType
{
get;
}
[JsonPropertyName("version")]
public string Version { get; }
public Header(string messageType, string messageId, string version, string session, string username, string date = null)
{
if (string.IsNullOrWhiteSpace(messageType))
{
throw new ArgumentException("Value cannot be null or whitespace.", nameof(messageType));
}
if (string.IsNullOrWhiteSpace(messageId))
{
throw new ArgumentException("Value cannot be null or whitespace.", nameof(messageId));
}
if (string.IsNullOrWhiteSpace(version))
{
throw new ArgumentException("Value cannot be null or whitespace.", nameof(version));
}
MessageType = messageType;
MessageId = messageId;
Version = version;
Session = session;
Username = username;
Date = date ?? DateTime.UtcNow.ToString("o");
}
public static Header Create<T>(T messageContent, string session)
where T : Protocol.Message
{
if (messageContent is null)
{
throw new ArgumentNullException(nameof(messageContent));
}
return Create(messageContent.MessageType, session);
}
public static Header Create<T>(string session)
where T : Protocol.Message
{
var messageType = Protocol.Message.GetMessageType(typeof(T));
return Create(messageType, session);
}
private static Header Create(string messageType, string session)
{
var newHeader = new Header(
messageType: messageType,
messageId: Guid.NewGuid().ToString(),
version: Constants.MESSAGE_PROTOCOL_VERSION,
username: Constants.USERNAME,
session: session);
return newHeader;
}
}