Flow.Launcher/Flow.Launcher.Core/Plugin/JsonPRCModel.cs

129 lines
3.9 KiB
C#
Raw Normal View History

2014-07-18 12:00:55 +00:00

2020-04-22 10:26:09 +00:00
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Flow Launcher and other plugins,
2014-07-18 12:00:55 +00:00
* like python or other self-execute program. But, we added addtional infos (proxy and so on) into rpc request. Also, we didn't use the
* "id" and "jsonrpc" in the request, since it's not so useful in our request model.
*
* When execute a query:
2020-04-22 10:26:09 +00:00
* Flow Launcher -------JsonRPCServerRequestModel--------> client
* Flow Launcher <------JsonRPCQueryResponseModel--------- client
2014-07-18 12:00:55 +00:00
*
* When execute a action (which mean user select an item in reulst item):
2020-04-22 10:26:09 +00:00
* Flow Launcher -------JsonRPCServerRequestModel--------> client
* Flow Launcher <------JsonRPCResponseModel-------------- client
2014-07-18 12:00:55 +00:00
*
*/
using System.Collections.Generic;
2014-07-10 15:57:08 +00:00
using System.Linq;
using System.Text.Json.Serialization;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Plugin;
2014-07-05 15:10:34 +00:00
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Core.Plugin
2014-07-05 15:10:34 +00:00
{
2014-07-07 15:05:06 +00:00
public class JsonRPCErrorModel
2014-07-05 15:10:34 +00:00
{
2014-07-07 15:05:06 +00:00
public int Code { get; set; }
2014-07-05 15:10:34 +00:00
2014-07-07 15:05:06 +00:00
public string Message { get; set; }
public string Data { get; set; }
}
public class JsonRPCModelBase
{
public int Id { get; set; }
}
public class JsonRPCResponseModel : JsonRPCModelBase
{
public string Result { get; set; }
public JsonRPCErrorModel Error { get; set; }
2014-07-05 15:10:34 +00:00
}
2014-07-07 15:05:06 +00:00
public class JsonRPCQueryResponseModel : JsonRPCResponseModel
2014-07-05 15:10:34 +00:00
{
[JsonPropertyName("result")]
2014-07-09 10:15:23 +00:00
public new List<JsonRPCResult> Result { get; set; }
2021-02-14 05:59:59 +00:00
public string DebugMessage { get; set; }
2014-07-07 15:05:06 +00:00
}
2014-07-18 15:12:50 +00:00
public class JsonRPCRequestModel : JsonRPCModelBase
2014-07-07 15:05:06 +00:00
{
public string Method { get; set; }
2014-07-10 15:57:08 +00:00
public object[] Parameters { get; set; }
2014-07-09 10:15:23 +00:00
public override string ToString()
{
2014-07-18 12:00:55 +00:00
string rpc = string.Empty;
2014-07-10 15:57:08 +00:00
if (Parameters != null && Parameters.Length > 0)
2014-07-09 10:15:23 +00:00
{
2021-02-14 04:02:59 +00:00
string parameters = $"[{string.Join(',', Parameters.Select(GetParameterByType))}]";
rpc = $@"{{\""method\"":\""{Method}\"",\""parameters\"":{parameters}";
2014-07-18 12:00:55 +00:00
}
else
{
2021-02-14 04:02:59 +00:00
rpc = $@"{{\""method\"":\""{Method}\"",\""parameters\"":[]";
2014-07-09 10:15:23 +00:00
}
2014-07-18 12:00:55 +00:00
return rpc;
2014-07-10 15:57:08 +00:00
}
2017-04-11 13:25:25 +00:00
private string GetParameterByType(object parameter)
2021-02-14 04:02:59 +00:00
=> parameter switch
2014-07-10 15:57:08 +00:00
{
2021-02-14 04:02:59 +00:00
null => "null",
string _ => $@"\""{ReplaceEscapes(parameter.ToString())}\""",
bool _ => $@"{parameter.ToString().ToLower()}",
_ => parameter.ToString()
};
2021-02-14 04:02:59 +00:00
private string ReplaceEscapes(string str)
{
return str.Replace(@"\", @"\\") //Escapes in ProcessStartInfo
.Replace(@"\", @"\\") //Escapes itself when passed to client
.Replace(@"""", @"\\""""");
}
2014-07-07 15:05:06 +00:00
}
2014-07-18 12:00:55 +00:00
/// <summary>
2020-04-22 10:26:09 +00:00
/// Json RPC Request that Flow Launcher sent to client
2014-07-18 12:00:55 +00:00
/// </summary>
public class JsonRPCServerRequestModel : JsonRPCRequestModel
{
public override string ToString()
{
string rpc = base.ToString();
return rpc + "}";
}
2014-07-18 12:00:55 +00:00
}
/// <summary>
2020-04-22 10:26:09 +00:00
/// Json RPC Request(in query response) that client sent to Flow Launcher
2014-07-18 12:00:55 +00:00
/// </summary>
public class JsonRPCClientRequestModel : JsonRPCRequestModel
{
public bool DontHideAfterAction { get; set; }
2014-07-18 15:12:50 +00:00
public override string ToString()
{
string rpc = base.ToString();
return rpc + "}";
}
2014-07-18 12:00:55 +00:00
}
/// <summary>
2020-04-22 10:26:09 +00:00
/// Represent the json-rpc result item that client send to Flow Launcher
2014-07-18 12:00:55 +00:00
/// Typically, we will send back this request model to client after user select the result item
2020-04-22 10:26:09 +00:00
/// But if the request method starts with "Flow Launcher.", we will invoke the public APIs we expose.
2014-07-18 12:00:55 +00:00
/// </summary>
2014-07-07 15:05:06 +00:00
public class JsonRPCResult : Result
{
2014-07-18 12:00:55 +00:00
public JsonRPCClientRequestModel JsonRPCAction { get; set; }
2014-07-05 15:10:34 +00:00
}
}