Skip to content

Commit

Permalink
feat(client cli): extend client cli to request json queries
Browse files Browse the repository at this point in the history
Signed-off-by: Shanin Roman <[email protected]>
  • Loading branch information
Erigara committed Jun 6, 2024
1 parent 2bfaf1d commit d5e6f09
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
10 changes: 8 additions & 2 deletions client_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ iroha [OPTIONS] <SUBCOMMAND>
| `blocks` | Get block stream from Iroha peer |
| `domain` | Execute commands related to domains: register a new one, list all domains |
| `events` | Get event stream from Iroha peer |
| `json` | Submit multi-instructions as JSON |
| `json` | Submit multi-instructions or request query as JSON |
| `peer` | Execute commands related to peer administration and networking |
| `wasm` | Execute commands related to WASM |
| `help` | Print the help message for `iroha` and/or the current subcommand other than `help` subcommand |
Expand Down Expand Up @@ -183,5 +183,11 @@ The reference implementation of the Rust client, `iroha`, is often used for diag
To test transactions in the JSON format (used in the genesis block and by other SDKs), pipe the transaction into the client and add the `json` subcommand to the arguments:

```bash
cat /path/to/file.json | ./iroha json
cat /path/to/file.json | ./iroha json transaction
```

### Request arbitrary query

```bash
echo '{ "FindAllParameters": null }' | ./iroha --config client.toml json query
```
35 changes: 31 additions & 4 deletions client_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,11 +1046,23 @@ mod wasm {
mod json {
use std::io::{BufReader, Read as _};

use clap::Subcommand;
use iroha::data_model::query::QueryBox;

use super::*;

/// Subcommand for submitting multi-instructions
#[derive(Clone, Copy, Debug, clap::Args)]
pub struct Args;
pub struct Args {
#[clap(subcommand)]
variant: Variant,
}

#[derive(Clone, Copy, Debug, Subcommand)]
enum Variant {
Transaction,
Query,
}

impl RunArgs for Args {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
Expand All @@ -1059,9 +1071,24 @@ mod json {
reader.read_to_end(&mut raw_content)?;

let string_content = String::from_utf8(raw_content)?;
let instructions: Vec<InstructionBox> = json5::from_str(&string_content)?;
submit(instructions, UnlimitedMetadata::new(), context)
.wrap_err("Failed to submit parsed instructions")

match self.variant {
Variant::Transaction => {
let instructions: Vec<InstructionBox> = json5::from_str(&string_content)?;
submit(instructions, UnlimitedMetadata::new(), context)
.wrap_err("Failed to submit parsed instructions")
}
Variant::Query => {
let client = Client::new(context.configuration().clone());
let query: QueryBox = json5::from_str(&string_content)?;
let response = client
.request(query)
.and_then(core::convert::identity)
.wrap_err("Failed to query response")?;
context.print_data(&response)?;
Ok(())
}
}
}
}
}
Expand Down

0 comments on commit d5e6f09

Please sign in to comment.