77using Newtonsoft . Json . Linq ;
88
99public class JSONRPC {
10+ // Endpoint and Basic Authentication
1011 private string _endpoint ;
11-
12- // Basic authentication
1312 private string _username = null ;
1413 private string _password = null ;
1514
16- public void setEndpoint ( string endpoint , string username = null , string password = null ) {
15+ public void SetEndpoint ( string endpoint , string username = null , string password = null ) {
1716 _endpoint = endpoint ;
1817 _username = username ;
1918 _password = password ;
2019 }
2120
22- public void call ( string methodName , JObject parameters , Dictionary < string , string > headers , CookieContainer cookies , Action < Exception , JObject > cb ) {
23- call ( JValue . CreateNull ( ) , methodName , parameters , headers , cookies , cb ) ;
21+ public void Call ( string methodName , JObject parameters , Dictionary < string , string > headers , CookieContainer cookies , Action < Exception , JObject > cb ) {
22+ Call ( JValue . CreateNull ( ) , methodName , parameters , headers , cookies , cb ) ;
2423 }
2524
26- public void call ( string id , string methodName , JObject parameters , Dictionary < string , string > headers , CookieContainer cookies , Action < Exception , JObject > cb ) {
27- call ( new JValue ( id ) , methodName , parameters , headers , cookies , cb ) ;
25+ public void Call ( string id , string methodName , JObject parameters , Dictionary < string , string > headers , CookieContainer cookies , Action < Exception , JObject > cb ) {
26+ Call ( new JValue ( id ) , methodName , parameters , headers , cookies , cb ) ;
2827 }
2928
30- public void call ( int id , string methodName , JObject parameters , Dictionary < string , string > headers , CookieContainer cookies , Action < Exception , JObject > cb ) {
31- call ( new JValue ( id ) , methodName , parameters , headers , cookies , cb ) ;
29+ public void Call ( int id , string methodName , JObject parameters , Dictionary < string , string > headers , CookieContainer cookies , Action < Exception , JObject > cb ) {
30+ Call ( new JValue ( id ) , methodName , parameters , headers , cookies , cb ) ;
3231 }
3332
34- public void call ( JValue id , string methodName , JObject parameters , Dictionary < string , string > headers , CookieContainer cookies , Action < Exception , JObject > cb ) {
35- // Make sure the endpoint is set
36- if ( string . IsNullOrEmpty ( _endpoint ) ) {
37- cb ( new Exception ( "Endpoint has not been set" ) , null ) ;
38- return ;
39- }
40-
33+ public void Call ( JValue id , string methodName , JObject parameters , Dictionary < string , string > headers , CookieContainer cookies , Action < Exception , JObject > cb ) {
4134 // Setup JSON request object
4235 JObject requestObject = new JObject ( ) ;
4336 requestObject . Add ( "jsonrpc" , new JValue ( "2.0" ) ) ;
@@ -55,31 +48,72 @@ public void call(JValue id, string methodName, JObject parameters, Dictionary<st
5548 return ;
5649 }
5750
58- // Make a copy of the provided headers and add additional required headers
59- Dictionary < string , string > _headers = new Dictionary < string , string > ( headers ) ;
60- if ( _username != null && _password != null ) {
61- string authInfo = _username + ":" + _password ;
62- string encodedAuth = Convert . ToBase64String ( Encoding . Default . GetBytes ( authInfo ) ) ;
63- _headers . Add ( "Authorization" , "Basic " + encodedAuth ) ;
64- }
65-
66- // Send HTTP post to JSON rpc endpoint
67- HTTPRequest . Post ( _endpoint , "application/json" , postData , _headers , cookies , ( Exception requestError , string responseString ) => {
51+ // Send request
52+ SendRequest ( postData , headers , cookies , ( Exception requestError , string responseString ) => {
6853 if ( requestError != null ) {
6954 cb ( requestError , null ) ;
7055 return ;
7156 }
72-
57+
7358 // Deserialize the JSON response
7459 JObject responseObject ;
7560 try {
7661 responseObject = JObject . Parse ( responseString ) ;
77- } catch ( Exception error ) {
78- cb ( error , null ) ;
62+ } catch ( Exception parseError ) {
63+ cb ( parseError , null ) ;
7964 return ;
8065 }
81-
66+
8267 cb ( null , responseObject ) ;
8368 } ) ;
8469 }
70+
71+ public void CallBatch ( JSONRPCBatch rpcBatch , Dictionary < string , string > headers , CookieContainer cookies , Action < Exception , JArray > cb ) {
72+ // Serialize JSON request object into string
73+ string postData ;
74+ try {
75+ postData = rpcBatch . batch . ToString ( ) ;
76+ } catch ( Exception serializeError ) {
77+ cb ( serializeError , null ) ;
78+ return ;
79+ }
80+
81+ // Send request
82+ SendRequest ( postData , headers , cookies , ( Exception requestError , string responseString ) => {
83+ if ( requestError != null ) {
84+ cb ( requestError , null ) ;
85+ return ;
86+ }
87+
88+ // Deserialize the JSON response
89+ JArray responseArray ;
90+ try {
91+ responseArray = JArray . Parse ( responseString ) ;
92+ } catch ( Exception parseError ) {
93+ cb ( parseError , null ) ;
94+ return ;
95+ }
96+
97+ cb ( null , responseArray ) ;
98+ } ) ;
99+ }
100+
101+ private void SendRequest ( string postData , Dictionary < string , string > headers , CookieContainer cookies , Action < Exception , string > cb ) {
102+ // Make sure the endpoint is set
103+ if ( string . IsNullOrEmpty ( _endpoint ) ) {
104+ cb ( new Exception ( "Endpoint has not been set" ) , null ) ;
105+ return ;
106+ }
107+
108+ // Make a copy of the provided headers and add additional required headers
109+ Dictionary < string , string > _headers = new Dictionary < string , string > ( headers ) ;
110+ if ( _username != null && _password != null ) {
111+ string authInfo = _username + ":" + _password ;
112+ string encodedAuth = Convert . ToBase64String ( Encoding . Default . GetBytes ( authInfo ) ) ;
113+ _headers . Add ( "Authorization" , "Basic " + encodedAuth ) ;
114+ }
115+
116+ // Send HTTP post to JSON rpc endpoint
117+ HTTPRequest . Post ( _endpoint , "application/json" , postData , _headers , cookies , cb ) ;
118+ }
85119}
0 commit comments