-
Notifications
You must be signed in to change notification settings - Fork 100
Canister query and call commands #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0b14edc
775f36f
58395d2
4eafb10
fa6d0bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| use crate::lib::api_client::{call, Blob}; | ||
| use crate::lib::env::ClientEnv; | ||
| use crate::lib::error::DfxResult; | ||
| use crate::lib::CanisterId; | ||
| use crate::util::clap::validators; | ||
| use clap::{App, Arg, ArgMatches, SubCommand}; | ||
| use tokio::runtime::Runtime; | ||
|
|
||
| pub fn construct() -> App<'static, 'static> { | ||
| SubCommand::with_name("call") | ||
| .about("Call a canister.") | ||
| .arg( | ||
| Arg::with_name("canister") | ||
| .takes_value(true) | ||
| .help("The canister ID (a number) to call.") | ||
| .required(true) | ||
| .validator(validators::is_canister_id), | ||
| ) | ||
| .arg( | ||
| Arg::with_name("method_name") | ||
| .help("The method name file to use.") | ||
| .required(true), | ||
| ) | ||
| .arg( | ||
| Arg::with_name("arguments") | ||
| .help("Arguments to pass to the method.") | ||
| .takes_value(true) | ||
| .multiple(true), | ||
| ) | ||
| } | ||
|
|
||
| pub fn exec<T>(env: &T, args: &ArgMatches<'_>) -> DfxResult | ||
| where | ||
| T: ClientEnv, | ||
| { | ||
| // Read the config. | ||
| let canister_id = args.value_of("canister").unwrap().parse::<CanisterId>()?; | ||
| let method_name = args.value_of("method_name").unwrap(); | ||
| let arguments: Option<Vec<&str>> = args.values_of("arguments").map(|args| args.collect()); | ||
|
|
||
| let client = env.get_client(); | ||
| let install = call( | ||
| client, | ||
| canister_id, | ||
| method_name.to_owned(), | ||
| arguments.map(|args| Blob(Vec::from(args[0]))), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a possibility that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, but this isn't represented in typing. |
||
| ); | ||
|
|
||
| let mut runtime = Runtime::new().expect("Unable to create a runtime"); | ||
| runtime.block_on(install)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| use crate::lib::api_client::{query, Blob, QueryResponseReply, ReadResponse}; | ||
| use crate::lib::env::ClientEnv; | ||
| use crate::lib::error::{DfxError, DfxResult}; | ||
| use crate::lib::CanisterId; | ||
| use crate::util::clap::validators; | ||
| use clap::{App, Arg, ArgMatches, SubCommand}; | ||
| use tokio::runtime::Runtime; | ||
|
|
||
| pub fn construct() -> App<'static, 'static> { | ||
| SubCommand::with_name("query") | ||
| .about("Query a canister.") | ||
| .arg( | ||
| Arg::with_name("canister") | ||
| .takes_value(true) | ||
| .help("The canister ID (a number) to query.") | ||
| .required(true) | ||
| .validator(validators::is_canister_id), | ||
| ) | ||
| .arg( | ||
| Arg::with_name("method_name") | ||
| .help("The name of the method to query.") | ||
| .required(true), | ||
| ) | ||
| .arg( | ||
| Arg::with_name("arguments") | ||
| .help("Arguments to pass to the method.") | ||
| .takes_value(true) | ||
| .multiple(true), | ||
| ) | ||
| } | ||
|
|
||
| pub fn exec<T>(env: &T, args: &ArgMatches<'_>) -> DfxResult | ||
| where | ||
| T: ClientEnv, | ||
| { | ||
| // Read the config. | ||
| let canister_id = args.value_of("canister").unwrap().parse::<CanisterId>()?; | ||
| let method_name = args.value_of("method_name").unwrap(); | ||
| let arguments: Option<Vec<&str>> = args.values_of("arguments").map(|args| args.collect()); | ||
|
|
||
| let client = env.get_client(); | ||
| let install = query( | ||
| client, | ||
| canister_id, | ||
| method_name.to_owned(), | ||
| arguments.map(|args| Blob(Vec::from(args[0]))), | ||
| ); | ||
|
|
||
| let mut runtime = Runtime::new().expect("Unable to create a runtime"); | ||
| match runtime.block_on(install) { | ||
| Ok(ReadResponse::Pending) => { | ||
| println!("Pending"); | ||
| Ok(()) | ||
| } | ||
| Ok(ReadResponse::Replied { | ||
| reply: QueryResponseReply { arg: Blob(blob) }, | ||
| }) => { | ||
| println!("{}", String::from_utf8_lossy(&blob)); | ||
| Ok(()) | ||
| } | ||
| Ok(ReadResponse::Rejected { | ||
| reject_code, | ||
| reject_message, | ||
| }) => Err(DfxError::ClientError(reject_code, reject_message)), | ||
| // TODO: remove this when moving to ic_http_api. | ||
| Ok(ReadResponse::Unknown) => Err(DfxError::Unknown("Unknown response".to_owned())), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is what we want to do here. The spec says:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it was wrong before as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually Query cannot return UNKNOWN, so I don't know why that's there. That would be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In another PR I suggested having different response status types because some have pending and unknown and others don't. I think that's the way to go. |
||
| Err(x) => Err(x), | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.