PowerToys/Wox.Core/Plugin/JsonPRCModel.cs

141 lines
4.5 KiB
C#
Raw Normal View History

2014-07-18 20:00:55 +08:00

/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins,
* 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:
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCQueryResponseModel--------- client
*
* When execute a action (which mean user select an item in reulst item):
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCResponseModel-------------- client
*
*/
using System.Collections.Generic;
2014-07-10 23:57:08 +08:00
using System.Linq;
2014-07-07 23:05:06 +08:00
using Wox.Plugin;
2014-07-05 23:10:34 +08:00
2014-12-26 19:36:43 +08:00
namespace Wox.Core.Plugin
2014-07-05 23:10:34 +08:00
{
2014-07-07 23:05:06 +08:00
public class JsonRPCErrorModel
2014-07-05 23:10:34 +08:00
{
2014-07-07 23:05:06 +08:00
public int Code { get; set; }
2014-07-05 23:10:34 +08:00
2014-07-07 23:05:06 +08: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 23:10:34 +08:00
}
2014-07-07 23:05:06 +08:00
public class JsonRPCQueryResponseModel : JsonRPCResponseModel
2014-07-05 23:10:34 +08:00
{
2014-07-09 18:15:23 +08:00
public new List<JsonRPCResult> Result { get; set; }
2014-07-07 23:05:06 +08:00
}
2014-07-18 23:12:50 +08:00
public class JsonRPCRequestModel : JsonRPCModelBase
2014-07-07 23:05:06 +08:00
{
public string Method { get; set; }
2014-07-10 23:57:08 +08:00
public object[] Parameters { get; set; }
2014-07-09 18:15:23 +08:00
public override string ToString()
{
2014-07-18 20:00:55 +08:00
string rpc = string.Empty;
2014-07-10 23:57:08 +08:00
if (Parameters != null && Parameters.Length > 0)
2014-07-09 18:15:23 +08:00
{
2014-07-10 23:57:08 +08:00
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParamterByType(o) + ","));
parameters = parameters.Substring(0, parameters.Length - 1) + "]";
2014-07-18 20:00:55 +08:00
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}", Method, parameters);
}
else
{
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":[]", Method);
2014-07-09 18:15:23 +08:00
}
2014-07-18 20:00:55 +08:00
return rpc;
2014-07-10 23:57:08 +08:00
}
private string GetParamterByType(object paramter)
{
2014-07-18 20:00:55 +08:00
2014-07-10 23:57:08 +08:00
if (paramter is string)
{
return string.Format(@"\""{0}\""", RepalceEscapes(paramter.ToString()));
2014-07-10 23:57:08 +08:00
}
if (paramter is int || paramter is float || paramter is double)
{
return string.Format(@"{0}", paramter);
}
if (paramter is bool)
{
return string.Format(@"{0}", paramter.ToString().ToLower());
}
return paramter.ToString();
2014-07-09 18:15:23 +08:00
}
private string RepalceEscapes(string str)
{
return str.Replace(@"\", @"\\") //Escapes in ProcessStartInfo
.Replace(@"\", @"\\") //Escapes itself when passed to client
.Replace(@"""", @"\\""""");
}
2014-07-07 23:05:06 +08:00
}
2014-07-18 20:00:55 +08:00
/// <summary>
/// Json RPC Request that Wox sent to client
/// </summary>
public class JsonRPCServerRequestModel : JsonRPCRequestModel
{
public IHttpProxy HttpProxy { get; set; }
public override string ToString()
{
string rpc = base.ToString();
if (HttpProxy != null)
{
rpc += string.Format(@",\""proxy\"":{{\""enabled\"":{0},\""server\"":\""{1}\"",\""port\"":{2},\""username\"":\""{3}\"",\""password\"":\""{4}\""}}",
HttpProxy.Enabled.ToString().ToLower(), HttpProxy.Server, HttpProxy.Port, HttpProxy.UserName, HttpProxy.Password);
}
return rpc + "}";
}
}
/// <summary>
2014-07-18 23:12:50 +08:00
/// Json RPC Request(in query response) that client sent to Wox
2014-07-18 20:00:55 +08:00
/// </summary>
public class JsonRPCClientRequestModel : JsonRPCRequestModel
{
public bool DontHideAfterAction { get; set; }
2014-07-18 23:12:50 +08:00
public override string ToString()
{
string rpc = base.ToString();
return rpc + "}";
}
2014-07-18 20:00:55 +08:00
}
/// <summary>
/// Represent the json-rpc result item that client send to Wox
/// Typically, we will send back this request model to client after user select the result item
/// But if the request method starts with "Wox.", we will invoke the public APIs we expose.
/// </summary>
2014-07-07 23:05:06 +08:00
public class JsonRPCResult : Result
{
2014-07-18 20:00:55 +08:00
public JsonRPCClientRequestModel JsonRPCAction { get; set; }
2014-07-05 23:10:34 +08:00
}
}