@@ -3,9 +3,11 @@ use crate::verbosity;
33use crate :: Result ;
44use colored_json:: ToColoredJson ;
55use serde:: Deserialize ;
6+ use serde:: Serialize ;
67use serde_json:: { json, Value } ;
8+ use std:: i64;
79
8- #[ derive( Deserialize ) ]
10+ #[ derive( Serialize , Deserialize ) ]
911pub struct RpcResponse {
1012 pub result : Option < Value > ,
1113 pub error : Option < Value > ,
@@ -28,36 +30,67 @@ impl RpcResponse {
2830}
2931
3032pub fn call ( method : & str , params : Value ) -> Result < RpcResponse > {
31- if verbosity ( ) > 0 {
32- println ! (
33- "{}" ,
34- serde_json:: to_string( & params) ?. to_colored_json_auto( ) ?
35- ) ;
33+ match verbosity ( ) {
34+ i64:: MIN ..=0 => { }
35+ 1 ..=i64:: MAX => {
36+ println ! ( "Calling method {method} with the following params:" ) ;
37+ println ! (
38+ "{}" ,
39+ serde_json:: to_string( & params) ?. to_colored_json_auto( ) ?
40+ ) ;
41+ }
3642 }
3743 let params = params
3844 . as_object ( )
3945 . ok_or ( "params value is not a valid JSON object" ) ?;
40- let args = json ! (
46+ let req_body = json ! (
4147 { "jsonrpc" : "2.0" , "method" : method, "params" : params, "id" : 1 }
4248 ) ;
4349 let mut api_url = settings:: get_str ( "api_url" ) ?;
4450 if api_url. trim ( ) . is_empty ( ) {
4551 api_url = "https://api.btcmap.org/rpc" . into ( ) ;
4652 }
4753 if verbosity ( ) >= 2 {
48- println ! ( "{}" , serde_json:: to_string( & args) ?. to_colored_json_auto( ) ?) ;
54+ println ! ( "Full request body:" ) ;
55+ println ! (
56+ "{}" ,
57+ serde_json:: to_string( & req_body) ?. to_colored_json_auto( ) ?
58+ ) ;
4959 }
50- let response = ureq:: post ( api_url)
60+ let response: RpcResponse = ureq:: post ( api_url)
5161 . header ( "Content-Type" , "application/json" )
5262 . header (
5363 "Authorization" ,
5464 format ! ( "Bearer {}" , settings:: get_str( "password" ) ?) ,
5565 )
56- . send_json ( args ) ?
66+ . send_json ( req_body ) ?
5767 . body_mut ( )
58- . read_to_string ( ) ?;
59- if verbosity ( ) >= 2 {
60- println ! ( "{}" , response) ;
61- }
62- Ok ( serde_json:: from_str ( & response) ?)
68+ . read_json ( ) ?;
69+ match verbosity ( ) {
70+ i64:: MIN ..=0 => { }
71+ 1 ..=2 => {
72+ if response. result . is_some ( ) {
73+ println ! ( "RPC result:" ) ;
74+ println ! (
75+ "{}" ,
76+ serde_json:: to_string( & response. result) ?. to_colored_json_auto( ) ?
77+ ) ;
78+ }
79+ if response. error . is_some ( ) {
80+ println ! ( "RPC error:" ) ;
81+ println ! (
82+ "{}" ,
83+ serde_json:: to_string( & response. error) ?. to_colored_json_auto( ) ?
84+ ) ;
85+ }
86+ }
87+ 3 ..=i64:: MAX => {
88+ println ! ( "RPC response:" ) ;
89+ println ! (
90+ "{}" ,
91+ serde_json:: to_string( & response) ?. to_colored_json_auto( ) ?
92+ ) ;
93+ }
94+ } ;
95+ Ok ( response)
6396}
0 commit comments