Skip to content

Commit 76dc0cd

Browse files
author
Hans Larsen
committed
fixup: canister query
1 parent 416dcf3 commit 76dc0cd

File tree

3 files changed

+37
-51
lines changed

3 files changed

+37
-51
lines changed

dfx/src/commands/call.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,24 @@ where
3030
let name = args.value_of(NAME_ARG).unwrap();
3131
let client = env.get_client();
3232

33-
let query = query(
34-
client,
35-
CanisterQueryCall {
36-
canister_id: 42,
37-
method_name: "greet".to_string(),
38-
arg: Blob(Vec::from(name)),
39-
},
40-
)
41-
.and_then(|r| match r {
42-
ReadResponse::Pending => {
43-
println!("Pending");
44-
ok(())
45-
}
46-
ReadResponse::Replied {
47-
reply: QueryResponseReply { arg: Blob(blob) },
48-
} => {
49-
println!("{}", String::from_utf8_lossy(&blob));
50-
ok(())
51-
}
52-
ReadResponse::Rejected {
53-
reject_code,
54-
reject_message,
55-
} => err(DfxError::ClientError(reject_code, reject_message)),
56-
ReadResponse::Unknown => err(DfxError::Unknown("Unknown response".to_owned())),
57-
});
33+
let query =
34+
query(client, 42, "greet".to_string(), Some(Blob(Vec::from(name)))).and_then(|r| match r {
35+
ReadResponse::Pending => {
36+
println!("Pending");
37+
ok(())
38+
}
39+
ReadResponse::Replied {
40+
reply: QueryResponseReply { arg: Blob(blob) },
41+
} => {
42+
println!("{}", String::from_utf8_lossy(&blob));
43+
ok(())
44+
}
45+
ReadResponse::Rejected {
46+
reject_code,
47+
reject_message,
48+
} => err(DfxError::ClientError(reject_code, reject_message)),
49+
ReadResponse::Unknown => err(DfxError::Unknown("Unknown response".to_owned())),
50+
});
5851

5952
let mut runtime = Runtime::new().expect("Unable to create a runtime");
6053
runtime.block_on(query)

dfx/src/commands/canister/query.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lib::api_client::{query, Blob, CanisterQueryCall, QueryResponseReply, ReadResponse};
1+
use crate::lib::api_client::{query, Blob, QueryResponseReply, ReadResponse};
22
use crate::lib::env::ClientEnv;
33
use crate::lib::error::{DfxError, DfxResult};
44
use crate::lib::CanisterId;
@@ -41,13 +41,9 @@ where
4141
let client = env.get_client();
4242
let install = query(
4343
client,
44-
CanisterQueryCall {
45-
canister_id,
46-
method_name: method_name.to_owned(),
47-
arg: arguments
48-
.map(|args| Blob(Vec::from(args[0])))
49-
.unwrap_or_else(|| Blob(vec![])),
50-
},
44+
canister_id,
45+
method_name.to_owned(),
46+
arguments.map(|args| Blob(Vec::from(args[0]))),
5147
);
5248

5349
let mut runtime = Runtime::new().expect("Unable to create a runtime");

dfx/src/lib/api_client.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,20 @@ fn submit(
176176
/// returns the canister’s response directly within the HTTP response.
177177
pub fn query(
178178
client: Client,
179-
request: CanisterQueryCall,
179+
canister_id: CanisterId,
180+
method_name: String,
181+
arg: Option<Blob>,
180182
) -> impl Future<Item = ReadResponse<QueryResponseReply>, Error = DfxError> {
181-
read(client, ReadRequest::Query { request })
183+
read(
184+
client,
185+
ReadRequest::Query {
186+
request: CanisterQueryCall {
187+
canister_id,
188+
method_name,
189+
arg: arg.unwrap_or_else(|| Blob(vec![])),
190+
},
191+
},
192+
)
182193
}
183194

184195
/// Canister Install call
@@ -358,14 +369,7 @@ mod tests {
358369
url: mockito::server_url(),
359370
});
360371

361-
let query = query(
362-
client,
363-
CanisterQueryCall {
364-
canister_id: 1,
365-
method_name: "main".to_string(),
366-
arg: Blob(vec![]),
367-
},
368-
);
372+
let query = query(client, 1, "main".to_string(), Some(Blob(vec![])));
369373

370374
let mut runtime = tokio::runtime::Runtime::new().expect("Unable to create a runtime");
371375
let result = runtime.block_on(query);
@@ -430,14 +434,7 @@ mod tests {
430434
url: mockito::server_url(),
431435
});
432436

433-
let query = query(
434-
client,
435-
CanisterQueryCall {
436-
canister_id: 1,
437-
method_name: "main".to_string(),
438-
arg: Blob(vec![]),
439-
},
440-
);
437+
let query = query(client, 1, "main".to_string(), Some(Blob(vec![])));
441438

442439
let mut runtime = tokio::runtime::Runtime::new().expect("Unable to create a runtime");
443440
let result = runtime.block_on(query);

0 commit comments

Comments
 (0)