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

136 lines
4.2 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;
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
{
2014-07-09 10:15:23 +00:00
public new List<JsonRPCResult> Result { 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
{
2017-04-11 13:25:25 +00:00
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParameterByType(o) + ","));
2014-07-10 15:57:08 +00:00
parameters = parameters.Substring(0, parameters.Length - 1) + "]";
2014-07-18 12:00:55 +00:00
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}", Method, parameters);
}
else
{
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":[]", Method);
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)
2014-07-10 15:57:08 +00:00
{
2017-04-11 13:25:25 +00:00
if (parameter == null) {
return "null";
}
if (parameter is string)
2014-07-10 15:57:08 +00:00
{
2017-04-11 13:25:25 +00:00
return string.Format(@"\""{0}\""", ReplaceEscapes(parameter.ToString()));
2014-07-10 15:57:08 +00:00
}
2017-04-11 13:25:25 +00:00
if (parameter is int || parameter is float || parameter is double)
2014-07-10 15:57:08 +00:00
{
2017-04-11 13:25:25 +00:00
return string.Format(@"{0}", parameter);
2014-07-10 15:57:08 +00:00
}
2017-04-11 13:25:25 +00:00
if (parameter is bool)
2014-07-10 15:57:08 +00:00
{
2017-04-11 13:25:25 +00:00
return string.Format(@"{0}", parameter.ToString().ToLower());
2014-07-10 15:57:08 +00:00
}
2017-04-11 13:25:25 +00:00
return parameter.ToString();
2014-07-09 10:15:23 +00:00
}
2017-04-11 13:25:25 +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
}
}