commit c19de0df907c5032f8861114af9b866ebbd85add Author: Harald Wolff Date: Mon Sep 18 11:30:23 2017 +0200 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e82d27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +# Autosave files +*~ + +# build +[Oo]bj/ +[Bb]in/ +packages/ +TestResults/ + +# globs +Makefile.in +*.DS_Store +*.sln.cache +*.suo +*.cache +*.pidb +*.userprefs +*.usertasks +config.log +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.user +*.tar.gz +tarballs/ +test-results/ +Thumbs.db + +# Mac bundle stuff +*.dmg +*.app + +# resharper +*_Resharper.* +*.Resharper + +# dotCover +*.dotCover diff --git a/JSONRPC.csproj b/JSONRPC.csproj new file mode 100644 index 0000000..a19df25 --- /dev/null +++ b/JSONRPC.csproj @@ -0,0 +1,56 @@ + + + + Debug + AnyCPU + {DCE6066E-9709-4D12-8994-F7879C3557D6} + Library + JSONRPC + JSONRPC + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + true + bin\Release + prompt + 4 + false + + + + + ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..274e18b --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("JSONRPC")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/RPCCall.cs b/RPCCall.cs new file mode 100644 index 0000000..2d69077 --- /dev/null +++ b/RPCCall.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +namespace JSONRPC +{ + public class RPCCall + { + public RPCConnector Connector { get; private set; } + public String MethodName { get; private set; } + public Dictionary args; + + public RPCCall(RPCConnector connector) + { + this.Connector = connector; + this.args = new Dictionary(); + } + + public RPCCall method(string method){ + this.MethodName = method; + return this; + } + + public RPCCall parameter(string name,object value){ + this.args.Add(name,value); + return this; + } + + + public Response execute(){ + return Connector.Call(this.MethodName,this.args); + } + + } +} diff --git a/RPCConnector.cs b/RPCConnector.cs new file mode 100644 index 0000000..0bf385a --- /dev/null +++ b/RPCConnector.cs @@ -0,0 +1,52 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using System.Collections.Generic; + +namespace JSONRPC +{ + public class RPCConnector + { + private readonly HttpClient http; + + public RPCConnector(string hostname,int port) + { + this.http = new HttpClient(); + this.Hostname = hostname; + this.Port = port; + } + + public String Hostname { get; private set; } + public int Port { get; private set; } + public string URL { get { return String.Format("http://{0}:{1}/json_rpc",Hostname,Port); } } + + + public RPCCall Call(){ + return new RPCCall(this); + } + + public Response Call(string method) + { + return Call(method, null); + } + public Response Call(string method,Dictionary args){ + try + { + string request = JSONRPC.Request.Create(method,args); + + Task response = http.PostAsync(URL, new StringContent(request)); + response.Wait(); + Task result = response.Result.Content.ReadAsStringAsync(); + result.Wait(); + + return new Response(result.Result); + } + catch (HttpRequestException e) + { + Response resp = new Response(String.Format("{ \"id\": \"0\", \"jsonrpc\": \"2.0\", \"result\": { status: \"Error\", message: \"HttpException: {0}\"}", e.ToString())); + return resp; + } + } + + } +} diff --git a/Request.cs b/Request.cs new file mode 100644 index 0000000..1c5e1ea --- /dev/null +++ b/Request.cs @@ -0,0 +1,35 @@ +using System; +using Newtonsoft.Json; +using System.Collections.Generic; + +namespace JSONRPC +{ + public class Request { + [JsonPropertyAttribute("jsonrpc")] + public string jsonrpc { get; set; } = "2.0"; + [JsonPropertyAttribute("id")] + public String id { get; set; } = "0"; + [JsonPropertyAttribute("method")] + public String method { get; set; } + [JsonPropertyAttribute("params")] + public Dictionary args { get; set; } + + private Request(string method,Dictionary args){ + this.id = Environment.TickCount.ToString(); + this.method = method; + this.args = args == null ? new Dictionary() : new Dictionary(args); + } + + public static string Create(string method) + { + return Create(method, null); + } + + public static string Create(string method,Dictionary args){ + Request r = new Request(method,args); + return JsonConvert.SerializeObject(r); + } + + + } +} diff --git a/Response.cs b/Response.cs new file mode 100644 index 0000000..3c228a6 --- /dev/null +++ b/Response.cs @@ -0,0 +1,24 @@ +using System; +using Newtonsoft.Json.Linq; +using System.Threading; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json; +using System.Collections.Generic; + +namespace JSONRPC +{ + public class Response { + public string jsonrpc { get; set; } + public string id { get; set; } + public Dictionary result { get; set; } + + public Response(string source){ + JsonConvert.PopulateObject(source,this); + } + + public bool Success(){ + return result["status"].ToString().Equals("OK"); + } + + } +} diff --git a/packages.config b/packages.config new file mode 100644 index 0000000..ee51c23 --- /dev/null +++ b/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file