-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add little command for easy access to JSON-RPC interface #180
Conversation
57259ef
to
1a04495
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, LGTM after tests pass!
trin-cli/src/main.rs
Outdated
let mut client = match TrinClient::from_ipc(&ipc) { | ||
Ok(client) => client, | ||
Err(err) => { | ||
eprintln!("Could not connect. err={:?}", err); | ||
return; | ||
} | ||
}; | ||
|
||
let req = client.build_request(endpoint.as_str()); | ||
let resp = client.make_request(req); | ||
|
||
match resp { | ||
Err(error) => { | ||
eprintln!("error: {}", error); | ||
} | ||
Ok(value) => { | ||
// unwrap: this value is safe to serialize, it was just deserialized! | ||
println!("{}", serde_json::to_string_pretty(&value).unwrap()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you like it, it looks like this could shrink a bit by returning a result from main (with a bit of error tweaking):
let mut client = match TrinClient::from_ipc(&ipc) { | |
Ok(client) => client, | |
Err(err) => { | |
eprintln!("Could not connect. err={:?}", err); | |
return; | |
} | |
}; | |
let req = client.build_request(endpoint.as_str()); | |
let resp = client.make_request(req); | |
match resp { | |
Err(error) => { | |
eprintln!("error: {}", error); | |
} | |
Ok(value) => { | |
// unwrap: this value is safe to serialize, it was just deserialized! | |
println!("{}", serde_json::to_string_pretty(&value).unwrap()); | |
} | |
} | |
let mut client = TrinClient::from_ipc(&ipc)?; | |
let req = client.build_request(endpoint.as_str()); | |
let resp = client.make_request(req); | |
let value = resp?; | |
// unwrap: this value is safe to serialize, it was just deserialized! | |
println!("{}", serde_json::to_string_pretty(&value).unwrap()); |
Or, in this kind of one-shot CLI tool, it doesn't bother me to .unwrap()
either (as is already used elsewhere in the module).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point, just pushed a much shorter version
trin-cli/src/main.rs
Outdated
|
||
fn build_request<'a>(method: &'a str, request_id: u64) -> jsonrpc::Request<'a> { | ||
jsonrpc::Request { | ||
method: method, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clippy said:
method: method, | |
method, |
// gave it exclusive ownership that would require us to open a new connection for every | ||
// command we wanted to send! By making a clone (or, by trying to) we can have our cake |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that's only important with an interactive REPL-type tool, rather than this CLI one-shot, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, definitely. I can drop it if you would prefer. This was originally added because I had grand plans of turning this into a client we could use in other parts of the codebase which want to make JSON-RPC calls, such as in the peer tester. You're right that it's unnecessary for current usage but I still think it's useful to have that long-term goal for TrinClient
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm happy to keep it. Maybe just add that little bit of context to the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks cool! It'd be nice if you could add a README.md
with a couple basic usage examples. On a broader note, I typically use a python script / ipython console to hit the rpc endpoints. The ipython repl is pretty solid for just hitting endpoints ad nauseam. Just curious, how do you see this crate evolving? To play devils advocate just a bit, would distributing a python script be simpler?
I really wish that distributing a python script was easier, but the second that script uses a library from outside the standard library then it becomes very difficult for anyone who is not a python developer to use, by bundling simple tooling with trin we can make it easy for anyone who can install trin to install the associated tooling. Unfortunately I will not be around to see it out, but my plan had a couple different prongs:
|
This is a much less ambitious version of #169 and replaces it.
Example output:
(the first line is emitted to stderr, so the output of this command can be piped into jq)