diff --git a/src/client/spark_websocket/Core/WebsocketClientID.cs b/src/client/spark_websocket/Core/WebsocketClientID.cs index 2e80c4ea4..168bf6a71 100644 --- a/src/client/spark_websocket/Core/WebsocketClientID.cs +++ b/src/client/spark_websocket/Core/WebsocketClientID.cs @@ -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()!); } -} \ No newline at end of file +} diff --git a/src/client/spark_websocket/Core/WebsocketJsonParsers.cs b/src/client/spark_websocket/Core/WebsocketJsonParsers.cs index 9a1c6dd63..f9fd6c323 100644 --- a/src/client/spark_websocket/Core/WebsocketJsonParsers.cs +++ b/src/client/spark_websocket/Core/WebsocketJsonParsers.cs @@ -142,6 +142,5 @@ namespace bts.websocket_core { } return TryParse(value, key, out obj, out error); } - } } diff --git a/src/client/spark_websocket/JsonRpcError.cs b/src/client/spark_websocket/JsonRpcError.cs new file mode 100644 index 000000000..7bb53ac0e --- /dev/null +++ b/src/client/spark_websocket/JsonRpcError.cs @@ -0,0 +1,23 @@ +namespace Bts.Websocket; + +using System.Text.Json.Serialization; + +public static class JsonRpcError { + public static JsonRpcError Create(long code, string? message, T data) => + new() { + Code = code, + Message = message, + Data = data + }; +} + +public class JsonRpcError { + [JsonPropertyName("code")] + public long Code { get; set; } + + [JsonPropertyName("message")] + public string? Message { get; set; } + + [JsonPropertyName("data")] + public T? Data { get; set; } +} diff --git a/src/client/spark_websocket/JsonRpcId.cs b/src/client/spark_websocket/JsonRpcId.cs new file mode 100644 index 000000000..dc338b3cd --- /dev/null +++ b/src/client/spark_websocket/JsonRpcId.cs @@ -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 { + 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); + } + } + } + +} diff --git a/src/client/spark_websocket/JsonRpcRequest.cs b/src/client/spark_websocket/JsonRpcRequest.cs new file mode 100644 index 000000000..037590e7d --- /dev/null +++ b/src/client/spark_websocket/JsonRpcRequest.cs @@ -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 Create(JsonRpcId id, string method, T parameters) => + new() { + Id = id, + Method = method, + Params = parameters + }; + + public static JsonRpcRequest CreateNotification(string method, T parameters) => + new() { + Method = method, + Params = parameters + }; + + public static bool TryDeserialize( + WebsocketIncomingMessage message, + [NotNullWhen(true)]out JsonRpcRequest? result, + [NotNullWhen(false)]out string? error + ) { + try { + result = JsonSerializer.Deserialize>(message.message_body)!; + error = null; + return true; + } catch (Exception ex) { + error = ex.ToString(); + result = null; + return false; + } + } +} + +public class JsonRpcRequest { + [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; + } + } +} diff --git a/src/client/spark_websocket/JsonRpcResponse.cs b/src/client/spark_websocket/JsonRpcResponse.cs new file mode 100644 index 000000000..7be43d66f --- /dev/null +++ b/src/client/spark_websocket/JsonRpcResponse.cs @@ -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 Create(JsonRpcId id, T result) => + new() { Result = result }; + + public static JsonRpcResponse CreateError(JsonRpcId id, JsonRpcError error) => + new() { Id = id, Error = error }; +} + +public class JsonRpcResponse { + [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? Error { get; set; } + + [JsonPropertyName("jsonrpc")] + public string JsonRpc { get; set; } = "2.0"; +}