-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJsonRpcRequest.cs
71 lines (60 loc) · 2.63 KB
/
JsonRpcRequest.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
// The Sisk Framework source code
// Copyright (c) 2024- PROJECT PRINCIPIUM and all Sisk contributors
//
// The code below is licensed under the MIT license as
// of the date of its publication, available at
//
// File name: JsonRpcRequest.cs
// Repository: https://github.com/sisk-http/core
using LightJson;
namespace Sisk.JsonRPC;
/// <summary>
/// Represents an JSON-RPC request message.
/// </summary>
public sealed class JsonRpcRequest {
/// <summary>
/// Gets the version used in the JSON-RPC message.
/// </summary>
public string Version { get; }
/// <summary>
/// Gets the method name of the JSON-RPC message.
/// </summary>
public string Method { get; }
/// <summary>
/// Gets the <see cref="JsonValue"/> containing the message parameter values.
/// </summary>
public JsonValue Parameters { get; }
/// <summary>
/// Gets the ID of the JSON-RPC message.
/// </summary>
public JsonValue Id { get; }
internal JsonRpcRequest ( string method, JsonValue parameters, string id ) {
this.Version = "2.0";
this.Method = method;
this.Parameters = parameters;
this.Id = id;
if (string.IsNullOrEmpty ( this.Method ))
throw new JsonRpcException ( "The JSON-RPC request Method cannot be null or an empty string." );
if (this.Parameters.Type is not JsonValueType.Array &&
this.Parameters.Type is not JsonValueType.Object) {
throw new JsonRpcException ( "The JSON-RPC request parameters must be an array or an object." );
}
}
internal JsonRpcRequest ( JsonObject rpcMessage ) {
this.Version = rpcMessage [ "jsonrpc" ].GetString ();
this.Method = rpcMessage [ "Method" ].GetString ();
this.Parameters = rpcMessage [ "params" ];
this.Id = rpcMessage [ "id" ];
if (this.Id.Type != JsonValueType.String && this.Id.Type != JsonValueType.Number && this.Id.Type != JsonValueType.Null && this.Id.Type != JsonValueType.Undefined) {
throw new JsonRpcException ( "The JSON-RPC request id must be an string, number or null." );
}
if (this.Version != "2.0")
throw new JsonRpcException ( "The JSON-RPC request version must be \"2.0\"." );
if (string.IsNullOrEmpty ( this.Method ))
throw new JsonRpcException ( "The JSON-RPC request Method cannot be null or an empty string." );
if (this.Parameters.Type is not JsonValueType.Array &&
this.Parameters.Type is not JsonValueType.Object) {
throw new JsonRpcException ( "The JSON-RPC request parameters must be an array or an object." );
}
}
}