feat: Add JsonRpc types for websocket paradigm
This commit is contained in:
1 parent
4fa8a63c71
commit
8009e61fc5
6 files changed
+197
-2
No files matched your search
@@ -1,3 +1,5 @@
|
||||
using Bts.Websocket;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
@@ -7,5 +9,10 @@ namespace bts.websocket_core {
|
||||
public class WebsocketClientID : UniqueID {
|
||||
public WebsocketClientID() : base(Guid.NewGuid().ToString()) {
|
||||
}
|
||||
|
||||
public WebsocketClientID(string str) : base(str) { }
|
||||
|
||||
public static WebsocketClientID? Parse(JsonRpcId id) =>
|
||||
new(id.ToString()!);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,6 +142,5 @@ namespace bts.websocket_core {
|
||||
}
|
||||
return TryParse(value, key, out obj, out error);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
namespace Bts.Websocket;
|
||||
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public static class JsonRpcError {
|
||||
public static JsonRpcError<T> Create<T>(long code, string? message, T data) =>
|
||||
new() {
|
||||
Code = code,
|
||||
Message = message,
|
||||
Data = data
|
||||
};
|
||||
}
|
||||
|
||||
public class JsonRpcError<T> {
|
||||
[JsonPropertyName("code")]
|
||||
public long Code { get; set; }
|
||||
|
||||
[JsonPropertyName("message")]
|
||||
public string? Message { get; set; }
|
||||
|
||||
[JsonPropertyName("data")]
|
||||
public T? Data { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
namespace Bts.Websocket;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
[JsonConverter(typeof(JsonRpcIdConverter))]
|
||||
public readonly struct JsonRpcId {
|
||||
readonly long? int64;
|
||||
readonly string? str;
|
||||
|
||||
JsonRpcId(long int64) =>
|
||||
this.int64 = int64;
|
||||
|
||||
JsonRpcId(string str) =>
|
||||
this.str = str;
|
||||
|
||||
public static JsonRpcId Create() =>
|
||||
new(DateTime.Now.Ticks);
|
||||
|
||||
public override string? ToString() =>
|
||||
int64?.ToString() ?? str;
|
||||
|
||||
class JsonRpcIdConverter : JsonConverter<JsonRpcId> {
|
||||
public override bool CanConvert(Type typeToConvert) =>
|
||||
typeToConvert == typeof(JsonRpcId);
|
||||
|
||||
public override JsonRpcId Read(
|
||||
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
|
||||
if (typeToConvert != typeof(JsonRpcId)) {
|
||||
throw new ArgumentException($"Unable to parse {typeToConvert}");
|
||||
}
|
||||
return reader.TokenType switch {
|
||||
JsonTokenType.String => new(reader.GetString()!),
|
||||
JsonTokenType.Number => new(reader.GetInt64()),
|
||||
_ => throw new ArgumentException($"Unable to parse {typeToConvert} from {reader.TokenType}"),
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(
|
||||
Utf8JsonWriter writer, JsonRpcId value, JsonSerializerOptions options) {
|
||||
if (value.int64.HasValue) {
|
||||
writer.WriteNumberValue(value.int64.Value);
|
||||
}
|
||||
if (value.str is not null) {
|
||||
writer.WriteStringValue(value.str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
namespace Bts.Websocket;
|
||||
|
||||
using bts.websocket_core;
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public static class JsonRpcRequest {
|
||||
public static JsonRpcRequest<T> Create<T>(JsonRpcId id, string method, T parameters) =>
|
||||
new() {
|
||||
Id = id,
|
||||
Method = method,
|
||||
Params = parameters
|
||||
};
|
||||
|
||||
public static JsonRpcRequest<T> CreateNotification<T>(string method, T parameters) =>
|
||||
new() {
|
||||
Method = method,
|
||||
Params = parameters
|
||||
};
|
||||
|
||||
public static bool TryDeserialize(
|
||||
WebsocketIncomingMessage message,
|
||||
[NotNullWhen(true)]out JsonRpcRequest<JsonObject?>? result,
|
||||
[NotNullWhen(false)]out string? error
|
||||
) {
|
||||
try {
|
||||
result = JsonSerializer.Deserialize<JsonRpcRequest<JsonObject?>>(message.message_body)!;
|
||||
error = null;
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
error = ex.ToString();
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class JsonRpcRequest<T> {
|
||||
[JsonPropertyName("method")]
|
||||
public string? Method { get; set; }
|
||||
|
||||
[JsonPropertyName("params")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[MaybeNull]
|
||||
public T Params { get; set; }
|
||||
|
||||
[JsonPropertyName("jsonrpc")]
|
||||
public string JsonRpc { get; set; } = "2.0";
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonPropertyName("id")]
|
||||
public JsonRpcId? Id { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool IsNotification =>
|
||||
!Id.HasValue;
|
||||
|
||||
public string Serialize() =>
|
||||
Serialize(JsonSerializerOptions.Default);
|
||||
|
||||
public string Serialize(JsonSerializerOptions opts) =>
|
||||
JsonSerializer.Serialize(this, opts);
|
||||
|
||||
public bool SerializeSafe(
|
||||
[NotNullWhen(true)]out string? result,
|
||||
[NotNullWhen(false)]out Exception? error) =>
|
||||
SerializeSafe(JsonSerializerOptions.Default, out result, out error);
|
||||
|
||||
public bool SerializeSafe(
|
||||
JsonSerializerOptions opts,
|
||||
[NotNullWhen(true)]out string? result,
|
||||
[NotNullWhen(false)]out Exception? error) {
|
||||
try {
|
||||
result = JsonSerializer.Serialize(this, opts);
|
||||
error = null;
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
error = ex;
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace Bts.Websocket;
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public static class JsonRpcResponse {
|
||||
public static JsonRpcResponse<T, JsonObject?> Create<T>(JsonRpcId id, T result) =>
|
||||
new() { Result = result };
|
||||
|
||||
public static JsonRpcResponse<JsonObject?, T> CreateError<T>(JsonRpcId id, JsonRpcError<T> error) =>
|
||||
new() { Id = id, Error = error };
|
||||
}
|
||||
|
||||
public class JsonRpcResponse<T, TError> {
|
||||
[JsonPropertyName("id")]
|
||||
public JsonRpcId Id { get; set; }
|
||||
|
||||
[JsonPropertyName("result")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[MaybeNull]
|
||||
public T Result { get; set; }
|
||||
|
||||
[JsonPropertyName("error")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public JsonRpcError<TError>? Error { get; set; }
|
||||
|
||||
[JsonPropertyName("jsonrpc")]
|
||||
public string JsonRpc { get; set; } = "2.0";
|
||||
}
|
||||
Reference in new issue
Block a user