73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
namespace Bts.DataStreamWebApp.Server.Messages;
|
|
|
|
using BCG;
|
|
|
|
using bts.websocket_core.json_rpc;
|
|
|
|
using Bts.DataStreamExpressions;
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using System.Text.Json.Serialization;
|
|
|
|
public class SubscribeRequest {
|
|
[JsonPropertyName("query")]
|
|
public string? Query { get; set; }
|
|
|
|
[JsonPropertyName("subscription_id")]
|
|
public string? SubscriptionId { get; set; }
|
|
|
|
internal static bool Parse(JsonRpcRequest<JsonNode> req, [NotNullWhen(true)]out SubscribeRequest? subscription, [NotNullWhen(false)]out SubscriptionError? error, out ErrorCode errorCode) {
|
|
subscription = null;
|
|
errorCode = ErrorCode.None;
|
|
var paramsJsonObj = req.Params?.AsObject();
|
|
error = null;
|
|
if (paramsJsonObj is null) {
|
|
errorCode = ErrorCode.InvalidSubscriptionRequest;
|
|
error = new() { ErrorMessage = "'params' object not found" };
|
|
return false;
|
|
}
|
|
subscription = paramsJsonObj.Deserialize<SubscribeRequest>();
|
|
if (subscription is null) {
|
|
errorCode = ErrorCode.InvalidSubscriptionRequest;
|
|
error = new() { ErrorMessage = "'params' object invalid" };
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
internal bool ParseQuery([NotNullWhen(true)] out ViewStream? stream, [NotNullWhen(false)] out SubscriptionError? error, out ErrorCode errorCode) {
|
|
stream = null;
|
|
errorCode = ErrorCode.None;
|
|
error = null;
|
|
if (Query is null) {
|
|
errorCode = ErrorCode.InvalidSubscriptionRequest;
|
|
error = new() { ErrorMessage = "'params.query' field not found (expected a string)" };
|
|
return false;
|
|
}
|
|
var exp = DataStreamExpressionsInterpreter.Evaluate(Query);
|
|
if (exp is null) {
|
|
errorCode = ErrorCode.InvalidSubscriptionRequest;
|
|
error = new() { ErrorMessage = "'params.query' expression has invalid syntax" };
|
|
return false;
|
|
}
|
|
Exception? exception = null;
|
|
try {
|
|
stream = exp.getView(new Dictionary<string, StatementExpression>(0));
|
|
} catch (Exception ex) {
|
|
exception = ex;
|
|
}
|
|
if (stream is null) {
|
|
errorCode = ErrorCode.InvalidSubscriptionRequest;
|
|
error = new() { ErrorMessage = "Failed to create ViewStream from 'params.query' expression (check for typos in arguments/typenames)" };
|
|
if (exception is not null) {
|
|
error.ErrorMessage += ". " + exception.Message;
|
|
error.StackTrace = exception.StackTrace?.Split("\n")?.ToList();
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|