-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
009cf1f
commit 7104921
Showing
21 changed files
with
1,000 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
extensions/Sisk.JsonRPC/Annotations/MethodDescriptionAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Sisk.JsonRPC.Annotations; | ||
|
||
/// <summary> | ||
/// Specifies a description for a method. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
public sealed class MethodDescriptionAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Gets the description of the method. | ||
/// </summary> | ||
public string Description { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MethodDescriptionAttribute"/> class with the specified description. | ||
/// </summary> | ||
/// <param name="description">The description of the method.</param> | ||
public MethodDescriptionAttribute(string description) | ||
{ | ||
Description = description; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
extensions/Sisk.JsonRPC/Annotations/ParamDescriptionAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace Sisk.JsonRPC.Annotations; | ||
|
||
/// <summary> | ||
/// Specifies a description for a method parameter. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
public sealed class ParamDescriptionAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Gets the description of the method parameter. | ||
/// </summary> | ||
public string Description { get; } | ||
|
||
/// <summary> | ||
/// Gets the target parameter name. | ||
/// </summary> | ||
public string ParameterName { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ParamDescriptionAttribute"/> class with the specified description. | ||
/// </summary> | ||
/// <param name="paramName">The parameter name.</param> | ||
/// <param name="description">The description of the method parameter.</param> | ||
public ParamDescriptionAttribute(string paramName, string description) | ||
{ | ||
ParameterName = paramName; | ||
Description = description; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace Sisk.JsonRPC.Annotations; | ||
|
||
/// <summary> | ||
/// Represents an JSON-RPC method. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] | ||
public sealed class WebMethodAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Gets or sets the method name. | ||
/// </summary> | ||
public string? Name { get; set; } | ||
|
||
/// <summary> | ||
/// Creates an new <see cref="WebMethodAttribute"/> with no parameters. | ||
/// </summary> | ||
public WebMethodAttribute() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates an new <see cref="WebMethodAttribute"/> with given | ||
/// parameters. | ||
/// </summary> | ||
/// <param name="methodName">The method name.</param> | ||
public WebMethodAttribute(string methodName) | ||
{ | ||
Name = methodName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace Sisk.JsonRPC.Annotations; | ||
|
||
/// <summary> | ||
/// Represents an attribute which holds the class name for a group of | ||
/// JSON-RPC methods. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class)] | ||
public sealed class WebNameAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Gets or sets the name associated with the method group. | ||
/// </summary> | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// Creates an new instance of the <see cref="WebMethodAttribute"/> attribute. | ||
/// </summary> | ||
/// <param name="name">The method-group name.</param> | ||
public WebNameAttribute(string name) | ||
{ | ||
Name = name; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
extensions/Sisk.JsonRPC/Converters/JsonRpcErrorConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using LightJson; | ||
using LightJson.Converters; | ||
|
||
namespace Sisk.JsonRPC.Converters; | ||
|
||
internal class JsonRpcErrorConverter : JsonConverter | ||
{ | ||
public override bool CanSerialize(Type type, JsonOptions options) | ||
{ | ||
return type == typeof(JsonRpcError); | ||
} | ||
|
||
public override object Deserialize(JsonValue value, Type requestedType, JsonOptions options) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override JsonValue Serialize(object value, JsonOptions options) | ||
{ | ||
JsonRpcError error = (JsonRpcError)value; | ||
return new JsonObject() | ||
{ | ||
["code"] = error.Code, | ||
["message"] = error.Message, | ||
["data"] = error.Data | ||
}; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
extensions/Sisk.JsonRPC/Converters/JsonRpcRequestConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using LightJson; | ||
using LightJson.Converters; | ||
|
||
namespace Sisk.JsonRPC.Converters; | ||
|
||
internal class JsonRpcRequestConverter : JsonConverter | ||
{ | ||
public override bool CanSerialize(Type type, JsonOptions options) | ||
{ | ||
return type == typeof(JsonRpcRequest); | ||
} | ||
|
||
public override object Deserialize(JsonValue value, Type requestedType, JsonOptions options) | ||
{ | ||
return new JsonRpcRequest(value.GetJsonObject()); | ||
} | ||
|
||
public override JsonValue Serialize(object value, JsonOptions options) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
extensions/Sisk.JsonRPC/Converters/JsonRpcResponseConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using LightJson; | ||
using LightJson.Converters; | ||
|
||
namespace Sisk.JsonRPC.Converters; | ||
|
||
internal class JsonRpcResponseConverter : JsonConverter | ||
{ | ||
public override bool CanSerialize(Type type, JsonOptions options) | ||
{ | ||
return type == typeof(JsonRpcResponse); | ||
} | ||
|
||
public override object Deserialize(JsonValue value, Type requestedType, JsonOptions options) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override JsonValue Serialize(object value, JsonOptions options) | ||
{ | ||
JsonRpcResponse response = (JsonRpcResponse)value; | ||
if (response.Error is JsonRpcError err) | ||
{ | ||
return new JsonObject() | ||
{ | ||
["jsonrpc"] = "2.0", | ||
["error"] = JsonValue.Serialize(err), | ||
["id"] = response.Id | ||
}; | ||
} | ||
else | ||
{ | ||
return new JsonObject() | ||
{ | ||
["jsonrpc"] = "2.0", | ||
["result"] = response.Result!.Value, | ||
["id"] = response.Id | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using LightJson; | ||
using Sisk.JsonRPC.Annotations; | ||
using System.Reflection; | ||
|
||
namespace Sisk.JsonRPC; | ||
internal class DocumentationDescriptor | ||
{ | ||
internal static JsonArray GetDocumentationDescriptor(JsonRpcHandler handler) | ||
{ | ||
|
||
JsonArray arr = new JsonArray(); | ||
|
||
foreach (var method in handler.Methods.methods) | ||
{ | ||
var methodDocs = method.Value.Method.GetCustomAttribute<MethodDescriptionAttribute>(); | ||
var paramsDocs = method.Value.Method.GetCustomAttributes<ParamDescriptionAttribute>(); | ||
|
||
var item = new | ||
{ | ||
name = method.Key, | ||
description = methodDocs?.Description, | ||
returns = method.Value.Method.ReturnType.Name, | ||
parameters = method.Value | ||
.Method.GetParameters() | ||
.Select(p => new | ||
{ | ||
name = p.Name, | ||
typeName = p.ParameterType.Name, | ||
description = paramsDocs.FirstOrDefault(f => f.ParameterName == p.Name)?.Description, | ||
isOptional = p.IsOptional | ||
}) | ||
.ToArray() | ||
}; | ||
|
||
arr.Add(JsonValue.Serialize(item, handler.JsonSerializerOptions)); | ||
} | ||
|
||
return arr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Sisk.JsonRPC; | ||
|
||
internal class JsonErrorCode | ||
{ | ||
public const int InvalidRequest = -32600; | ||
public const int MethodNotFound = -32601; | ||
public const int InvalidParams = -32602; | ||
public const int InternalError = -32603; | ||
public const int ParseError = -32700; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using LightJson; | ||
|
||
namespace Sisk.JsonRPC; | ||
|
||
/// <summary> | ||
/// Represents an JSON-RPC error. | ||
/// </summary> | ||
public readonly struct JsonRpcError | ||
{ | ||
/// <summary> | ||
/// Gets the JSON-RPC error code. | ||
/// </summary> | ||
public int Code { get; } | ||
|
||
/// <summary> | ||
/// Gets the JSON-RPC error message. | ||
/// </summary> | ||
public string Message { get; } | ||
|
||
/// <summary> | ||
/// Gets the JSON-RPC error additional data. | ||
/// </summary> | ||
public JsonValue Data { get; } | ||
|
||
/// <summary> | ||
/// Creates an new instance of the <see cref="JsonRpcError"/> structure. | ||
/// </summary> | ||
public JsonRpcError() | ||
{ | ||
Code = -32603; | ||
Message = "An exception was thrown."; | ||
Data = JsonValue.Null; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an new instance of the <see cref="JsonRpcError"/> structure with given | ||
/// parameters. | ||
/// </summary> | ||
/// <param name="code">The JSON-RPC error code.</param> | ||
/// <param name="message">The JSON-RPC error message.</param> | ||
public JsonRpcError(int code, string message) | ||
{ | ||
Code = code; | ||
Message = message; | ||
Data = JsonValue.Null; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an new instance of the <see cref="JsonRpcError"/> structure with given | ||
/// parameters. | ||
/// </summary> | ||
/// <param name="code">The JSON-RPC error code.</param> | ||
/// <param name="message">The JSON-RPC error message.</param> | ||
/// <param name="data">The JSON-RPC error additional data.</param> | ||
public JsonRpcError(int code, string message, JsonValue data) | ||
{ | ||
Code = code; | ||
Message = message; | ||
Data = data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using LightJson; | ||
|
||
namespace Sisk.JsonRPC; | ||
|
||
/// <summary> | ||
/// Represents an error that occur during the JSON-RPC application | ||
/// execution. | ||
/// </summary> | ||
public class JsonRpcException : Exception | ||
{ | ||
/// <summary> | ||
/// Gets the error code associated with the JSON-RPC error. | ||
/// </summary> | ||
public int Code { get; } | ||
|
||
/// <summary> | ||
/// Gets additional data associated with the error, if any. | ||
/// </summary> | ||
public new object? Data { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="JsonRpcException"/> class | ||
/// with a specified error message. | ||
/// </summary> | ||
/// <param name="message">The error message that explains the reason for the exception.</param> | ||
public JsonRpcException(string message) : base(message) | ||
{ | ||
Code = 1; | ||
Data = null; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="JsonRpcException"/> class | ||
/// with a specified error message, error code, and additional data. | ||
/// </summary> | ||
/// <param name="message">The error message that explains the reason for the exception.</param> | ||
/// <param name="code">The error code associated with the JSON-RPC error.</param> | ||
/// <param name="data">Additional data associated with the error.</param> | ||
public JsonRpcException(string message, int code, object? data) : base(message) | ||
{ | ||
Code = code; | ||
Data = data; | ||
} | ||
|
||
/// <summary> | ||
/// Converts the current <see cref="JsonRpcException"/> into a <see cref="JsonRpcError"/>. | ||
/// </summary> | ||
/// <returns>A <see cref="JsonRpcError"/> representing the error details.</returns> | ||
public JsonRpcError AsRpcError() | ||
{ | ||
return new JsonRpcError(Code, Message, JsonValue.Serialize(Data)); | ||
} | ||
} |
Oops, something went wrong.